How to convert HTML to pdf in angular2?

后端 未结 1 955
攒了一身酷
攒了一身酷 2021-02-11 00:01

I need to convert Dynamic generated html table in to pdf and able to print it too. I need it to be done in angular2 and Typescript.

1条回答
  •  说谎
    说谎 (楼主)
    2021-02-11 00:33

    JSPDF works for angular 2. You need to download the definitions from dt~. Import the library as:

    import * as jsPDF from "jspdf";
    .
    .
    .
    
    let doc = new jsPDF();
    
    // Add a title to your PDF
    doc.setFontSize(30); 
    doc.text(12, 10, "Your Title");
    
    // Create your table here (The dynamic table needs to be converted to canvas).
    let element = document.getElementsByClassName("pvtTable")[0];
    html2canvas(element)
    .then((canvas: any) => {
        doc.addImage(canvas.toDataURL("image/jpeg"), "JPEG", 0, 50, doc.internal.pageSize.width, element.offsetHeight / 5 );
        doc.save(`Report-${Date.now()}.pdf`);
    })
    

    In your system.js, in the map section add this line:

    "jspdf": "/jspdf.js",
    

    0 讨论(0)
提交回复
热议问题