Re: Barebones QZ Tray example? (Including qz-websocket.js & calling deployQZ() results in errors)

515 views
Skip to first unread message

Tres Finocchiaro

unread,
Oct 21, 2015, 11:20:49 AM10/21/15
to Nate, qz-print
We're working on a barebones example here... 

URL and content subject to change until it's been published



On Wed, Oct 21, 2015 at 11:09 AM, Nate <natha...@gmail.com> wrote:
I included qz-websocket.js in my web page and tried calling deployQZ(). The output is:

Starting deploy of qz
qz-websocket.js:43 Attempting connection on port 8181
undefined
qz-websocket.js:56 Open:
qz-websocket.js:57 Event {isTrusted: true}
qz-websocket.js:127 Websocket connection successful
jquery-1.11.3.min.js:5 Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check http://xhr.spec.whatwg.org/.
qz-websocket.js:132
Sending {"method":"listMessages","params":["<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n [THE REST OF MY WEB PAGE'S BODY]\n</html>\n"]}
qz-websocket.js:235 Uncaught TypeError: Cannot read property 'length' of undefined

Do you have just a barebones example of using QZ Tray, or can you tell me what I'm doing wrong?

Best regards,

Nate

--
You received this message because you are subscribed to the Google Groups "qz-print" group.
To unsubscribe from this group and stop receiving emails from it, send an email to qz-print+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

natha...@gmail.com

unread,
Oct 21, 2015, 12:53:17 PM10/21/15
to qz-print, natha...@gmail.com
Hi Tres,

Thanks for such a quick response.

I hate to take up more of your time as I'm sure you're busy, but I tried implementing the basic demo and it's not working, and if I can't get QZ working by tomorrow the company I'm working for will probably ask me to take a different approach.

I have the latest version of QZ Tray installed on OS X El Capitan. I installed a Zebra LP2844 per the instructions in the wiki, and it shows up in CUPS (as 'zebra') and in Printers & Scanners (as 'zebraClass').

Here's the code I have:

<html>
<head>
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="/assets/js/qz-websocket.js"></script>

<script>
$(document).ready(function () {
console.warn('CALLING deployQZ()');
deployQZ();

window.setTimeout(function() {
console.warn('CALLING findPrinter()');
findPrinter('zebra')
}, 1000);

window.setTimeout(function() {
console.warn('PRINTING');
qz.append64("SEVMTE8hICBZT1UgQVJFIFJFQURJTkcgQSBERUNPREVE");
qz.print();
}, 5000);
});

function getCertificate(callback) {
console.warn('INSIDE getCertificate()');

$.ajax({
method : 'GET',
url : '/assets/qz_tray/digital-certificate.txt', //edit this line to match the address of your certificate
async : false,
success: callback // Data returned from ajax call should be the site certificate
});
}

function signRequest(toSign, callback) {
console.warn('INSIDE signRequest()');

var data = {
request: toSign
};

$.ajax({
url : "/packing-station/action/signQzTrayRequest",
type : "POST",
dataType: "json",
data : data,
cache : false,
async : false,
success : callback
});
}

// Upon load, the function qzReady will be called automatically.
function qzReady() {
console.warn('INSIDE qzReady()');

try {
var version = qz.getVersion(); // Validate communication
console.log('qzReady() successful. Version: ' + version);
} catch (err) {
alert("Not working! " + err);
}
}

//Automatically gets called when "qz.print()" is finished.
function qzDonePrinting() {
console.warn('INSIDE qzDonePrinting()');

// Alert error, if any
if (qz.getException()) {
alert('Error printing:\n\n\t' + qz.getException().getLocalizedMessage());
qz.clearException();
return;
}

alert('Successfully sent print data to "' + qz.getPrinter() + '" queue.');
}

function findPrinter(name) {
qz.findPrinter(name);
}

function qzDoneFinding() {
if (qz.getPrinter()) {
alert("Printer " + qz.getPrinter() + " found.");
} else {
alert("Printer not found.");
}
}
</script>
</head>
<body></body>
</html>

The output is:

