The Mechanic The Mechanic - 4 months ago 27
Javascript Question

(epson tm-t20ii) print multiple html files with qz-print

I tried to print multiple html files in folder with qz-print and EPSON - TM-T20II, but seem no work.

This is an example of script that i used:

function printPages() {
if (notReady()) {
return;
}

qz.appendHTMLFile(getPath() + "misc/out-may-21.html");
qz.appendHTMLFile(getPath() + "misc/out-may-22.html");


while (!qz.isDoneAppending()) {
qz.printHTML();
}


but it will only print "out-may-22.html" file.
- And one more problem, when printing html file, it print html file, then print one more text file with tag "".

So confused,
thanks.

Answer

Update: Since QZ Tray 2.0, the new API supports a data block which supports multiple HTML files without confusing callbacks.

For those still using QZ Print/QZ Tray 1.9, the function qz.appendHTMLFile("..."); is not synchronous and cannot currently be called in succession.

Instead, you'll have to wait until qzDoneAppending() is called, then call printHTML(), then call qz.appendHTMLFile("..."); again, etc.

<script>

   // Called automatically when the software has loaded
   function qzReady() {
      qz.findPrinter("epson");
   }

   // Called automatically when the software has finished searching for the printer
   function qzDoneFinding() {
       // append first file
       qz.appendHTMLFile("first.html");
   }

   var secondHasAppended= false;

   // Called automatically when a file is done appending
   function qzDoneAppending() {
       qz.print();
   }

   // Called automatically when document has been sent to the printer
   function qzDonePrinting() {
       if (!secondHasAppended) {
           qz.appendHTMLFile("second.html");
           secondHasAppended = true;
           // qzDoneAppending and qzDonePrinting will take care of the rest
       } else {
           alert("Done!");
       }
   }

</script>

If you require more than two documents, replace the boolean with a counter.