将 html 字符串中的图像包含到 pdf 文件下载中

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

我有以下函数,它需要大量的 html 字符串并将它们转换为 pdf 文件:

  async signedAgreements(documentstoDownload) {
    this.loading = true;

    // Create an array to hold the promises of each download operation
    const downloadLimit = limit(5); // Set the maximum number of concurrent downloads
    const downloadPromises = documentstoDownload.map((documenttoDownload) =>
      downloadLimit(async () => {
        // Create a new element to hold the HTML content
        const wrapper = document.createElement("div");
        wrapper.innerHTML = documenttoDownload.document;

        // Set the options for html2pdf
        const options = {
          margin: 10,
          filename:
            documenttoDownload.school + ": " + documenttoDownload.document_name,
          image: { type: "jpeg", quality: 0.98 },
          html2canvas: { scale: 2, logging: true, scrollX: 0, scrollY: 0 },
          jsPDF: { unit: "mm", format: "a4", orientation: "portrait" },
        };

        // Call the html2pdf function to convert and save as PDF
        await html2pdf().from(wrapper).set(options).save();
      })
    );

    // Wait for all the download promises to complete before proceeding
    await Promise.all(downloadPromises);

    this.loading = false;
  }

但是,它并没有显示pdf中的图像,它只是在它们应该在的地方留下了很大的空间。有没有办法将它们包含在内?

还有什么办法可以加快这个功能的速度吗?一些需要下载的文档大约有 20 页,目前下载其中 5 个文档大约需要 20 秒。

提前致谢!

html angular typescript html2pdf promise.all
© www.soinside.com 2019 - 2024. All rights reserved.