I try to make a web app to print on LQ-310 Printer using express and QZ-Tray. However, I cannot execute the printing process as expected, it only triggered to print if users hit the endpoint, if I put the QZ-Tray functions inside another function. I have tried to create a dedicated file for it and execute it using bash/shell script if users trigger the endpoint like this:
const printInvoice = (req, res) => {
fs.writeFile(`src/invoice/invoice.json`, JSON.stringify(req.body), (err) => {
if (err) throw err;
});
// shell script to run qz-tray
const shell = require('shelljs');
shell.echo('Running qz-tray');
shell.exec('bash src/invoice/qz-tray.sh');
fs.readFile(`src/invoice/invoice.json`, (err, data) => {
if (err) throw err;
res.json(JSON.parse(data));
});
}
However, my terminal return no result, not even error message.
I also have set out to just do it inside one function on route, but found no result.
QZ Tray is most commonly a client-side library. This means that the JavaScript calls must originate from the client (browser), not the server (node).
Here's a very simple boilerplate layout.jade
to get Express + Jade talking to QZ Tray. It's built on top of the express application generator.
Note 1: Please excuse any fundamental app design or templating mistakes below as I don't have experience with Express -- or Jade for that matter.
Note 2: Node/server/backend communication with QZ Tray is supported too, but this combination is much less common.
# put qz-tray.js in an accessible, public directory
curl -o public/javascripts/qz-tray.js https://raw.githubusercontent.com/qzind/tray/master/js/qz-tray.js
doctype html
html
head
title= title
link(rel='stylesheet', href='/stylesheets/style.css')
/***************************
* code changes start here *
***************************/
script(src='/javascripts/qz-tray.js', type='text/javascript')
script.
qz.websocket.connect().then(() => {
return qz.printers.find()
}).then(printers => {
document.getElementsByTagName("p")[0].innerHTML += "<p>Found printers:</p><ul>";
printers.forEach(printer => {
document.getElementsByTagName("p")[0].innerHTML += "<li>" + printer + "</li>";
});
document.getElementsByTagName("p")[0].innerHTML += "</ul>";
});
/***************************
* end of code changes *
***************************/
body
block content