当一个表增加行时,如何调整JSPDF文档中的元素,避免表或文本垂直重叠?

发布于 2025-01-30 08:05:53 字数 1949 浏览 2 评论 0原文

我是React和JavaScript的初学者。我一直在尝试找到解决问题的解决方案,但似乎找不到特定的解决方案。我正在使用“ JSPDF”和它的插件“ JSPDF-autotable”生成.pdf文档(客户发票)。

它都会生成确定的,给出每个元素(文本,图像,表),它是xy启动在JavaScript代码中的坐标。我有2张桌子。表A和表B。两者都垂直将一个位于另一个位置。当“表A”增加时,它是一段时间开始重叠“表B”的时间。表B具有固定数量的行。

如何避免“表A重叠”表B”,并在“表A”增加时将表A“动态'向下'移动'向下移动'表A的所有内容?

示例:

const generatePDF = () => {

        const unit = "pt";
        const size = "A4"
        const orientation = "portrait";

        const doc = new jsPDF(orientation, unit, size);

        let contentA = {
            head: [["Item", "Quantity", "Price", "Item Total"]],
           
            body: [
                ['Water Bottle A', '1', '8.99', '8.99'],
                ['Water Bottle B', '1', '8.99', '8.99'],
                ['Water Bottle C', '1', '8.99', '8.99'],
                ['Water Bottle D', '1', '8.99', '8.99'],
               ],
            foot: [['', '', 'Total:', '35.96']],
            margin: 40,
            tableLineWidth: 0.5,
            tableLineColor: "black",
            tableWidth: 'auto',
            startY: 100,
            theme: 'striped',
            pageBreak: 'auto',
        }

        let contentB = {
            head: [["Subtotal", "$35.96"]],
           
            body: [
                ['Taxes', '$4.49'],
                ['Payment Method:', 'Cash'],
               ],
            foot: [['Total:', '35.96']],
            margin: 40,
            tableLineWidth: 0.5,
            tableLineColor: "black",
            tableWidth: 'auto',
            startY: 150,
            theme: 'striped',
            pageBreak: 'auto',
        }

        doc.autoTable(contentA)
        doc.autoTable(contentB)
        doc.text('Thank you for your purchase!', 150, 800);
        doc.save("my-report-document.pdf")
}

这里的所有行都是静态的(在我的应用程序中,我会通过一系列对象循环并盖上行,但是此示例解释了我的问题)将与“ contentb”表重叠。

“ starty”属性是目前唯一建立每个表的启动'y坐标的属性。我不介意“表A”下面的内容分为第二页,我只想摆脱垂直重叠。

任何帮助将不胜感激。

I'm a beginner in React and Javascript. I've been trying to find a solution to my problem but can't seem to find the specific solution. I'm generating a .pdf document (customer invoice) using "jsPdf" and it's plugin "jspdf-autoTable".

It all generates OK giving each element (text, image, table) it's XY starting coordinates in the javascript code. I have 2 tables. Table A and Table B. Both are positioned one next to the other vertically. When "table A" increases it's rows, there comes a time when it starts to overlap "table B". Table B has fixed number of rows.

How can I avoid "table A" overlapping "table B", and have ALL the content below "table A" move dynamically 'downward' when "table A" increases?

Example:

const generatePDF = () => {

        const unit = "pt";
        const size = "A4"
        const orientation = "portrait";

        const doc = new jsPDF(orientation, unit, size);

        let contentA = {
            head: [["Item", "Quantity", "Price", "Item Total"]],
           
            body: [
                ['Water Bottle A', '1', '8.99', '8.99'],
                ['Water Bottle B', '1', '8.99', '8.99'],
                ['Water Bottle C', '1', '8.99', '8.99'],
                ['Water Bottle D', '1', '8.99', '8.99'],
               ],
            foot: [['', '', 'Total:', '35.96']],
            margin: 40,
            tableLineWidth: 0.5,
            tableLineColor: "black",
            tableWidth: 'auto',
            startY: 100,
            theme: 'striped',
            pageBreak: 'auto',
        }

        let contentB = {
            head: [["Subtotal", "$35.96"]],
           
            body: [
                ['Taxes', '$4.49'],
                ['Payment Method:', 'Cash'],
               ],
            foot: [['Total:', '35.96']],
            margin: 40,
            tableLineWidth: 0.5,
            tableLineColor: "black",
            tableWidth: 'auto',
            startY: 150,
            theme: 'striped',
            pageBreak: 'auto',
        }

        doc.autoTable(contentA)
        doc.autoTable(contentB)
        doc.text('Thank you for your purchase!', 150, 800);
        doc.save("my-report-document.pdf")
}

All rows here are static (in my app Im looping through a cart array of objects and gnerating the rows, but this example explains my problem), if I where to add 30 more rows to body property of "contentA", the table would overlap with "contentB" table.

The "startY" property is the only property right now establishing the starting 'Y' coordinate of each table. I dont mind if the content below "table A" breaks into a 2nd page, I just want to get rid of the vertical overlapping.

Any help will be appreciated.

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

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

发布评论

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

评论(2

歌枕肩 2025-02-06 08:05:53

您可以使用:

doc.lastAutoTable.finalY

这样:

doc.text('Thank you for your purchase!', 150, doc.lastAutoTable.finalY + 30);

you can used :

doc.lastAutoTable.finalY

like this :

doc.text('Thank you for your purchase!', 150, doc.lastAutoTable.finalY + 30);
独行侠 2025-02-06 08:05:53
let finalY = doc.lastAutoTable.finalY;
doc.text('Payments can be remitted to the Commerce City address', 10, doc.lastAutoTable.finalY + 10);
let finalY = doc.lastAutoTable.finalY;
doc.text('Payments can be remitted to the Commerce City address', 10, doc.lastAutoTable.finalY + 10);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文