Print.js

A tiny javascript library to help printing from the web.

PDF Printing

Print.js was primarily written to help us print PDF files directly within our apps, without leaving the interface, and no use of embeds. For unique situations where there is no need for users to open or download the PDF files, and instead, they just need to print them.

One scenario where this is useful, for example, is when users request to print reports that are generated on the server side. These reports are sent back as PDF files. There is no need to open these files before printing them. Print.js offers a quick way to print these files within our apps.

PDF files must be served from the same domain as your app is hosted under. Print.js uses iframe to load files before printing them, therefore, it is limited by the Same Origin Policy. This helps preventing Cross-site scripting (XSS) attacks.

Example

Add a button to print a PDF file located on your hosting server:

   

For large files, you can show a message to the user when loading files.

  

Print Large PDF ( 5mb file ) Print Extra Large PDF ( 16mb file )

The library supports base64 PDF printing:

  

Print base64 PDF

HTML Printing

Sometimes we just want to print selected parts of a HTML page, and that can be tricky. With Print.js, we can easily pass the id of the element that we want to print. The element can be of any tag, as long it has a unique id. The library will try to print it very close to how it looks on screen, and at the same time, it will create a printer friendly format for it.

Example

Add a print button to a HTML form:

  

Print.js accepts an object with arguments. Let's print the form again, but now we will add a header to the page:

  

Print Form with Header

Image Printing

Print.js can be used to quickly print any image on your page, by passing the image url. This can be useful when you have multiple images on the screen, using a low resolution version of the images. When users try to print the selected image, you can pass the high resolution url to Print.js.

Example

Load images on your page with just the necessary resolution you need on screen:

  

In your javascript, pass the highest resolution image url to Print.js for a better print quality:

 printJS('images/print-01-highres.jpg', 'image')