Skip to content

Instantly share code, notes, and snippets.

@shivasurya
Last active April 28, 2017 04:13
Show Gist options
  • Save shivasurya/d1f7ee5b2bac0323d9ea078a4bcbf5d4 to your computer and use it in GitHub Desktop.
Save shivasurya/d1f7ee5b2bac0323d9ea078a4bcbf5d4 to your computer and use it in GitHub Desktop.
Pdf.js example source code - i-visionblog
PDFJS.workerSrc = 'build/pdf.worker.js';
var url = 'pdf/appathon.pdf';
var currentPage = 1;
PDFJS.getDocument(url).then(function (pdf) {
PROGRESS.max = pdf.numPages;
PROGRESS.value = 1;
var holder = document.getElementById('canvas')
if (currentPage <= pdf.numPages) getPage();
function getPage() {
pdf.getPage(currentPage).then(function(page) {
var scale = 1;
var viewport = page.getViewport(scale);
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
canvas.height = viewport.height;
canvas.width = viewport.width;
var renderContext = {
canvasContext: ctx,
viewport: viewport
};
holder.appendChild(canvas);
holder.appendChild(document.createElement("br"));
page.render(renderContext).promise.then(function() {
if (currentPage < pdf.numPages) {
currentPage++;
PROGRESS.value = currentPage;
getPage();
}
else {
//all pages are rendered use it as callback
}
});
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment