Home >Web Front-end >JS Tutorial >How to Print PDFs Directly from JavaScript in HTML-Based Workflows?
Printing PDFs Directly from JavaScript
In HTML-based workflows, integrating direct print functionality for PDFs can be a valuable addition for users. To achieve this, several approaches can be explored.
One technique involves embedding the PDF within the document using the
<embed type="application/pdf" src="path_to_pdf_document.pdf" id="pdfDocument" width="100%" height="100%" />
Once embedded, JavaScript can be used to trigger printing:
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(); } }
This method allows for seamless printing without displaying the PDF to the user. Embedded PDFs can be placed within hidden iframes for a more user-friendly experience. However, it's worth noting that this approach may not be compatible with all modern browsers.
The above is the detailed content of How to Print PDFs Directly from JavaScript in HTML-Based Workflows?. For more information, please follow other related articles on the PHP Chinese website!