65

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 some buttons to change page or even better load all standard controls of PDF.JS like in Firefox. How to acomplish this?

Tom Smykowski
  • 25,487
  • 54
  • 159
  • 236

11 Answers11

78

PDFJS has a member variable numPages, so you'd just iterate through them. BUT it's important to remember that getting a page in pdf.js is asynchronous, so the order wouldn't be guaranteed. So you'd need to chain them. You could do something along these lines:

var currPage = 1; //Pages are 1-based not 0-based
var numPages = 0;
var thePDF = null;

//This is where you start
PDFJS.getDocument(url).then(function(pdf) {

        //Set PDFJS global object (so we can easily access in our page functions
        thePDF = pdf;

        //How many pages it has
        numPages = pdf.numPages;

        //Start with first page
        pdf.getPage( 1 ).then( handlePages );
});



function handlePages(page)
{
    //This gives us the page's dimensions at full scale
    var viewport = page.getViewport( 1 );

    //We'll create a canvas for each page to draw it on
    var canvas = document.createElement( "canvas" );
    canvas.style.display = "block";
    var context = canvas.getContext('2d');
    canvas.height = viewport.height;
    canvas.width = viewport.width;

    //Draw it on the canvas
    page.render({canvasContext: context, viewport: viewport});

    //Add it to the web page
    document.body.appendChild( canvas );

    //Move to next page
    currPage++;
    if ( thePDF !== null && currPage <= numPages )
    {
        thePDF.getPage( currPage ).then( handlePages );
    }
}
Don Rhummy
  • 24,730
  • 42
  • 175
  • 330
  • this doesn't work for me. My canvas is inside of a div and when run above code it shows the pdf pages on top of each other at the end of the page (not div) – Sara Feb 04 '15 at 04:12
  • 3
    @Sara you need to learn DOM. the above code is an example only. it appends the created pages to the document. you need to put them in your div and style the canvases as needed in your project. but all that is outside the scope of this question – Don Rhummy Feb 04 '15 at 05:02
  • Thanks for quick response:) I added div and it adds the canvas to the right place but it overwrite them.. – Sara Feb 04 '15 at 05:12
  • @Sara without seeing your code it's impossible to know what's wrong. why don't you post your code in a new question describing the issue? – Don Rhummy Feb 04 '15 at 05:16
  • did that. It is here:http://stackoverflow.com/questions/28314124/pdfjs-how-to-create-more-than-one-canvas-to-show-all-the-pages – Sara Feb 04 '15 at 05:30
  • @DonRhummy But its image! I want exactly something like https://mozilla.github.io/pdf.js/web/viewer.html ;;;;;;;;;;;;; text can selected and it's searchable. How can i achieve this? Thanks. – Dr.jacky Jul 12 '15 at 05:56
  • 1
    @Mr.Hyde i haven't looked at this project in years. it's very likely the api has methods to help with that but you can still use canvas and listen to mouse events to implement text selection. – Don Rhummy Jul 12 '15 at 21:15
  • you are appending canvas to body `document.body.appendChild( canvas );` but i have dynamic overlayscreen where i have to render pdf.how can i do that? i am using TOuchPDF library which is using pdf.js – JAVA_RMI Mar 06 '17 at 15:08
  • @JAVA_RMI https://developer.mozilla.org/en-US/docs/Web/API/Node/appendChild You can append a node to any element. – Don Rhummy Mar 06 '17 at 15:36
  • @DonRhummy yeah i am trying same but can't https://github.com/loicminghetti/touchpdf/blob/master/jquery.touchPDF.js#L514 – JAVA_RMI Mar 06 '17 at 15:41
  • @JAVA_RMI I'm not going to look through all that code. That's a very long file. You need to write a test case that just gets a PDF doc and appends the page to an element to see if you can reproduce. I will point out that the loading of PDF pages is *asynchronous* and your "goto" code appears to assume it's synchronous. – Don Rhummy Mar 06 '17 at 17:44
  • 1
    perfect solution – coder May 15 '19 at 23:17
  • I created my canvases first (dynamically) then I get each of them as I am iterating through the pages using document.getElementById.... you just have to give our cava – FernandoZ Jan 03 '20 at 14:24
  • If you have to render an array of PDFs, this solution still works, but you will need to change the global variables to an array of objects with each of the properties defined above, otherwise the properties of the different PDFs will overwrite each other. See below (https://stackoverflow.com/a/65727954/11809641) for rendering multiple PDFs. – user11809641 Jan 14 '21 at 22:59
  • Does this works ? Does it bring all the pages ? I've 19 page; will I be apply the same ? – Ritesh Aryal Oct 19 '22 at 17:31
  • Great solution! Using this approach, how do I run some code after every page has been rendered? Been stuck on trying to find where the code finishes rendering the PDF – Graham Billington Nov 27 '22 at 15:22
51

Here's my take. Renders all pages in correct order and still works asynchronously.

<style>
  #pdf-viewer {
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.1);
    overflow: auto;
  }
  
  .pdf-page-canvas {
    display: block;
    margin: 5px auto;
    border: 1px solid rgba(0, 0, 0, 0.2);
  }
