Can I Open A Pdf as an Image and then manipulate it?

edited February 2016 in p5.js Library Questions

is there a way to OPEN a pdf in processing?

:)

Tagged:

Answers

  • edited February 2016

    I am trying to do something like this:

    a) import PDF.js b) modify the importFile example c) pass the data to a Pimage

    //this is the code I added to the example

        PDFJS.getDocument('hellow.pdf').then(function(pdf) {
    
    
      pdf.getPage(1).then(function(page) {
        var scale = 1.5;
        var viewport = page.getViewport(scale);
    
    
        var canvas = document.getElementById('the-canvas');
        var context = canvas.getContext('2d');
        canvas.height = viewport.height;
        canvas.width = viewport.width;
    
    
        var renderContext = {
          canvasContext: context,
          viewport: viewport
        };
        page.render(renderContext);
    
     var img = new Image();
                img.onload = function (event2) {
                    sketch.newImageAvailable(img);
                }
                img.src = canvas.toDataURL(); ////Help!!
    
      });
    });
    

    few questions.

    1)How do you include a .jr file so that PDFJS visible there I tried to just create a new tap and paste it there

    2) how to make this work:

            img.src = canvas.toDataURL();
    

    3) where should I put the pdf to be loaded as a path (I don't need a load dialog) currently I am putting it in the folder with the html but it is being overwritten each time I press compile. , how can I avoid that?

    can I load from another directory ? ( on same machine )

  • edited February 2016

    also tried this:

    //I assume the pdf is automatically loaded

    function initUploader ( sketch ) {
    
    
    
        PDFJS.getDocument('http://www.cameronmoll.com/about/cameronmoll_bio.pdf').then(function(pdf) {
      // Using promise to fetch the page
      pdf.getPage(1).then(function(page) {
        var scale = 1.5;
        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);
         var img = new Image();
                img.onload = function (event2) {
                    sketch.newImageAvailable(img);
                }
                img.src =   canvas.toDataURL('image/jpeg');//tried png
    
      });
    });
    
    }
    
  • so the code breaks at PDFJS.get

    seems like it is not included properly?

Sign In or Register to comment.