Search code examples
angular7

How to display pdf on canvas using angular7 and fabric js


I want to display a pdf as an image on canvas using angular7 and fabric js

i am not able to find any code to try in angular7


Solution

  • Finally i am able to solve this...i have used pdfjsLib and i have imported in my ts file (import * as pdfjsLib from 'pdfjs-dist/build/pdf';)

     public src="/assets/myPhotos.pdf";
     showPdf(){
    pdfjsLib.getDocument(this.src).then(doc =>{
    
     // console.log("this file has "+ doc._pdfInfo.numPages+ "pages");
      doc.getPage(1).then(page => {
        var myCanvas =  <HTMLCanvasElement>document.getElementById("my_canvas");
        var context = myCanvas.getContext("2d");
        var scale = 1.5;
        var viewport = page.getViewport(scale);
        myCanvas.width = viewport.width;
        myCanvas.height = viewport.height;
    
        page.render({
          canvasContext : context,
          viewport : viewport
        })
      })
    });
    

    }