</style>

<script>   
    url = 'https://github.com/mozilla/pdf.js/blob/master/test/pdfs/tracemonkey.pdf';
    var thePdf = null;
    var scale = 1;
    
    PDFJS.getDocument(url).promise.then(function(pdf) {
        thePdf = pdf;
        viewer = document.getElementById('pdf-viewer');
        
        for(page = 1; page <= pdf.numPages; page++) {
          canvas = document.createElement("canvas");    
          canvas.className = 'pdf-page-canvas';         
          viewer.appendChild(canvas);            
          renderPage(page, canvas);
        }
    });
    
    function renderPage(pageNumber, canvas) {
        thePdf.getPage(pageNumber).then(function(page) {
          viewport = page.getViewport({ scale: scale });
          canvas.height = viewport.height;
          canvas.width = viewport.width;          
          page.render({canvasContext: canvas.getContext('2d'), viewport: viewport});
    });
    }
</script>

<div id='pdf-viewer'></div>
Clément
  • 12,299
  • 15
  • 75
  • 115
Reto Höhener
  • 5,419
  • 4
  • 39
  • 79
  • 6
    Brilliant - Thank you! – Mark G Dec 14 '17 at 19:55
  • 1
    Simple and clean. Made it scale = 2 for web. – Vael Victus Sep 05 '18 at 16:13
  • How does this solution ensure the correct page order? Seems to me that it could still go out of order in a race condition, as you are iterating through the pages to render, but not waiting for the previous page to finish processing? Please do correct me if I misread it – redfox05 Jun 25 '20 at 15:56
  • 1
    @redfox05 the canvas elements are created and appended in order. the render function then works on the canvas that it received as argument. – Reto Höhener Jun 25 '20 at 17:15
  • 1
    @RetoHöhener, thanks, yep I wondered that just now myself hence I came back to check if you'd replied. It just clicked when I thought about passing the references. So the canvas is created, in order, and then the reference to that is passed on to the render function, so when it finishes loading that page, it throws it into the original canvas element it came from, thus rendering it in order :) I think in my implementation I'll add some sort of ID count into the element, to make it more obvious to the next developer. – redfox05 Jun 26 '20 at 13:39
  • btw, technically having the renderPage function seems obsolete as its not called from anywhere else but that loop. Im guessing that could just be left in the loop itself? Readability I guess though – redfox05 Jun 26 '20 at 13:42
  • oh, slight typo. Missing a `var` on the `viewer`,`page`(for loop), `canvas` and `viewport` variables. – redfox05 Jun 26 '20 at 13:46
  • I changed the call to `page.getViewport` from `page.getViewport(scale)` to `page.getViewport({ scale: scale })`, since that's what seem to works in recent releases. https://github.com/mozilla/pdf.js/blob/master/src/display/api.js – Clément Aug 25 '21 at 21:45
16

