简体   繁体   English

如何使用 jsPDF 呈现多个分页结果(HTML 到 PDF)

[英]How can I use jsPDF to render multiple pagination results (HTML to PDF)

I'm trying to print the result of multiple jsPDF.HTML(...).我正在尝试打印多个 jsPDF.HTML(...) 的结果。 Each one of them runs in a different pagination of a list inside the same div.它们中的每一个都在同一 div 内的列表的不同分页中运行。 The PDF conversion of that div displays the first page multiple times even though I can see the items inside it changing in the UI before executing each jsPDF.HTML(...).该 div 的 PDF 转换多次显示第一页,即使在执行每个 jsPDF.HTML(...) 之前我可以看到其中的项目在 UI 中发生变化。

Is there a solution for this or at least an explanation of why it is happening?是否有解决方案或至少解释为什么会发生这种情况?

  async printReceipt() {
    console.log('getting the number of pages in the pagination');
    var lenArr = new Array(Math.ceil(this.receiptItems.length / 10));
    var doc = new jsPDF('p', 'pt', [496, 702]);
    var arrayBuffer: ArrayBuffer[] = [];
    var fileURL;
    console.log('creatting main doc');
    for (let index = 0; index < lenArr.length; index++) {
      console.log('Changing pagination to:', index + 1);
      this.page = index + 1;
      console.log('Converting page:', index + 1);
      await new Promise<void>(async (resolve, reject) => {
        doc.html(document.getElementById('receipt'), {
          callback: async (res) => {
            console.log('Adding page to buffer, page:', index + 1);
            arrayBuffer.push(res.output('arraybuffer'));
            if (lenArr.length - 1 == index) {
              console.log('Printing');
              await this.mergePdfs(arrayBuffer);
            }
            resolve();
          },
        });
      });
    }
  }

logs order:日志顺序:

在此处输入图像描述

I solved the problem by moving the declaration of the jsPDF object into the promise and adding a timeout between the page change and the promise.我通过将 jsPDF object 的声明移动到 promise 并在页面更改和 promise 之间添加超时来解决了这个问题。

  async printReceipt() {
    console.log('getting the number of pages in the pagination');
    var lenArr = new Array(Math.ceil(this.receiptItems.length / 10));
    var arrayBuffer: ArrayBuffer[] = [];
    var fileURL;
    console.log('creatting main doc');
    for (let index = 0; index < lenArr.length; index++) {
      console.log('Changing pagination to:', index + 1);
      this.page = index + 1;
      await this.sleep(1000);
      console.log('Converting page:', index + 1);
      await new Promise<void>(async (resolve, reject) => {
        ***var doc = new jsPDF('p', 'pt', [496, 702]);***
        doc.html(document.getElementById('receipt'), {
          callback: async (res) => {
            console.log('Adding page to buffer, page:', index + 1);
            arrayBuffer.push(res.output('arraybuffer'));
            if (lenArr.length - 1 == index) {
              console.log('Printing');
              await this.mergePdfs(arrayBuffer);
            }
            resolve();
          },
        });
      });
    }
  }

  sleep(ms) {
    return new Promise((resolve) => setTimeout(resolve, ms));
  }

It appears that the jsPDF object preserves the first result of jsPDF.html(...) no matter if this method is executed multiple times. jsPDF object似乎保留了 jsPDF.html(...) 的第一个结果,无论此方法是否多次执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM