Antoine Aubry
Antoine Aubry

Reputation: 12459

How to print a PDF from the browser

In a Web application, is it possible to force a PDF file to be printed on the client? If the browser is configured to open the PDF inside the window, I guess that calling window.print() will work, but some browsers (like mine) are configured to open the PDF externally.

Upvotes: 28

Views: 105724

Answers (8)

konnic
konnic

Reputation: 74

If you want to print a PDF via the browser's print dialog, you can render the PDF inside an iframe on which you call the print() method.

// create iframe element
const iframe = document.createElement('iframe');

// create object URL for your blob or file and set it as the iframe's src
iframe.src = window.URL.createObjectURL(fileOrBlob);
iframe.name = 'pdf';

// call the print method in the iframe onload handler
iframe.onload = () => {
    const pdfFrame = window.frames["pdf"];
    pdfFrame.focus();
    pdfFrame.print();
}
document.body.appendChild(iframe);

You can also set iframe.hidden = true; to do all of this in a hidden iframe, which the user won't notice.

This solution should work with all major browser's except Legacy Microsoft Edge. If you need to be compatible with Legacy Microsoft Edge you can fall back to the npm package print-js: https://www.npmjs.com/package/print-js.

Upvotes: 3

Ali
Ali

Reputation: 1200

you can use the simple amazing library printjs "http://printjs.crabbly.com" it takes PDF file and print it without showing print dialog if you need , a simple way to do it below :

 <button type="button" onclick="printJS('docs/printjs.pdf')">
    Print PDF
 </button>

Upvotes: 2

Simone
Simone

Reputation: 247

You can't print a PDF document directly from browser using Javascript. The Javascript function window.print() use the browser printing function and this is not what you need. You can achieve your aim starting the print through Java Web Start. Put your PDF document directly into the jnlp so you can run a Java program that recieve the raw PDF document as argument. Now you're running in the system and no longer in the browser, so you can directly interface with printing driver through JAVA API. This seem quite simple but really it's not because the JAVA printing API doesn't accept a file as input but a particular data structure that implements the ava.awt.print.Pageable interface.

Exist a web service at www.pdfprint.it that do all the work for you. Here a snippet taken from the official documentation.

<?php

// 1. GET the jnlp file with curl

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.pdfprint.it/printPdf?auth=XXXX");        
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //return the transfer as a string       
$jnlp = curl_exec($ch);
curl_close($ch); 

$pdfDoc ="example.pdf";


//2. put in the jnlp your PDF document base64 encoded

$jnlp = str_replace("####", base64_encode(file_get_contents($pdfDoc)),$jnlp);


//3. echo the jnlp file 

header('Content-type: application/x-java-jnlp-file');

echo $jnlp;

You only need to get the jnlp file, put in your PDF document and send the jnlp to the browser. The JAVA program that run the printing will be dowloaded directly from the web service. You can also set some printing options as copies, sides, and so on

Upvotes: 0

Aaron Renoir
Aaron Renoir

Reputation: 4381

similarly to taeyoung's suggestion you can use an iframe to render the pdf and then use contentWindow.print();

Upvotes: 2

taeyoung
taeyoung

Reputation: 65

<html>
<script language="javascript">
timerID = setTimeout("exPDF.print();", 1000);
</script>
<body>
<object id="exPDF" type="application/pdf" data="111.pdf" width="100%" height="500"/>
</body>
</html>

Upvotes: 4

theman_on_vista
theman_on_vista

Reputation:

you can set a http header to application/pdf and then force a new window open with javascript and print that way. but who would really do that? i mean come on now.

Upvotes: 1

DOK
DOK

Reputation: 32831

Do you mean that you want to force the file to be sent to a printer? Are you thinking of the Law of Unintended Consequences -- the user's device isn't connected to a printer? Could be a BlackBerry, could be a laptop on wi-fi. What if the user doesn't want it to go to the default printer?

Upvotes: 1

Lou Franco
Lou Franco

Reputation: 89142

The way google docs does it is by embedding JavaScript into the PDF that tells Acrobat Reader or any other compliant reader to print it.

You would need a PDF toolkit to do this with a random PDF.

Upvotes: 29

Related Questions