The pdfjs-dist library contains parts for building PDF viewer. You can use PDFPageView to render all pages. Based on https://github.com/mozilla/pdf.js/blob/master/examples/components/pageviewer.html :

var url = "https://cdn.mozilla.net/pdfjs/tracemonkey.pdf";
var container = document.getElementById('container');
// Load document
PDFJS.getDocument(url).then(function (doc) {
  var promise = Promise.resolve();
  for (var i = 0; i < doc.numPages; i++) {
    // One-by-one load pages
    promise = promise.then(function (id) {
      return doc.getPage(id + 1).then(function (pdfPage) {
// Add div with page view.
var SCALE = 1.0; 
var pdfPageView = new PDFJS.PDFPageView({
      container: container,
      id: id,
      scale: SCALE,
      defaultViewport: pdfPage.getViewport(SCALE),
      // We can enable text/annotations layers, if needed
      textLayerFactory: new PDFJS.DefaultTextLayerFactory(),
      annotationLayerFactory: new PDFJS.DefaultAnnotationLayerFactory()
    });
    // Associates the actual page with the view, and drawing it
    pdfPageView.setPdfPage(pdfPage);
    return pdfPageView.draw();        
      });
    }.bind(null, i));
  }
  return promise;
});
#container > *:not(:first-child) {
  border-top: solid 1px black; 
}
<link href="https://npmcdn.com/pdfjs-dist/web/pdf_viewer.css" rel="stylesheet"/>
<script src="https://npmcdn.com/pdfjs-dist/web/compatibility.js"></script>
<script src="https://npmcdn.com/pdfjs-dist/build/pdf.js"></script>
<script src="https://npmcdn.com/pdfjs-dist/web/pdf_viewer.js"></script>

<div id="container" class="pdfViewer singlePageView"></div>
async5
  • 2,505
  • 1
  • 20
  • 27
12

The accepted answer is not working anymore (in 2021), due to the API change for var viewport = page.getViewport( 1 ); to var viewport = page.getViewport({scale: scale});, you can try the full working html as below, just copy the content below to a html file, and open it:

<html>
<head>
<script src="https://mozilla.github.io/pdf.js/build/pdf.js"></script>
<head>
<body>
</body>

<script>

var url = 'https://raw.githubusercontent.com/mozilla/pdf.js/ba2edeae/web/compressed.tracemonkey-pldi-09.pdf';

// Loaded via <script> tag, create shortcut to access PDF.js exports.
var pdfjsLib = window['pdfjs-dist/build/pdf'];

// The workerSrc property shall be specified.
pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://mozilla.github.io/pdf.js/build/pdf.worker.js';

var currPage = 1; //Pages are 1-based not 0-based
var numPages = 0;
var thePDF = null;

//This is where you start
pdfjsLib.getDocument(url).promise.then(function(pdf) {

        //Set PDFJS global object (so we can easily access in our page functions
        thePDF = pdf;

        //How many pages it has
        numPages = pdf.numPages;

        //Start with first page
        pdf.getPage( 1 ).then( handlePages );
});


function handlePages(page)
{
    //This gives us the page's dimensions at full scale
    var viewport = page.getViewport( {scale: 1.5} );

    //We'll create a canvas for each page to draw it on
    var canvas = document.createElement( "canvas" );
    canvas.style.display = "block";
    var context = canvas.getContext('2d');

    canvas.height = viewport.height;
    canvas.width = viewport.width;

    //Draw it on the canvas
    page.render({canvasContext: context, viewport: viewport});

    //Add it to the web page
    document.body.appendChild( canvas );

    var line = document.createElement("hr");
    document.body.appendChild( line );

    //Move to next page
    currPage++;
    if ( thePDF !== null && currPage <= numPages )
    {
        thePDF.getPage( currPage ).then( handlePages );
    }
}
</script>

</html>
Sam YC
  • 10,725
  • 19
  • 102
  • 158
4

The following answer is a partial answer targeting anyone trying to get a PDF.js to display a whole PDF in 2019, as the api has changed significantly. This was of course the OP's primary concern. inspiration sample code

