Angular 6的HTML到PDF转换器

时间:2019-04-17 10:41:43

标签: angular6 jspdf html2pdf

Angular 6最好的HTML到PDF转换器有哪些?我尝试使用

  

jspdf

以及

  

html2canvas

但是它完全破坏了HTML的设计。

请在下面检查我的代码-

import * as jspdf from 'jspdf';
import html2canvas from 'html2canvas'; 

html2canvas(document.getElementById('documentToPrint')).then(canvas => {
      var imgWidth = 208;
      var pageHeight = 295;
      var imgHeight = canvas.height * imgWidth / canvas.width;
      var heightLeft = imgHeight;

      const contentDataURL = canvas.toDataURL('image/png')
      let pdf = new jspdf('p', 'mm', 'a4');
      var position = 0;
      pdf.addImage(contentDataURL, 'PNG', 0, position, imgWidth, imgHeight);
      pdf.save('MYPdf.pdf');
    });

1 个答案:

答案 0 :(得分:0)

尝试下面的代码

const browser = await puppeteer.launch();

            const page = await browser.newPage();



            await page.setContent(`<html>`+req.body.inputValue['htmlContent']+`</html>`);//'<html><body>'+req.body.html+'</body></html>
            await page.emulateMedia('screen');


            filepath = path.join(__dirname,'../../uploads/pdf_output') +'/'+ req.body.inputValue['appNo'] + '/' + 'application-0.pdf';

            console.log("file name with path "+filepath);
            await page.pdf({


                printBackground: true,
                path: filepath

            });

            await browser.close();