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

collinanderson picture collinanderson  路  29Comments

soa-x picture soa-x  路  174Comments

Richard-Mlynarik picture Richard-Mlynarik  路  32Comments

Rob--W picture Rob--W  路  43Comments

Vanuan picture Vanuan  路  34Comments