How to display whole PDF (not only one page) with PDF.JS?

前端 未结 6 1702
无人及你
无人及你 2020-12-24 05:42

I\'ve created this demo:

http://polishwords.com.pl/dev/pdfjs/test.html

It displays one page. I would like to display all pages. One below another, or place s

6条回答
  •  独厮守ぢ
    2020-12-24 06:03

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

提交回复
热议问题