Can a PDF File's Print Dialog Be Opened with JavaScript

download pdf and open it in print dialogue directly

Yes, it is possible that you open and download a PDF file in the new tab.

click here

How to open print dialog after pdf generated?

Thanks to Alex K I found the answer:

according to JRPdfExporterParameter.html#PDF_JAVASCRIPT

you can use PDF_JAVASCRIPT property to add javascript to the pdf when you generate it.

so I added

JRPdfExporter exporter = new  JRPdfExporter();
exporter.setParameter(JRPdfExporterParameter.PDF_JAVASCRIPT, "this.print();");

when exporting PDF and it worked

Print PDF directly from JavaScript

Based on comments below, it no longer works in modern browsers
This question demonstrates an approach that might be helpful to you: Silent print an embedded PDF

It uses the <embed> tag to embed the PDF in the document:

<embed
type="application/pdf"
src="path_to_pdf_document.pdf"
id="pdfDocument"
width="100%"
height="100%" />

Then you call the .print() method on the element in Javascript when the PDF is loaded:

function printDocument(documentId) {
var doc = document.getElementById(documentId);

//Wait until PDF is ready to print
if (typeof doc.print === 'undefined') {
setTimeout(function(){printDocument(documentId);}, 1000);
} else {
doc.print();
}
}

You could place the embed in a hidden iframe and print it from there, giving you a seamless experience.

I am trying to open pdf file in new window and open the print dialog after opening the window

do this in angular :-

this.http.get('https://mediasb.shiftinc.com/booking_pdfs/QALNHF.pdf', {responseType: 'blob'}).subscribe((res) => {
let url = window.URL.createObjectURL(res);
var myWindow = window.open(url, 'Booking', 'width=600,height=600');
myWindow.print();
myWindow.focus();
});

How to print a PDF from the browser

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.

Print a pdf without visually opening it

UPDATE: This link details an elegant solution that involves editing the page properties for the first page and adding an action on Page Open. Works across all browsers (as browsers will execute the JavaScript placed in the actions section). Requires Adobe Acrobat Pro.


It seems 2016 brings no new advancements to the printing problem. Had a similar issue and to make the printing cross-browser I solved it using PDF.JS but had to make a one-liner addition to the source (they ask you to build upon it in anyways).

The idea:

  • Download the pre-built stable release from https://mozilla.github.io/pdf.js/getting_started/#download and add the "build" and "web" folders to the project.
  • The viewer.html file is what renders out PDFs with a rich interface and contains print functionality. I added a link in that file to my own JavaScript that simply triggers window.print() after a delay.

The link added to viewer:

    <script src="viewer.js"></script>
<!-- this autoPrint.js was added below viewer.js -->
<script src="autoPrint.js"></script>
</head>

The autoPrint.js javascript:

(function () {
function printWhenReady() {
if (PDFViewerApplication.initialized) {
window.print();
}
else {
window.setTimeout(printWhenReady, 3000);
}
};

printWhenReady();
})();
  • I could then put calls to viewer.html?file= in the src of an iframe and hide it. Had to use visibility, not display styles because of Firefox:

    <iframe src="web/viewer.html?file=abcde.pdf" style="visibility: hidden">

The result: the print dialog showed after a short delay with the PDF being hidden from the user.

Tested in Chrome, IE, Firefox.



Related Topics



Leave a reply



Submit