How to know if PDF.JS has finished rendering?

后端 未结 12 1132
情深已故
情深已故 2020-12-13 14:44

I am using PDF.JS to render pdf pages into different canvas elements. my requirement is to capture the output of the canvas and to display it as an image. Is there some even

12条回答
  •  感情败类
    2020-12-13 15:08

    If you want to render all pages of pdf document in different canvases, all one by one synchronously this is kind of solution:

    index.html

    
    
    
        
        PDF Sample
        
        
        
        
    
      
    
    
    

    main.css

    canvas {
        display: block;
    }
    

    main.js

    $(function() {  
        var filePath = "document.pdf";
    
        function Num(num) {
            var num = num;
    
            return function () {
                return num;
            }
        };
    
        function renderPDF(url, canvasContainer, options) {
            var options = options || {
                    scale: 1.5
                },          
                func,
                pdfDoc,
                def = $.Deferred(),
                promise = $.Deferred().resolve().promise(),         
                width, 
                height,
                makeRunner = function(func, args) {
                    return function() {
                        return func.call(null, args);
                    };
                };
    
            function renderPage(num) {          
                var def = $.Deferred(),
                    currPageNum = new Num(num);
                pdfDoc.getPage(currPageNum()).then(function(page) {
                    var viewport = page.getViewport(options.scale);
                    var canvas = document.createElement('canvas');
                    var ctx = canvas.getContext('2d');
                    var renderContext = {
                        canvasContext: ctx,
                        viewport: viewport
                    };
    
                    if(currPageNum() === 1) {                   
                        height = viewport.height;
                        width = viewport.width;
                    }
    
                    canvas.height = height;
                    canvas.width = width;
    
                    canvasContainer.appendChild(canvas);
    
                    page.render(renderContext).then(function() {                                        
                        def.resolve();
                    });
                })
    
                return def.promise();
            }
    
            function renderPages(data) {
                pdfDoc = data;
    
                var pagesCount = pdfDoc.numPages;
                for (var i = 1; i <= pagesCount; i++) { 
                    func = renderPage;
                    promise = promise.then(makeRunner(func, i));
                }
            }
    
            PDFJS.disableWorker = true;
            PDFJS.getDocument(url).then(renderPages);       
        };
    
        var body = document.getElementById("body");
        renderPDF(filePath, body);
    });
    

提交回复
热议问题