Problem with long text in cell spreading on several pages
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();
Hi, @MeMineToMe, What version pdfkit-table?
Hi Natan, I used version 0.1.77
Check this: https://github.com/natancabral/pdfkit-table#0183
Hi Natan, Thanks for your very quick answer and fix ! That's better but not perfect yet ;) I see 3 additional problems :
- When there is a line before one with a long text, an addPage event is wrongly emitted
- If you have custom style on rows with prepareRow callback, it's broken on 2nd page and the next ones
- 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();
`
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.
humm Try last version, 0.1.84
$ npm install pdfkit-table@latest
Just tried 0.1.84. The 3 problems listed previously are still here. Please see attached document : document.pdf
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
Try a new package typescript (async)
$ npm i pdfkit-table-ts@latestSample 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!
Seeing this issue with latest version
Try a new package typescript (async)
$ npm i pdfkit-table-ts@latestSample 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