1

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.

Note: this is my repo for the problem, Rusydy/print-qz

Rusydy
  • 53
  • 5
  • Hi, where do you intend to run the QZ Tray desktop application? The server running Express or a PC rendering the web page? – tresf Oct 03 '22 at 03:38
  • Hi @tresf I try to create a web app using Express and run the QZ-Tray on it. – Rusydy Oct 03 '22 at 04:28
  • Ok, if you're trying to run QZ Tray on a web page (browser -- e.g. QZ Tray is installed on the PC that's rendering the page), you'll likely have to use the client-side implementation of QZ Tray. I've posted an example below. Please excuse my lack of Express experience. :) – tresf Oct 03 '22 at 04:34

1 Answers1

1

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
tresf
  • 7,103
  • 6
  • 40
  • 101
  • 1
    Thank you very much @tresf! Now I have progress on my project. However, I encounter another blocker about certificate and signing. I've watch your tutorial on YouTube [link](https://www.youtube.com/watch?v=X72y6ZZTWzE&t=777s), but I still have error on setCertificatePromise. I'll upload another question for that. – Rusydy Oct 11 '22 at 18:44
  • 1
    https://stackoverflow.com/q/74032951/11973623 – Rusydy Oct 11 '22 at 19:06