Please take note of the following:

  • extra libs are being used -- Lodash (for range() function) and polyfills (for promises)....
  • Bootstrap is being used
    <div class="row">
        <div class="col-md-10 col-md-offset-1">
            <div id="wrapper">

            </div>
        </div>
    </div>

    <style>
        body {
            background-color: #808080;
            /* margin: 0; padding: 0; */
        }
    </style>    
    <link href="//cdnjs.cloudflare.com/ajax/libs/pdf.js/2.1.266/pdf_viewer.css" rel="stylesheet"/>    

    <script src="//cdnjs.cloudflare.com/ajax/libs/pdf.js/2.1.266/pdf.js"></script>    
    <script src="//cdnjs.cloudflare.com/ajax/libs/pdf.js/2.1.266/pdf_viewer.js"></script>
    <script src="//cdn.polyfill.io/v2/polyfill.min.js"></script>    
    <script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>
    <script>
        $(document).ready(function () {
            // startup
        });

        'use strict';

        if (!pdfjsLib.getDocument || !pdfjsViewer.PDFViewer) {
            alert("Please build the pdfjs-dist library using\n" +
                "  `gulp dist-install`");
        }

        var url = '//www.pdf995.com/samples/pdf.pdf';

        pdfjsLib.GlobalWorkerOptions.workerSrc =
            '//cdnjs.cloudflare.com/ajax/libs/pdf.js/2.1.266/pdf.worker.js';

        var loadingTask = pdfjsLib.getDocument(url);
        loadingTask.promise.then(function(pdf) {
            // please be aware this uses .range() function from lodash
            var pagePromises = _.range(1, pdf.numPages).map(function(number) {
                return pdf.getPage(number);
            });
            return Promise.all(pagePromises);
        }).then(function(pages) {
                var scale = 1.5;
                var canvases = pages.forEach(function(page) {
                    var viewport = page.getViewport({ scale: scale, }); // Prepare canvas using PDF page dimensions

                    var canvas = document.createElement('canvas');
                    canvas.height = viewport.height;
                    canvas.width = viewport.width; // Render PDF page into canvas context

                    var canvasContext = canvas.getContext('2d');
                    var renderContext = {
                        canvasContext: canvasContext,
                        viewport: viewport
                    };
                    page.render(renderContext).promise.then(function() {
                        if (false)
                            return console.log('Page rendered');
                    });
                    document.getElementById('wrapper').appendChild(canvas);
                });
            },
            function(error) {
                return console.log('Error', error);
            });
    </script>
petrosmm
  • 528
  • 9
  • 23
2

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

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>PDF Sample</title>
    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript" src="pdf.js"></script>
    <script type="text/javascript" src="main.js">
    </script>
    <link rel="stylesheet" type="text/css" href="main.css">
</head>
<body id="body">  
</body>
</html>

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);
});
Vladimir Trifonov
  • 1,395
  • 11
  • 9
1

First of all please be aware that doing this is really not a good idea; as explained in https://github.com/mozilla/pdf.js/wiki/Frequently-Asked-Questions#allthepages

How to do it;

Use the viewer provided by mozilla; https://mozilla.github.io/pdf.js/web/viewer.html

modify BaseViewer class, _getVisiblePages() method in viewer.js to

/* load all pages */ 
_getVisiblePages() {
      let visible = [];
      let currentPage = this._pages[this._currentPageNumber - 1];
      for (let i=0; i<this.pagesCount; i++){
        let aPage = this._pages[i];
        visible.push({ id: aPage.id, view: aPage, });
      }
      return { first: currentPage, last: currentPage, views: visible, };
    }
Fayyaz Ali
  • 787
  • 1
  • 8
  • 18
0

If you want to render all pages of pdf document in different canvases

