natancabral / pdfkit-table

Helps to draw informations in simple tables using pdfkit. #server-side. Generate pdf tables with javascript (PDFKIT plugin)
MIT License
93 stars 58 forks source link

Tables overwrite previous tables on the same page #38

Open Babbiorsetto opened 2 years ago

Babbiorsetto commented 2 years ago

I'm generating multiple tables on one page, one after the other. When reaching the end of the page, I expect the next table to move to a new page, instead what happens is it's placed on top of the same page, thereby overwriting the tables that were placed before. image

I have created a minimal reproducible example recreating this issue

example ```javascript const PDFDocument = require("pdfkit-table"); const path = require("path"); const fs = require("fs"); const doc = new PDFDocument({layout: "landscape"}); const tables = [ { title: "One", headers: ["Hello"], rows: [["World"]], }, { title: "Two", headers: ["Hello"], rows: [["World"]], }, { title: "Three", headers: ["Hello"], rows: [["World"]], }, { title: "Four", headers: ["Hello"], rows: [["World"]], }, { title: "Five", headers: ["Hello"], rows: [["World"]], }, { title: "Six", headers: ["Hello"], rows: [["World"]], }, { title: "Seven", headers: ["Hello"], rows: [["World"]], }, { title: "Eight", headers: ["Hello"], rows: [["World"]], }, { title: "Nine", headers: ["Hello"], rows: [["World"]], }, { title: "Ten", headers: ["Hello"], rows: [["World"]], }, ]; for (const table of tables) { doc.table(table); } doc.end() const file = fs.createWriteStream(path.join(process.cwd(), "out.pdf"), {flags: "w"}) doc.pipe(file) ```

For now I'm circumventing the issue with the equivalent of this code replaced in the previous snippet

for (const table of tables) {
    if (doc.y > 0.8 * doc.page.height) {
        doc.addPage()
    }
    doc.table(table);
}

Let me know if you find a better solution.

natancabral commented 2 years ago

I fixed this problem in the next version 0.2.x. But it's not over the full migration.

async functions is the solution,

natancabral commented 2 years ago

@Babbiorsetto Try this typescript example (another/testing package):

$ npm i pdfkit-table-ts

Sample here https://github.com/natancabral/pdfkit-table-ts

Babbiorsetto commented 2 years ago

By converting the example code to an async function, it does seem to work, although this happens at the page break image The title is split from content and a row is missing(!)

natancabral commented 2 years ago

Sorry, my bad. Try now:

$ npm i pdfkit-table-ts@latest
natancabral commented 2 years ago

Screenshot from 2022-05-23 15-21-09

Babbiorsetto commented 2 years ago

I'm glad to see you're sorting things out. I'll make sure to try it when it's ready for release. I think I'll stick with my solution for now since it works decently and I can't convert all my code right now.

natancabral commented 2 years ago

To fix this on new version:

        // calc if header + first line fit on last space page
        this.titleAndHeaderAndFirstLineHeightCalc = (
          this.titleHeight +
          this.subtitleHeight +
          this.headerHeight + // + header height 
          this.firstLineHeight + // + first line height
          (this.pdfkitTableCache.distanceCorrection * 2); // space between titles and lines
)
sepiaflux commented 2 years ago

Is there any news on this? Is using pdfkit-table-ts instead of pdfkit-table still the recommended solution? :)