【问题标题】:How to Export HTML to PDF (multiple pages) using jQuery?如何使用 jQuery 将 HTML 导出为 PDF(多页)?
【发布时间】:2021-03-26 05:30:36
【问题描述】:

我想使用运行良好的 jQuery 将 HTML 导出为 PDF,但仅适用于 HTML 将在单页 PDF 中完成时。

但是当 html 中有大量数据并且 PDF 页面可能不止一个时,我面临着将 html 导出为 PDF 的困难。

我的代码:

   $("body").on("click", "#downloadPDF", function () {
        html2canvas($('#downloadPDFData')[0], {
            onrendered: function (canvas) {
                var data = canvas.toDataURL();
                var docDefinition = {
                    content: [{
                        image: data,
                        width: 500
                    }]
                };
                pdfMake.createPdf(docDefinition).download("report.pdf");
            }
        });
    });
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.22/pdfmake.min.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.min.js"></script>
    
  <button id="downloadPDF">Download PDF</button>
  
    <div id="downloadPDFData">
    <p>
   Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    </p>
       <p>
   Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    </p>
       <p>
   Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    </p>
       <p>
   Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    </p>
       <p>
   Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    </p>
       <p>
   Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    </p>
       <p>
   Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    </p>
       <p>
   Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    </p>
       <p>
   Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    </p>
       <p>
   Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    </p>
       <p>
   Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    </p>
    </div>

我的代码有什么问题

我们将不胜感激!

【问题讨论】:

    标签: javascript html jquery pdf pdf-generation


    【解决方案1】:

    我发现了 2 个问题...

      1. 最好使用最新版本的 pdfMake。您使用的是 0.1.22...我使用的是在 cdnjs 上找到的 0.1.68
      1. 似乎 pdfMake 无法“拆分”图像并扩展到多个页面...因此您必须提供一些已拆分的块。幸运的是,我找到了一个专门针对此的 SO answer... 我在这里稍微改编了一下。

    您可以选择一个预定义的页面格式,如 letter 或 A4 等作为 pdfMake 参数。见possible options

    然后您将不得不通过尝试/失败来调整分割图像块的高度。在这里,我发现 775px 对于一个字母页面来说似乎不错...但仅使用您提供的 HTML 进行了尝试。

    所以 sn-ps 不允许下载。所以,让它成为一个 sn-p 没用,但你可以查看我的CodePen

    一些文档:


    // Slightly adapted function from this SO answer: https://stackoverflow.com/a/21937796/2159528
    // It now returns the objects formatted for pdfMake
    function getClippedRegion(image, x, y, width, height) {
      var canvas = document.createElement("canvas"),
          ctx = canvas.getContext("2d");
    
      canvas.width = width;
      canvas.height = height;
    
      //                   source region         dest. region
      ctx.drawImage(image, x, y, width, height, 0, 0, width, height);
    
      return {
        // Those are some pdfMake params
        image: canvas.toDataURL(),
        width: 500
      };
    }
    
    $("body").on("click", "#downloadPDF", function () {
    
      html2canvas($("#downloadPDFData")[0], {
        onrendered: function (canvas) {
    
          // split the canvas produced by html2canvas into several, based on desired PDF page height
          let splitAt = 775; // A page height which fits for "LETTER" pageSize...
    
          let images = [];
          let y = 0;
          while (canvas.height > y) {
            images.push(getClippedRegion(canvas, 0, y, canvas.width, splitAt));
            y += splitAt;
          }
    
          // PDF creation using pdfMake
          var docDefinition = {
            content: images,
            pageSize: "LETTER"
          };
          pdfMake.createPdf(docDefinition).download("report.pdf");
        }
      });
    });
    

    【讨论】:

    • 谢谢@Louys Patrice Bessette,它现在工作正常,正如我想要的那样(y)
    • 我建议您也支持我链接的“其他 SO 答案”......这是我答案的重要组成部分。 ;)
    • 我想让您知道,您可以将“下载到 pdf”按钮放置在页面中的任何位置,即使在 #dowloadPDF 内部,如果您将 data-html2canvas-ignore 属性添加到它(或其容器)并您希望排除的 HTML 中的任何内容。在文档中看到...并认为它真的很有用。 ;)
    猜你喜欢
    • 2023-03-20
    • 2013-08-29
    • 2014-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-25
    • 2013-08-25
    • 1970-01-01
    相关资源
    最近更新 更多