2

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');
});
2
  • I think something is wrong with your parameters... Commented Mar 1, 2013 at 22:28
  • Thanks for the feedback @RicardoOrtegaMagaña. Do you mean when they're being used in the jquery function at the bottom of the page, or in the creation of the PrintAll function? Commented Mar 1, 2013 at 23:27

1 Answer 1

6

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.