Jamie
Jamie

Reputation: 379

Printing HTML + Multiple PDF files with Javascript

I've got a project where I'm needing to print the HTML page the user is on, as well as multiple PDF files (multiple prompts is OK)

After hunting around on Google and Stackoverflow I found Printing multiple PDF files using JavaScript which is close, but not quite working.

The code below prints the HTML page fine, and creates a print prompt for the PDF, but the PDF print prompt is for a blank page.

I've triple checked the example address and the fine is definitely at http://www.mydomain.co.nz/wp-content/uploads/Golden-China1.pdf.

Anyone spot what I'm doing wrong?

function PrintAll(file1, file2, file3) {
    window.print();
    var pages = [file1, file2, file3];
    for (var i = 0; i < pages.length; i++) {
        if (pages[i] != undefined) {
            var oWindow = window.open(pages[i], "print");
            oWindow.print();
            oWindow.close();
        }
    }
}

$('.print-btn').click(function() {
    PrintAll('/wp-content/uploads/Golden-China1.pdf');
});

Upvotes: 2

Views: 13540

Answers (1)

I tested some code, and the problem i was getting with your code, is the timeload, added a delay so i let the PDF to complete load, then, send the print command:

<!DOCTYPE html>
<html>
<body>
<script>
var pages = ["P1.pdf", "P2.pdf", "P3.pdf"];
var oWindow=new Array();
function PrintAll(){
    for (var i = 0; i < pages.length; i++) {
        oWindow[i].print();
        oWindow[i].close();

    }
}
    function OpenAll() {
    for (var i = 0; i < pages.length; i++) {
        oWindow[i] = window.open(pages[i]);
    }
    setTimeout("PrintAll()", 5000);
    }
    OpenAll();
</script>
</body>
</html>

Upvotes: 6

Related Questions