qz-tray sha-256 not defined

1.5k Views Asked by At

I am trying to print the simple data text using Angular and Qz-Tray. But everytime I try to Print the error is displayed:

qz-tray.js:35 ReferenceError: Sha256 is not defined
at Object.hash (qz-tray.js:408)
at WebSocket._qz.websocket.connection.sendData (qz-tray.js:184)
at qz-tray.js:280
at j (rsvp.min.js:1)
at new t (rsvp.min.js:1)
at Object.promise (qz-tray.js:395)
at Object.dataPromise (qz-tray.js:272)
at Object.print (qz-tray.js:805)
at PrinterService.push../src/app/shared/services/print.service.ts.PrinterService.printData (print.service.ts:46)
at FoddListComponent.push../src/app/layout/foodlist/foddlist.component.ts.FoddListComponent.createPdf (foddlist.component.ts:122)

I have included all the 3 .js files in index.html

Here is my index.html:

<head>
<meta charset="utf-8">
<title>ABC</title>
<base href="/">

<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">

<script src="https://cdn.jsdelivr.net/npm/rsvp@4/dist/rsvp.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/js-sha256/0.9.0/sha256.js"></script>
<script type="text/javascript" src="./qz-tray.js"></script>

3

There are 3 best solutions below

0
On

As @Bart has indicated, this means the sha256 is missing from your imports. He has provided a JavaScript (AngularJS) example because you have angular in your question tags however your example uses HTML tags. For completeness, here's the HTML requirements:

<!-- Required scripts -->
<script type="text/javascript" src="rsvp-3.1.0.min.js"></script>
<script type="text/javascript" src="sha-256.min.js"></script>
<script type="text/javascript" src="qz-tray.js"></script>

Alternately, you can avoid RSVP by doing the following:

qz.api.setPromiseType(function promise(resolver) { return new Promise(resolver); });

And, if running HTTPS with JavaScript running on a compatible browser you can avoid SHA256 by doing the following:

function sha256 (str) {
    // We transform the string into an arraybuffer.
    var buffer = new TextEncoder('utf-8').encode(str)
    return crypto.subtle.digest('SHA-256', buffer).then(function (hash) {
        return hex(hash)
    })
}

function hex (buffer) {
    var hexCodes = []
    var view = new DataView(buffer)
    for (var i = 0; i < view.byteLength; i += 4) {
        // Using getUint32 reduces the number of iterations needed (we process 4 bytes each time)
        var value = view.getUint32(i)
        // toString(16) will give the hex representation of the number without padding
        var stringValue = value.toString(16)
        // We use concatenation and slice for padding
        var padding = '00000000'
        var paddedValue = (padding + stringValue).slice(-padding.length)
        hexCodes.push(paddedValue)
    }

    // Join all the hex strings into one
    return hexCodes.join('')
}

qz.api.setSha256Type(function (data) {
    return sha256(data)
})

... but most people are just fine with @Bart's code snippet. Here's the full snippet from the QZ Tray wiki, AngularJS section:

import * as qz from 'qz-tray';
import * as sha256 from 'js-sha256';

qz.api.setSha256Type(function(data) { return sha256(data); });
qz.api.setPromiseType(function promise(resolver) { return new Promise(resolver); });

qz.websocket.connect()
.then(qz.printers.getDefault)
.then(function(printer) {
   console.log("The default printer is: " + printer);
})
.then(qz.websocket.disconnect)
.catch(function(err) {
   console.error(err);
});
0
On

You probably forget the import:

import * as sha256 from 'js-sha256';
qz.api.setSha256Type(function(data) { return sha256(data); });
0
On

I have got the similar issue. Resolve it by importing sha56 module by require. Here is code. try once let me know if you are facing any issues.

declare var require: any;
import { Injectable } from '@angular/core';
import * as qz from 'qz-tray';
var sha1= require('js-sha256');

qz.api.setSha256Type(function (data) {
  var hashed = sha1(data);
  return hashed;
});