Pdf.js: How to get page width and height in pdf.js?

Created on 26 Jan 2018  路  7Comments  路  Source: mozilla/pdf.js

How to get page width and height for each page in pdf.js?

Most helpful comment

Here is an example that I have used. This will scale the PDF viewer to have a full page width. Then, it will resize the canvas to match the full size of the PDF page.

pdfDoc.getPage(num).then(function(page) {
    canvas.width = document.body.clientWidth;
    let scale = (canvas.width / page.view[2]);
    let viewport = page.getViewport({scale: scale});
    canvas.height = page.view[3] * scale;

    // Render PDF page into canvas context
    var renderContext = {
      canvasContext: ctx, // Initialized in a higher scope using: ctx = canvas.getContext('2d');
      viewport: viewport
    };
    var renderTask = page.render(renderContext);

    // etc.
}

All 7 comments

Try this:

pdf.getPage(page_index).then(page => {
    console.log(page.view);
});

@pavex what is unit of coordinates.it is a px or pt ??

@afidosstar pixels

hi, it is means canvas document size.
but i want the whole pagesize...

the data is in page.view: [left, top, width, height]

Here is an example that I have used. This will scale the PDF viewer to have a full page width. Then, it will resize the canvas to match the full size of the PDF page.

pdfDoc.getPage(num).then(function(page) {
    canvas.width = document.body.clientWidth;
    let scale = (canvas.width / page.view[2]);
    let viewport = page.getViewport({scale: scale});
    canvas.height = page.view[3] * scale;

    // Render PDF page into canvas context
    var renderContext = {
      canvasContext: ctx, // Initialized in a higher scope using: ctx = canvas.getContext('2d');
      viewport: viewport
    };
    var renderTask = page.render(renderContext);

    // etc.
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

BrennanDuffey picture BrennanDuffey  路  3Comments

patelsumit5192 picture patelsumit5192  路  3Comments

brandonros picture brandonros  路  3Comments

sujit-baniya picture sujit-baniya  路  3Comments

smit-modi picture smit-modi  路  3Comments