http://imageshack.com/a/img910/927/02YFWr.jpg

I think the issue may be finding the printer. When I go to the demo page and click Find Printer, an alert pops up saying:

Printer found: "" after searching for "zebra"

However, when I click List All Printers, the alert shows zebra in there:

Brother_HL_6180DW_series
Brother_HL_6180DW_series__001ba9dd3fab_
zebra

Any suggestions?

Thanks again for your help,

Nate

Tres Finocchiaro

unread,
Oct 21, 2015, 1:08:42 PM10/21/15
to Nathan House, qz-print
Restart QZ Tray.  It caches the printer names and it doesn't know about the newly added Zebra printer.

Also, you shouldn't use setTimout in production, we have readiness indicators (such as qzReady) for notifying the page when it is ready.

Nate

natha...@gmail.com

unread,
Oct 21, 2015, 1:08:47 PM10/21/15
to qz-print, natha...@gmail.com
Okay, actually it appears there was an error when getting the signed request. I fixed it, but it's still not finding the printer.

qz.getPrinters() returns:

"Brother_HL_6180DW_series,Brother_HL_6180DW_series__001ba9dd3fab_,zebra"

New code:

<html>
<head>
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="/assets/js/qz-websocket.js"></script>

<script>
$(document).ready(function () {
console.warn('CALLING deployQZ()');
deployQZ();

window.setTimeout(function() {
console.warn('CALLING findPrinter()');
findPrinter('zebra')

}, 2000);
});

function getCertificate(callback) {
console.warn('INSIDE getCertificate()');

$.ajax({
method : 'GET',
url : '/assets/qz_tray/digital-certificate.txt', //edit this line to match the address of your certificate
async : false,
success: callback // Data returned from ajax call should be the site certificate
});
}

function signRequest(toSign, callback) {
console.warn('INSIDE signRequest() with callback' + callback);

var data = {
request: toSign
};

$.ajax({
url : "/packing-station/action/signQzTrayRequest",
type : "POST",

data : data,
cache : false,
async : false,
success : callback
});
}

// Upon load, the function qzReady will be called automatically.
function qzReady() {
console.warn('INSIDE qzReady()');

try {
var version = qz.getVersion(); // Validate communication
console.log('qzReady() successful. Version: ' + version);
} catch (err) {
alert("Not working! " + err);
}
}

function findPrinter(name) {
qz.findPrinter(name);
}

function qzDoneFinding() {
if (qz.getPrinter()) {
alert("Printer '" + qz.getPrinter() + "' found.");
} else {

alert("Printer not found!");

natha...@gmail.com

unread,
Oct 21, 2015, 1:12:58 PM10/21/15
to qz-print, natha...@gmail.com
The alert that pops up says:

Printer not found!

So it looks like everything is working correctly, except for QZ actually finding the printer. It sees that the printer exists when qz.getPrinters() is called, as mentioned, but outputs the above error when I call:

qz.findPrinter('zebra')

I got rid of timeout and further simplified the test code:

<html>
<head>
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="/assets/js/qz-websocket.js"></script>

<script>
$(document).ready(function () {
console.warn('CALLING deployQZ()');
deployQZ();

});

console.warn('CALLING findPrinter()');
findPrinter('zebra')

Tres Finocchiaro

unread,
Oct 21, 2015, 1:14:22 PM10/21/15
to Nathan House, qz-print
I'm not sure... This would probably require an interactive WebEx session to troubleshoot properly.

I see that you are using POST instead of GET for your signing (this is recommended in production) but can be a bit harder to troubleshoot.

I recommend you put qz.findPrinter(name); inside qzReady instead of in the timeout function.

Contact me privately and we'll get a session going.  I won't have audio, but we can chat via the messenger.

Tres Finocchiaro

unread,
Oct 21, 2015, 1:48:35 PM10/21/15
to Nathan House, qz-print
Apparently our steps for adding a printer class are causing issues on your PC.  When the printer class name matches in OS X, it returns a blank printer name.

For now, either skip the printer class step, or add the printer class through the GUI.

-Tres
Reply all
Reply to author
Forward
0 new messages