如何使用jspdf和html2canvas获取生成的pdf的文件大小(以字节为单位)

问题描述 投票:0回答:1

我目前正在使用html2canvas和jspdf将html页面转换为PDF。我想检索文件大小(以字节为单位)以在屏幕上显示给用户。如果有人知道一种方便的方法,那将是有帮助的。感谢您的帮助:)

这里是将html转换为PDF的代码:

 exportPDF() {
const data = document.getElementById("content");
html2canvas(data).then(canvas => {
  // Few necessary setting options
  const imgWidth = 208;
  const imgHeight = (canvas.height * imgWidth) / canvas.width;

  const contentDataURL = canvas.toDataURL("image/png");
  const pdf = new jsPDF("p", "mm", "a4"); // A4 size page of PDF
  const position = 0;
  pdf.addImage(contentDataURL, "PNG", 0, position, imgWidth, imgHeight);
  pdf.save("file.pdf"); // Generated PDF
});

}

angular jspdf html2canvas
1个回答
1
投票

使用blob将其输出,像这样。

  let blob = pdf.output("blob");
  console.log(blob.size);
  pdf.save("file.pdf"); // Generated PDF
© www.soinside.com 2019 - 2024. All rights reserved.