pdfkit-table icon indicating copy to clipboard operation
pdfkit-table copied to clipboard

Problem with long text in cell spreading on several pages

Open MeMineToMe opened this issue 4 years ago • 12 comments

If a cell contains a long text making the table spreading on several pages, the layout is broken. The problem can be simply reproduced with the following code :

const fs = require("fs");
const PDFDocument = require("pdfkit-table");

let doc = new PDFDocument({ margin: 30, size: 'A4' });
doc.pipe(fs.createWriteStream("./document.pdf"));

const lorem = `Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.`;
let longtext = "";
for(let i = 0; i < 20; i++) {
  longtext += `${i + 1}. ${lorem}\n`;
}

const table = {
  headers: [ "Name", "Description" ],
  rows: [
    [ "Name 1", longtext ],
    [ 'Name 2', 'Lorem ipsum dolor.' ],
    [ 'Name 3', longtext ],
    [ 'Name 4', 'Lorem ipsum dolor.' ]
  ]
};

doc.table(table);
doc.end();

MeMineToMe avatar Jan 21 '22 15:01 MeMineToMe

Hi, @MeMineToMe, What version pdfkit-table?

natancabral avatar Jan 21 '22 15:01 natancabral

Hi Natan, I used version 0.1.77

MeMineToMe avatar Jan 21 '22 16:01 MeMineToMe

Try again. $ npm install pdfkit-table@latest

See example: document-8.pdf

natancabral avatar Jan 21 '22 19:01 natancabral

Check this: https://github.com/natancabral/pdfkit-table#0183

natancabral avatar Jan 21 '22 20:01 natancabral

Hi Natan, Thanks for your very quick answer and fix ! That's better but not perfect yet ;) I see 3 additional problems :

  1. When there is a line before one with a long text, an addPage event is wrongly emitted
  2. If you have custom style on rows with prepareRow callback, it's broken on 2nd page and the next ones
  3. If you are in landscape layout, the following pages switch back to portrait

Here is the updated code to reproduce :

`

const fs = require("fs");
const PDFDocument = require("pdfkit-table");

let doc = new PDFDocument({ margin: 30, size: 'A4' });
doc.pipe(fs.createWriteStream("./document.pdf"));

const lorem = `Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.`;
let longtext = "";
for(let i = 0; i < 20; i++) {
  longtext += `${i + 1}. ${lorem}\n`;
}

const table = {
  headers: [ "Name", "Description" ],
  rows: [
    [ "Name 0", 'Lorem ipsum dolor.' ],
    [ "Name 1", longtext ],
    [ 'Name 2', 'Lorem ipsum dolor.' ],
    [ 'Name 3', longtext ],
    [ 'Name 4', 'Lorem ipsum dolor.' ]
  ]
};

const tableOptions1 = {
  divider: {
    header: { disabled: true },
    horizontal: { disabled: true },
  },
  prepareRow: (row, indexColumn, indexRow, rectRow) => {
    doc.font("Helvetica").fontSize(8);
    indexColumn === 0 && doc.addBackground(rectRow, (indexRow % 2 ? 'blue' : 'green'), 0.15);
  }
};

const tableOptions2 = {
  prepareRow: (row, indexColumn, indexRow, rectRow) => {
    doc.font("Helvetica").fontSize(12);
  }
};

doc.table(table, tableOptions1);
doc.addPage({ margin: 100, layout: 'landscape' });
doc.table(table, tableOptions2);
doc.end();

`

MeMineToMe avatar Jan 24 '22 08:01 MeMineToMe

Natan,

I also noted a strange behaviour on my specific test cases with this new release, the pdf generation may crash with the following stack trace :

./node_modules/pdfkit/js/pdfkit.js:113
    static convert(object, encryptFn = null) {
                ^

RangeError: Maximum call stack size exceeded
    at Function.convert (./node_modules/pdfkit/js/pdfkit.js:113:17)
    at ./node_modules/pdfkit/js/pdfkit.js:163:47
    at Array.map (<anonymous>)
    at Function.convert (./node_modules/pdfkit/js/pdfkit.js:163:28)
    at Function.convert (./node_modules/pdfkit/js/pdfkit.js:170:39)
    at PDFReference.finalize (./node_modules/pdfkit/js/pdfkit.js:256:36)
    at PDFReference.end (./node_modules/pdfkit/js/pdfkit.js:233:17)
    at PDFPage.end (./node_modules/pdfkit/js/pdfkit.js:424:21)
    at PDFDocumentWithTables.flushPages (./node_modules/pdfkit/js/pdfkit.js:6236:12)
    at PDFDocumentWithTables.addPage (./node_modules/pdfkit/js/pdfkit.js:6180:12)

Do you think that could be related to your last fix ? I didn't find a simple use case to reproduce yet.

MeMineToMe avatar Jan 24 '22 14:01 MeMineToMe

humm Try last version, 0.1.84

$ npm install pdfkit-table@latest

natancabral avatar Jan 24 '22 14:01 natancabral

Just tried 0.1.84. The 3 problems listed previously are still here. Please see attached document : document.pdf

MeMineToMe avatar Jan 24 '22 15:01 MeMineToMe

Try a new package typescript (async)

$ npm i pdfkit-table-ts@latest

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

PDF Sample: example-2.pdf

natancabral avatar May 23 '22 20:05 natancabral

Try a new package typescript (async)

$ npm i pdfkit-table-ts@latest

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

PDF Sample: example-2.pdf

Eu estava com o mesmo bug, mas essa versão do typescript parece realmente ter corrigido, espero não estar perdendo alguma outra funcionalidade por estar usando ela.

Além disso, parece que essa versão do typescript só aceita tipo "String" nas linhas da tabela, quebrou quando usei números.

No geral, obrigado!

Bonfims avatar Jun 09 '23 00:06 Bonfims

Seeing this issue with latest version

everythingspirals avatar Mar 11 '24 06:03 everythingspirals

Try a new package typescript (async)

$ npm i pdfkit-table-ts@latest

Sample init code https://github.com/natancabral/pdfkit-table-ts PDF Sample: example-2.pdf

Eu estava com o mesmo bug, mas essa versão do typescript parece realmente ter corrigido, espero não estar perdendo alguma outra funcionalidade por estar usando ela.

Além disso, parece que essa versão do typescript só aceita tipo "String" nas linhas da tabela, quebrou quando usei números.

No geral, obrigado!

PT-BR: Na versão pdfkit-table-ts o header para de aparecer em novas páginas. ENG - pdfkit-table-ts version the header is only shown on the first page

HyperMorgado avatar Apr 18 '24 20:04 HyperMorgado