Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send a pdf file directly to the printer using JavaScript?

How to send a PDF file directly to the printer using JavaScript?

I found two answers in a forum:

<embed src="vehinvc.pdf" id = "Pdf1" name="Pdf1" hidden> <a onClick="document.getElementById('Pdf1').printWithDialog()" style="cursor:hand;">Print file</a> 

and

<OBJECT id = "Pdf2" name="Pdf2" CLASSID="clsid:CA8A9780-280D-11CF-A24D-444553540000" WIDTH="364" HEIGHT="290">      <PARAM NAME='SRC' VALUE="file.pdf"> </OBJECT> <a onClick="document.Pdf2.printWithDialog()">Print file</a>  

But my problem is that it just works on IE, and doesnt work in Firefox or Chrome.

Is there any solution for this?

like image 456
Jignesh Manek Avatar asked Aug 16 '11 05:08

Jignesh Manek


People also ask

How do I print a PDF in HTML?

Most modern browsers (e.g., Chrome, Edge, and Firefox) now have the feature to print to a PDF instead of a printer. Press the shortcut key Ctrl + P to print the page, and then in the print window that appears, change the Destination to Save as PDF or choose Adobe PDF as the printer.


1 Answers

I think this Library of JavaScript might Help you:

It's called Print.js

First Include

<script src="print.js"></script> <link rel="stylesheet" type="text/css" href="print.css"> 

It's basic usage is to call printJS() and just pass in a PDF document url: printJS('docs/PrintJS.pdf')

What I did was something like this, this will also show "Loading...." if PDF document is too large.

<button type="button" onclick="printJS({printable:'docs/xx_large_printjs.pdf', type:'pdf', showModal:true})">     Print PDF with Message </button> 

However keep in mind that:

Firefox currently doesn't allow printing PDF documents using iframes. There is an open bug in Mozilla's website about this. When using Firefox, Print.js will open the PDF file into a new tab.

like image 70
zsubzwary Avatar answered Oct 10 '22 11:10

zsubzwary