Home > Web Front-end > JS Tutorial > body text

How to Print PDFs Directly from JavaScript in HTML-Based Workflows?

DDD
Release: 2024-10-20 20:20:30
Original
912 people have browsed it

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 tag:

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

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();
    }
}
Copy after login

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!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!