Skip to content

Instantly share code, notes, and snippets.

@imolorhe
Forked from jdeng/pdf2img.html
Created August 13, 2018 09:38
Show Gist options
  • Select an option

  • Save imolorhe/f8794d3bb55e1a8065b23bcd0efeebe1 to your computer and use it in GitHub Desktop.

Select an option

Save imolorhe/f8794d3bb55e1a8065b23bcd0efeebe1 to your computer and use it in GitHub Desktop.

Revisions

  1. @jdeng jdeng created this gist Feb 11, 2015.
    53 changes: 53 additions & 0 deletions pdf2img.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    <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>