Skip to content

Instantly share code, notes, and snippets.

@rjcorwin
Last active December 20, 2015 07:19
Show Gist options
  • Save rjcorwin/6092002 to your computer and use it in GitHub Desktop.
Save rjcorwin/6092002 to your computer and use it in GitHub Desktop.
From pdf.js/examples/helloworld/hello.js, modify it to open display the page specified at the width of browser. We do this by looking at the width of the PDF's page and the width of the window and then setting the scale of the page's viewport accordingly (see line 5).
PDFJS.getDocument(url).then(function(pdf) {
// Using promise to fetch the page
pdf.getPage(1).then(function(page) {
// Scale the PDF to the width of the window
var scale = window.innerWidth / page.view[2]
var viewport = page.getViewport(scale);
//
// Prepare canvas using PDF page dimensions
//
var canvas = document.getElementById('the-canvas');
var context = canvas.getContext('2d');
canvas.height = viewport.height;
canvas.width = viewport.width;
//
// Render PDF page into canvas context
//
var renderContext = {
canvasContext: context,
viewport: viewport
};
page.render(renderContext);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment