使用JSPDF-Autotable在同一文档中打印表和图表

发布于 2025-02-08 21:26:19 字数 1286 浏览 2 评论 0原文

我如何使用JSPDF自动化印刷图表或图像和表格? 假设我有一个图表,我想将其放在桌子上。 或者我有一些客户端自定义的HTML文本,我需要将其与自定义HTML下方的小表相结合。 我试图将最高边距提高到插入图像的开放空间,这有点有效。 但是,如果我不知道桌子前或之后必须插入元素的大小怎么办?

  async printThisThingsTogether() {
    const doc = new jsPDF('p', 'mm','a4');
    const marginsParent = { top: 105, bottom: 35, left: 15, right: 15 };
    doc.setFontSize(5);
    doc.setPage(1);

    var coolCanvas = document.getElementById('coolCanvas')! as HTMLCanvasElement;
    var newCanvasImg = coolCanvas!.toDataURL("image/png", 1.0)!;
    doc.addImage(newCanvasImg, 'PNG', 10, 10, 190, 90 );

    let page = document.getElementById(`table`) as HTMLTableElement;
    autoTable(doc, {
      html: page,
      margin: marginsParent,
      useCss: false,
      theme: 'plain',
      styles: {
        lineColor: [44, 62, 80],
        lineWidth: 1,
      },
      headStyles: {
        fillColor: [241, 196, 15],
        fontSize: 15,
      },
      footStyles: {
        fillColor: [241, 196, 15],
        fontSize: 15,
      },
      bodyStyles: {
        fillColor: [52, 73, 94],
        textColor: 240,
      },
      alternateRowStyles: {
        fillColor: [74, 96, 117],
      },
    });
    await doc.output('dataurlnewwindow', {filename: 'listagem.pdf'});
  }

How would I print both a chart or image and a table, using jspdf-autotable?
Let's say I have a chart and I want to put that right before the table.
Or I have some HTML text customized by a client and I need to combine it with a small table right below the custom HTML.
I tried to increase the top margin to open space to the image inserted, it kinda worked.
But what if I don't know the size of the element I'll have to insert before or after the table?

  async printThisThingsTogether() {
    const doc = new jsPDF('p', 'mm','a4');
    const marginsParent = { top: 105, bottom: 35, left: 15, right: 15 };
    doc.setFontSize(5);
    doc.setPage(1);

    var coolCanvas = document.getElementById('coolCanvas')! as HTMLCanvasElement;
    var newCanvasImg = coolCanvas!.toDataURL("image/png", 1.0)!;
    doc.addImage(newCanvasImg, 'PNG', 10, 10, 190, 90 );

    let page = document.getElementById(`table`) as HTMLTableElement;
    autoTable(doc, {
      html: page,
      margin: marginsParent,
      useCss: false,
      theme: 'plain',
      styles: {
        lineColor: [44, 62, 80],
        lineWidth: 1,
      },
      headStyles: {
        fillColor: [241, 196, 15],
        fontSize: 15,
      },
      footStyles: {
        fillColor: [241, 196, 15],
        fontSize: 15,
      },
      bodyStyles: {
        fillColor: [52, 73, 94],
        textColor: 240,
      },
      alternateRowStyles: {
        fillColor: [74, 96, 117],
      },
    });
    await doc.output('dataurlnewwindow', {filename: 'listagem.pdf'});
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仙气飘飘 2025-02-15 21:26:19

如果要将表格/图像/HTML内容放在表格上方,则应设置自动操作的starty,这是指启动表的y点。

如果要将非表内容放在表面下方,则应获取表通过
doc.lastautotable.finaly其中 doc 涉及JSPDF实例。然后将内容放在其中。

希望它有帮助。

干杯

If you want to put chart/image/html content above the table, you should set startY of the autoTable which refers to starting Y point of the table.

If you want to put non-table content below the table then you should get the the Y point where the table ends via
doc.lastAutoTable.finalY in which doc refers the jsPDF instance. Then put the content with respect to that.

Hope it helps.

Cheers

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文