PDF.js - 一次打印多个文档

时间:2015-03-06 20:02:15

标签: javascript pdf pdf.js pdfjs

我需要在由AJAX响应确定的页面上放置多个PDF,并且当所有文档和页面都已渲染时,请调用window.print。

每个文档的页面(画布)都被添加到一个带有“pdfdoc'按文件保持页面顺序。

我是Promises的新手,由于异步性质,我无法确定何时调用window.print。有时它会在所有页面都可用之前被调用,即使我在调用它之前检查渲染保证中最后一个文档的最后一页。以下是代码:

// Called on each iteration of coredatalist
var getDoc = function(count) {
    PDFJS.getDocument(srcUrl).then(function(pdf) {
        var currentPage = 1;
        $('<div class="pdfdoc" id="doc' + count + '"></div>').appendTo('body');
        getPage(pdf, currentPage, count);
    });
};

// Called for each page in PDF
var getPage = function(pdf, currentPage, count) {
    pdf.getPage(currentPage).then(function(page) {
        var scale = 2,
            viewport = page.getViewport(scale),
            canvas = document.createElement('canvas'),
            context = canvas.getContext('2d'),
            renderContext = {
                canvasContext: context,
                viewport: viewport,
                intent: 'print'
            };

        // Prepare canvas using PDF page dimensions

        canvas.setAttribute('class', 'canvaspdf');
        canvas.id = 'canvas' + currentPage;
        canvas.height = viewport.height;
        canvas.width = viewport.width;

        // Render PDF page into canvas context

        page.render(renderContext).then(function(page) {
            $('#doc' + count).append(canvas);
            if (currentPage < pdf.numPages) {
                currentPage++;
                getPage(pdf, currentPage, count);
            } else {
                if (parseInt(count) === coredatalist.length - 1) {
                    window.print();
                }
            }
        });
    });
};

// Iterate through JSON object from AJAX request
for (var j in coredatalist) {
    if (coredatalist.hasOwnProperty(j)) {
        srcUrl = coredatalist[j].props.downloaduri;
        getDoc(j);
    }
}

在呈现所有文档和页面后,是否有人知道如何调用window.print?

1 个答案:

答案 0 :(得分:2)

对于任何有兴趣的人,我通过将PDFJS.getDocument()包含在带有回调'next'的函数中来满足我的要求:

loadPdf: function (pdfUrl, pdfindex, next) {
    var self = this;
    PDFJS.getDocument(pdfUrl).then(function (pdf) {

        var currentPage = 1;
        function getPage() {
            pdf.getPage(currentPage).then(function (page) {
                var scale = 2,
                    viewport = page.getViewport(scale),
                    canvas = document.createElement('canvas'),
                    context = canvas.getContext('2d'),
                    renderContext = {
                        canvasContext: context,
                        viewport: viewport,
                        intent: 'print'
                    };

                // Prepare canvas using PDF page dimensions

                canvas.setAttribute('class', 'canvaspdf');
                canvas.id = 'canvas' + currentPage + '-' + pdfindex;
                canvas.height = viewport.height;
                canvas.width = viewport.width;

                // Render PDF page into canvas context

                page.render(renderContext).then(function (page) {
                    document.body.appendChild(canvas);
                    if (currentPage < pdf.numPages) {
                        currentPage++;
                        getPage();
                    } else {
                        next();
                    }
                });
            });
        }

        if (currentPage < pdf.numPages) {
            getPage();
        }
    });
}

然后在回调中调用它:

var pdfUrls = [],

    next = function () {
        if (pdfUrls.length === 0) {
            window.print();
        } else {
            self.loadPdf(pdfUrls.pop(), pdfUrls.length, next);
        }
    };

for (var j in coredatalist) {
    if (coredatalist.hasOwnProperty(j)) {
        pdfUrls.push('url');
    }
}

next();