<html>
   <head>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <script src="pdf.js"></script>
      <script src="jquery.js"></script>
   </head>
   <body>
      <h1>PDF.js 'Hello, world!' example</h1>
      <div id="canvas_div"></div>
      <body>
         <script>
            // If absolute URL from the remote server is provided, configure the CORS
            // header on that server.
            var url = 'pdff.pdf';          
            // Loaded via <script> tag, create shortcut to access PDF.js exports.
            var pdfjsLib = window['pdfjs-dist/build/pdf'];            
            // The workerSrc property shall be specified.
            pdfjsLib.GlobalWorkerOptions.workerSrc = 'worker.js';
            var loadingTask = pdfjsLib.getDocument(url);
                loadingTask.promise.then(function(pdf) {
      var __TOTAL_PAGES = pdf.numPages; 
       // Fetch the first page
       var pageNumber = 1;     
     for( let i=1; i<=__TOTAL_PAGES; i+=1){
      var id ='the-canvas'+i;
      $('#canvas_div').append("<div style='background-color:gray;text-align: center;padding:20px;' ><canvas calss='the-canvas' id='"+id+"'></canvas></div>");    
        var canvas = document.getElementById(id);
        //var pageNumber = 1;
        renderPage(canvas, pdf, pageNumber++, function pageRenderingComplete() {
       if (pageNumber > pdf.numPages) {
         return; 
       }
       // Continue rendering of the next page
       renderPage(canvas, pdf, pageNumber++, pageRenderingComplete);
        });    
     }               
                });           
                function renderPage(canvas, pdf, pageNumber, callback) {
                  pdf.getPage(pageNumber).then(function(page) {
                    var scale = 1.5;
                    var viewport = page.getViewport({scale: scale});            
                    var pageDisplayWidth = viewport.width;
                    var pageDisplayHeight = viewport.height;
              //var pageDivHolder = document.createElement();
                    // Prepare canvas using PDF page dimensions
                    //var canvas = document.createElement(id);
                    var context = canvas.getContext('2d');
                    canvas.width = pageDisplayWidth;
                    canvas.height = pageDisplayHeight;
                   // pageDivHolder.appendChild(canvas);           
                    // Render PDF page into canvas context
                    var renderContext = {
                      canvasContext: context,
                      viewport: viewport
                    };
                    page.render(renderContext).promise.then(callback);
                  });
                }           
         </script>
         <html>
parmod
  • 25
  • 4
0

The accepted answer works perfectly for a single PDF. In my case there were multiple PDFs that I wanted to render all pages for in the same sequence of the array.

I adjusted the code so that the global variables are encapsulated in an object array as follows:

    var docs = []; // Add this object array
    var urls = []; // You would need an array of the URLs to start.

    // Loop through each url. You will also need the index for later.
    urls.forEach((url, ix) => {

        //Get the document from the url.
        PDFJS.getDocument(url).then(function(pdf) {

            // Make new doc object and set the properties of the new document
            var doc = {};

            //Set PDFJS global object (so we can easily access in our page functions
            doc.thePDF = pdf;
    
            //How many pages it has
            doc.numPages = pdf.numPages;
    
            //Push the new document to the global object array
            docs.push(doc);            

            //Start with first page -- pass through the index for the handlePages method
            pdf.getPage( 1 ).then(page => handlePages(page, ix) );
    });
});
    
    
    
    function handlePages(page, ix)
    {
        //This gives us the page's dimensions at full scale
        var viewport = page.getViewport( {scale: 1} );
    
        //We'll create a canvas for each page to draw it on
        var canvas = document.createElement( "canvas" );
        canvas.style.display = "block";
        var context = canvas.getContext('2d');
        canvas.height = viewport.viewBox[3];
        canvas.width = viewport.viewBox[2];
    
        //Draw it on the canvas
        page.render({canvasContext: context, viewport: viewport});
    
        //Add it to an element based on the index so each document is added to its own element
        document.getElementById('doc-' + ix).appendChild( canvas );
    
        //Move to next page using the correct doc object from the docs object array
        docs[ix].currPage++;
        if ( docs[ix].thePDF !== null && docs[ix].currPage <= docs[ix].numPages )
        {
            console.log("Rendering page " + docs[ix].currPage + " of document #" + ix);
            docs[ix].thePDF.getPage( docs[ix].currPage ).then(newPage => handlePages(newPage, ix) );
        }
    }

