JSPDF-多页 - 渲染HTML总是转到Page1

发布于 2025-01-31 20:58:09 字数 484 浏览 2 评论 0原文

我们使用JSPDF 2.5.1渲染多页PDF。

我们使用HTML函数将各种DOM元素呈现到每个页面,并且在JSPDF的1.x版本中都可以使用,

但是现在每次我们调用.html()时,它将其放在首页上,而不是新添加的页面,这是代码

if (pdfPageIndex < numPdfPages) {
    if (pdfPageIndex > 0) {
        pdf.addPage();
    }

    pdf.html(
        document.getElementById('pdfPage_' + pdfPageIndex),             
        {
         html2canvas: {
             logging: true
             },
         callback: function(){ return pdfCallback($scope)}});

We use jsPDF 2.5.1 to render a multi page PDF.

We use the html function to render various DOM elements to each page, and this was working in version 1.x of jsPDF

However now every time we call .html() - it puts it on the first page, rather than the newly added page, here is the code

if (pdfPageIndex < numPdfPages) {
    if (pdfPageIndex > 0) {
        pdf.addPage();
    }

    pdf.html(
        document.getElementById('pdfPage_' + pdfPageIndex),             
        {
         html2canvas: {
             logging: true
             },
         callback: function(){ return pdfCallback($scope)}});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无可置疑 2025-02-07 20:58:09

请尝试以下基于帆布高度和宽度以下的附加模特方法,它将在多个页面中呈现

const data = document.getElementById('pdfPage_');
html2canvas(data).then((canvas:any) => {
  const imgWidth = 208;
  const pageHeight = 295;
  const imgHeight = (canvas.height * imgWidth) / canvas.width;
  let heightLeft = imgHeight;
  let position = 0;
  heightLeft -= pageHeight;
  const doc = new jspdf('p', 'mm');
  doc.addImage(canvas, 'PNG', 0, position, imgWidth, imgHeight, '', 'FAST');
  while (heightLeft >= 0) {
    position = heightLeft - imgHeight;
    doc.addPage();
    doc.addImage(canvas, 'PNG', 0, position, imgWidth, imgHeight, '', 'FAST');
    heightLeft -= pageHeight;
  }
  doc.save('Downld.pdf');
});

Please try with below addImage method based on canvas height and width it will render in multiple pages

const data = document.getElementById('pdfPage_');
html2canvas(data).then((canvas:any) => {
  const imgWidth = 208;
  const pageHeight = 295;
  const imgHeight = (canvas.height * imgWidth) / canvas.width;
  let heightLeft = imgHeight;
  let position = 0;
  heightLeft -= pageHeight;
  const doc = new jspdf('p', 'mm');
  doc.addImage(canvas, 'PNG', 0, position, imgWidth, imgHeight, '', 'FAST');
  while (heightLeft >= 0) {
    position = heightLeft - imgHeight;
    doc.addPage();
    doc.addImage(canvas, 'PNG', 0, position, imgWidth, imgHeight, '', 'FAST');
    heightLeft -= pageHeight;
  }
  doc.save('Downld.pdf');
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文