Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 460
Tom Smykowski Avatar asked May 10 '13 10:05

Tom Smykowski


People also ask

How do I view a PDF in pages?

An HTML anchor link is the easiest way to display a PDF file. But if you want to display a PDF document on the web page, PDF file needs to be embedded in HTML. The HTML <embed> tag is the best option to embed PDF document on the web page.

What is Pdfjslib?

A typed array with the first portion or all of the pdf data. Used by the extension since some data is already loaded before the switch to range requests.

How do I preview a PDF in JavaScript?

JS library is initialized taking the object url as the source url of the PDF. PDF. JS' APIs getDocument and getPage are used to render the PDF. The first page of the PDF is rendered as an image, and that is shown as the preview of the PDF.

How do I render a PDF in canvas?

getElementById('cv'); var context = canvas. getContext('2d'); var scale = 1.5; var canvasWidth=0; var canvasHeight=0; var pageStarts=new Array(); pageStarts[0]=0; PDFJS. getDocument(url). then(function getPdfHelloWorld(_pdf) { pdf = _pdf; //Render all the pages on a single canvas for(var i = 1; i <= pdf.


1 Answers

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 );     } } 
like image 119
Don Rhummy Avatar answered Sep 24 '22 00:09

Don Rhummy