Skip to content
This repository has been archived by the owner on Dec 10, 2018. It is now read-only.

1.8 QuickStart

klabarge edited this page Sep 7, 2016 · 2 revisions

Compatibility

  • ⛔ 2.x | ✅ 1.9 | ...

Steps

What is the minimal code needed for QZ Print?

QZ Print ships with a sample.html page, which is an API for using the software. While this page may be great for testing out the software, it may have unnecessary code for your environment.

The sample.html page is a beefy 1164 lines of code. In this tutorial, we will shrink the code down to under 100 lines. This will illustrate the code that is essential to properly load QZ Tray in the browser.

Note: If you are using QZ Tray (desktop version), please look at QuickStart instead.

The Code

  1. There are several JavaScript files that are provided with QZ Print. The deployJava.js is needed for the applet to be deployed.

       <html>
       <!-- License:  LGPL 2.1 or QZ INDUSTRIES SOURCE CODE LICENSE -->
       <head><title>QZ Print Plugin</title>
    
       <script type="text/javascript" src="js/3rdparty/deployJava.js"></script>
       <script type="text/javascript">
  2. This next portion of the code is responsible for deploying the applet.

       deployQZApplet();
    
       function deployQZApplet() {
           console.log('Starting deploy of qz applet');
    
           var attributes = {id: "qz", code:'qz.PrintApplet.class',
               archive:'../qz-print.jar', width:1, height:1};
           var parameters = {jnlp_href: '../qz-print_jnlp.jnlp',
               cache_option:'plugin', disable_logging:'false',
               initial_focus:'false', separate_jvm:'true'};
           if (deployJava.versionCheck("1.7+") == true) {}
           else if (deployJava.versionCheck("1.6.0_45+") == true) {}
           else if (deployJava.versionCheck("1.6+") == true) {
               delete parameters['jnlp_href'];
           }
           deployJava.runApplet(attributes, parameters, '1.6');
       }
  3. The qzDonePrinting() function is also necessary for QZ Print. qzDonePrinting() must be called after each print request, as the software can only handle one at a time. If this is not done, they will race ahead of each other and cause printing issues.

qzDonePrinting is automatically called after qz.print(), qz.printPS(), qz.printHTML(), and qz.printToFile() are finished.

   /**
    * Automatically gets called when "qz.print()" is finished.
    */
   function qzDonePrinting() {
       // Alert error, if any
       if (qz.getException()) {
           alert('Error printing:\n\n\t' + qz.getException().getLocalizedMessage());
           qz.clearException();
           return;
       }

       // Alert success message
       alert('Successfully sent print data to "' + qz.getPrinter() + '" queue.');
   }
  1. That's it! The rest of the code in the sample.html is either not needed for QZ Print (used for QZ Tray), a prototype function for printing, or HTML5.

If you want to test to see if QZ Print is properly working, add this function (along with the above code) to a webpage. This should properly list all of your printers.

   /***************************************************************************
   * Prototype function for listing all printers attached to the system
   * Usage:
   *    qz.findPrinter('\\{dummy_text\\}');
   *    window['qzDoneFinding'] = function() { alert(qz.getPrinters()); };
   ***************************************************************************/
  function findPrinters() {
          // Searches for a locally installed printer with a bogus name
          qz.findPrinter('\\{bogus_printer\\}');
   }

  // Automatically gets called when "qz.findPrinter()" is finished.
  function qzDoneFinding() {
          // Get the CSV listing of attached printers
          var printers = qz.getPrinters().replace(/,/g, '\n');
          alert(printers);
  }

  </script>
  </head>
          <input type="button" onClick="findPrinters()" value="List All Printers">
  </body>

  </html>