Because the entire operation is asynchronous, without a unique object for each document, global variables of thePDF, currPage and numPages will be overwritten when subsequent PDFs are rendered, resulting in random pages being skipped, documents entirely skipped or pages from one document being appended to the wrong document.

One last point is that if this is being done offline or without using ES6 modules, the PDFJS.getDocument(url).then() method should change to pdfjsLib.getDocument(url).promise.then().

user11809641
  • 815
  • 1
  • 11
  • 22
0

Make it to be iterate every page how much you want.

const url = '/storage/documents/reports/AR-2020-CCBI IND.pdf';

pdfjsLib.GlobalWorkerOptions.workerSrc = '/vendor/pdfjs-dist-2.12.313/package/build/pdf.worker.js';

const loadingTask = pdfjsLib.getDocument({
    url: url,
    verbosity: 0
});

(async () => {
    const pdf = await loadingTask.promise;
    let numPages = await pdf.numPages;

    if (numPages > 10) {
        numPages = 10;
    }

    for (let i = 1; i <= numPages; i++) {
        let page = await pdf.getPage(i);
        let scale = 1.5;
        let viewport = page.getViewport({ scale });
        let outputScale = window.devicePixelRatio || 1;

        let canvas = document.createElement('canvas');
        let context = canvas.getContext("2d");

        canvas.width = Math.floor(viewport.width * outputScale);
        canvas.height = Math.floor(viewport.height * outputScale);
        canvas.style.width = Math.floor(viewport.width) + "px";
        canvas.style.height = Math.floor(viewport.height) + "px";

        document.getElementById('canvas-column').appendChild(canvas);

        let transform = outputScale !== 1 
            ? [outputScale, 0, 0, outputScale, 0, 0] 
            : null;

        let renderContext = {
            canvasContext: context,
            transform,
            viewport
        };

        page.render(renderContext);
    }
})();
hery
  • 32
  • 1
0

If you want update your single page PDF Viewer to show all pages and scroll through all of them, these are my changes

I had to change a PDF viewer from showing a single page to showing the full set of pages and be scrollable, so I had the code already for showing the one page, and to show all the pages each page needed to have it's own canvas, and because I am creating my canvas using the useRef() hook, I couldn't just put it in a function that will loop and create a canvas when it was building the pdf pages to be viewed. This is the error I kept getting when I used that hook in a function:.

Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons: 1. You might have mismatching versions of React and the renderer (such as React DOM) 2. You might be breaking the Rules of Hooks 3. You might have more than one copy of React in the same app See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.

Also, the other obstacle I had was when the const canvasRef = useRef(null) was created, the object looked like this when I needed it to be ready to hold page data: {current: null}. So there was a timing issue, I needed to have the DOM ready in that area of the react page before the canvasRef would blossom to a full canvas object, so I created a state hook to let me know when the DOM is ready, then kick off all the functions to render the pages for the PDF. Note, there was a renderPage function that took a pdfJs object, after my updates it took the pdfJs, the page number and the created canvas for that PDF page.

Here are my code snippets:

  const canvasRef = useRef(null);

  const canvasRefs: any[] = useMemo(() => {
    const tempRefs: any[] = []
      for(let i = 0; i < 4; i++) {
        tempRefs[i] = { ...canvasRef}
      }
      return tempRefs;
    }, [])


  useEffect(() => {
    if (canvasRefs[0]?.current) {
      setIsDomReady(true);
    }
  });

  useEffect(() => {
    if (isDomReady && pdfJs) {
      for(let i = 0; i < numPages; i++) {
        renderPage(pdfJs, canvasRefs[i].current, i+1);
      }
    }
  }, [pdfJs, isDomReady]);

  const renderPage = async (pdfJs, canvas, pgNumber) => { ...
Joey Garcia
  • 315
  • 3
  • 7