"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Print PDFs Directly from JavaScript in HTML-Based Workflows?

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

Published on 2024-11-09
Browse:152

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:

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.

Release Statement This article is reprinted at: 1729426756 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3