【问题标题】:Resize html before exporting to pdf with jspdf .html() method使用 jspdf .html() 方法在导出为 pdf 之前调整 html 的大小
【发布时间】:2020-10-04 02:34:17
【问题描述】:

我正在尝试弄清楚如何使用 jsPDF .html() 方法调整 HTML 元素的大小。

我尝试使用下面的代码,但 width 选项似乎对输出没有任何改变。

我只是假设width.html() 的一个选项,不幸的是,到目前为止, 这种方法没有文档,应该猜到from the code

另外,在我看来,它应该从html2canvas options 中选择,这就是我尝试使用width 的原因。

不管怎样,代码:

var legend = document.getElementById("my-table");
var pdf = new jsPDF(orientation, undefined, format);
pdf.html(legend, {
    width: 200,
    callback: function () {
        window.open(pdf.output('bloburl'));
     }
});

#my-table

<table>
    <tr>
        <td><img src="image1.jpeg"></td>
    </tr>
    <tr>
        <td><img src="image2.jpeg"></td>
    </tr>
    ...
</table>

【问题讨论】:

    标签: javascript html pdf jspdf html2canvas


    【解决方案1】:

    将选项传递给 html2canvas:

    pdf.html(legend, {
        html2canvas: {
            // insert html2canvas options here, e.g.
            width: 200
        },
        callback: function () {
            // ...
        }
    });
    

    对我来说,scale 选项可以很好地调整大小。您可以检索默认页面尺寸listed in the source 来计算所需的缩放系数。

    【讨论】:

    • 谢谢。尝试使用 scale:0.5 并且工作正常。接受。
    • image 选项是什么?
    【解决方案2】:

    我没有找到调整 HTML 对象大小的方法。相反,您可以将测量单位从 mm(默认)更改为 pt。这会将所有尺寸从原始尺寸减小到大约 1/3。例如:

    doc = new jsPDF('portrait', 'pt', 'a4');
    

    重要提示:任何其他对象(即文本、线条等)也会在大小和位置上减小。所以要小心。

    【讨论】:

      【解决方案3】:

      我不完全了解 jspdf 是什么,但您似乎错过了拼写

      var legend = document.getElementsById("my-table");
      

      这里。

      var legend = document.getElementById("my-table");
      

      也许,您可以通过 css 或 style 属性调整这些表格的大小。

      <table style="width:200;">
          <tr>
              <td><img src="image1.jpeg"></td>
          </tr>
          <tr>
              <td><img src="image2.jpeg"></td>
          </tr>
          ...
      </table>
      

      使用css

      #my-table {
       width:200;
      }
      

      【讨论】:

      • 谢谢。我将更改拼写错误的代码。无论如何,问题是我不想通过 css 调整表格的大小,因为只要 jsPDF 将其转换为 PDF,它就会尝试适应页面大小。
      • @umbe1987 好的,或者您可以修复文档的宽度和高度,并布局所有内容。虽然只是理论......我找到了html文档。在这里查看rawgit.com/MrRio/jsPDF/master/docs/module-html.html#~html
      • 再次感谢。我知道文档的那一页,但是如果您查看它,除了“可选设置的对象”之外,没有其他选项的描述。
      猜你喜欢
      • 1970-01-01
      • 2021-10-15
      • 2014-05-26
      • 2014-06-25
      • 2018-01-07
      • 1970-01-01
      • 1970-01-01
      • 2013-11-17
      • 2015-03-30
      相关资源
      最近更新 更多