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

PostScript Printing

Tres Finocchiaro edited this page Dec 8, 2015 · 19 revisions

The following code can be used for PostScript Printing only. If you are unsure what PostScript Printing is, please refer to What is PostScript Printing?


###HTML Printing

  1. QZ Print can print advanced HTML components by rendering an HTML component to an image using the html2canvas jQuery plugin (bundled in the /dist/js folder).

    <body id="content" bgcolor="#FFF380"> ... 
    <canvas id="hidden_screenshot" style="display:none;" /> ...
    
    // JavaScript (jQuery)
    $("#content").html2canvas({ 
             canvas: hidden_screenshot,
             onrendered: function() {
                qz.append64($("canvas")[0].toDataURL('image/png'));
                qz.printPS();
             }
        });

Note: HTML id #content can be virtually any HTML component. HTML id hidden_screenshot is a temporary place to draw the content and should be set display=none to prevent the render from becoming visiable.

  1. QZ Print has the ability to print basic HTML components to a PostScript style printer directly from the browser. (PostScript printers include all major LaserJet, DeskJet, PSC, Home Printers, etc). Mileage may vary across different operating systems. Please file a bug report using ISSUES link above if you encounter issues.

    // Basic HTML Printing
    qz.appendHTML("<html><h1><font color=Red>Hello world!</font></h1>qz-print HTML Printing Demo</html>");
    qz.printHTML();
    
    // Printing using extra spaces, fixed font, dashes
    var text = "*no-line-break*       Hello from ’qz-print’       *no-line-break*";
    
    // Fix whitespace and special character behavior
    text = text.replace(/ /g, "&nbsp;").replace(//g, "'").replace(/-/g,"&#8209;");      
    
    qz.appendHTML('<html><table border=1 style="font-size:10pt; font-face:\'Courier New\';"><tr><td>' + text + '</td></tr></table></html>');
    
    qz.printHTML();
    
    // Printing an existing HTML file
    qz.appendHTMLFile("../form.html");
    
    // Wait for append to finish
    function qzDoneAppending() {
        qz.printHTML();
    }

 > **Linux HTML Printing:** There is a bug preventing margins from working properly on Linux: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6726691

***

###PDF Printing

QZ Print allows printing PDF files directly to a printer. This functionality is experimental and uses a 3rd party renderer "PDF-RENDERER". 

 > **Note:** This feature will not work with many raw printing devices, such as Zebra, Epson, Citizen printers. The data is sent in PostScript format, which is more common for LaserJet printers. USE WITH CAUTION!

   ```js
   // Make sure pdf-renderer.jar is in the "./lib" folder
   // Append the PDF
   qz.appendPDF(window.location.href + "/../sample.pdf");

   function qzDoneAppending() {
      // Very important for PDFs, use "printPS()" instead of "print()"
      qz.printPS();
   }

###Base64 PDF Printing

// Must start with "data:application/pdf;base64,XXXXXXXXXX"
qz.appendPDF("data:application/pdf;base64,dGVzdA[...]==");

function qzDoneAppending() {
   // Very important for PDFs, use "printPS()" instead of "print()"
   qz.printPS();
}

CUPS/Linux PDF Printing: Please note, there is a bug preventing margins from working properly on some versions of CUPS: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6726691


###Printing Images

Experimental support for appending printer-specific images by url and/or Base64.

// DeskJet/LaserJet, etc (don't mix with raw devices)
// Paper size/orientation since version 1.4.5
qz.setPaperSize("210mm", "297mm");  // A4
// qz.setPaperSize("8.5in", "11.0in");  // US Letter
qz.setAutoSize(true);               // Preserve proportions
qz.setOrientation("landscape");     // Default is "portrait"
qz.appendImage("image.png");        // Append our image.  Note: Wait for this to finish, or implement   "jzebraDoneAppending()"
qz.printPS();