最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Printing HTML + Multiple PDF files with Javascript - Stack Overflow

programmeradmin3浏览0评论

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 .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');
});

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');
});
Share Improve this question edited May 23, 2017 at 12:09 CommunityBot 11 silver badge asked Mar 1, 2013 at 22:25 JamieJamie 3691 gold badge6 silver badges22 bronze badges 2
  • I think something is wrong with your parameters... – Ricardo Ortega Magaña 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? – Jamie Commented Mar 1, 2013 at 23:27
Add a ment  | 

1 Answer 1

Reset to default 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 plete load, then, send the print mand:

<!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>
发布评论

评论列表(0)

  1. 暂无评论