首页 > 解决方案 > jsPDF- html() 新函数没有为 pdf 页面设置边距的功能

问题描述

我在 vue cli 应用程序中使用 jsPDF,但我找不到任何解决方案来设置 pdf 页面的边距。所有的文本和图像都在页面上溢出。我想设置所有四个边距。jsPDF 文档中给出的设置边距的语法也不起作用。

这是我生成和下载 pdf 的功能:

downloadpdf() {
  var pdf = new jsPDF("p", "pt", "a4");
  pdf.setFontSize("10");
  pdf.setLineWidth("100");
  pdf.setTextColor("darkblue");

  // Printing text
  var vuejsinformation = `JQuery is a lightweight, "write less, do more", JavaScript library.`;
  pdf.text(vuejsinformation, 10, 10);
  //pdf.save("Sample.pdf");

  // Printing a complete div container who has class pdfrapper
  pdf.html(window.jQuery(".pdfwrapper").get(0), {
    callback: function (pdf) {
      pdf.save("Sample.pdf");
    },
    x: 10,
    y: 10,
  });
},

如何设置所有四个边距?

标签: jspdf

解决方案


您可以在 jspdf.html() 方法中添加一个选项 var,如下所示:

jspdf.html(src, {
margin: number,
html2canvas: {width: number, height: number, ...}
...
})

但是,似乎存在使用此方法时忽略边距的问题,请参阅https://github.com/MrRio/jsPDF/issues/2924。它可能很快就会修复:)。

这是 jspdf.html() 方法的文档http://raw.githack.com/MrRio/jsPDF/master/docs/module-html.html


推荐阅读