Skip to content

Instantly share code, notes, and snippets.

@jdeng
Created February 11, 2015 02:54
Show Gist options
  • Save jdeng/cbfad9cb21e452127c81 to your computer and use it in GitHub Desktop.
Save jdeng/cbfad9cb21e452127c81 to your computer and use it in GitHub Desktop.
pdf to image using pdf.js
<html>
<body>
<script type="text/javascript" src="//mozilla.github.io/pdf.js/build/pdf.js"></script>
<script type="text/javascript">
var url = "https://docs.google.com/document/export?format=pdf&id=1ML11ZyyMpnAr6clIAwWrXD53pQgNR-DppMYwt9XvE6s&token=AC4w5Vg7fSWH1Hq0SgNckx4YCvnGPaScyw%3A1423618416864";
var pages = [], heights = [], width = 0, height = 0, currentPage = 1;
var scale = 1.5;
function draw() {
var canvas = document.createElement('canvas'), ctx = canvas.getContext('2d');
canvas.width = width;
canvas.height = height;
for(var i = 0; i < pages.length; i++)
ctx.putImageData(pages[i], 0, heights[i]);
document.body.appendChild(canvas);
}
PDFJS.disableWorker = true; // due to CORS
PDFJS.getDocument(url).then(function (pdf) {
getPage();
function getPage() {
pdf.getPage(currentPage).then(function(page) {
console.log("Printing " + currentPage);
var viewport = page.getViewport(scale);
var canvas = document.createElement('canvas') , ctx = canvas.getContext('2d');
var renderContext = { canvasContext: ctx, viewport: viewport };
canvas.height = viewport.height;
canvas.width = viewport.width;
page.render(renderContext).then(function() {
pages.push(ctx.getImageData(0, 0, canvas.width, canvas.height));
heights.push(height);
height += canvas.height;
if (width < canvas.width) width = canvas.width;
if (currentPage < pdf.numPages) {
currentPage++;
getPage();
}
else {
draw();
}
});
});
}
});
</script>
</body>
</html>
@msankar1991
Copy link

Using angular how to use the above code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment