I'm trying to create a long table with pdfkit.
But once the table gets too long for the first page of the pdf, it starts separating each table item by page.
There are 92 pages below this last page that says '23'. The pages below say one item of the table; '23', '$40', 'flowers' etc... Each is in the correct x position but on its own separate page.
How to I get the table to work correctly after the first page?
const fs = require('fs');
const PDFGenerator = require('pdfkit');
const dummyOrders = [
{ price: '$40', name: 'flowers', paidOn: '1/2/22' },
{ price: '$40', name: 'flowers', paidOn: '1/2/22' },
{ price: '$40', name: 'flowers', paidOn: '1/2/22' },
{ price: '$40', name: 'flowers', paidOn: '1/2/22' },
{ price: '$40', name: 'flowers', paidOn: '1/2/22' },
{ price: '$40', name: 'flowers', paidOn: '1/2/22' },
{ price: '$40', name: 'flowers', paidOn: '1/2/22' },
{ price: '$40', name: 'flowers', paidOn: '1/2/22' },
{ price: '$40', name: 'flowers', paidOn: '1/2/22' },
{ price: '$40', name: 'flowers', paidOn: '1/2/22' },
{ price: '$40', name: 'flowers', paidOn: '1/2/22' },
{ price: '$40', name: 'flowers', paidOn: '1/2/22' },
{ price: '$40', name: 'flowers', paidOn: '1/2/22' },
{ price: '$40', name: 'flowers', paidOn: '1/2/22' },
{ price: '$40', name: 'flowers', paidOn: '1/2/22' },
{ price: '$40', name: 'flowers', paidOn: '1/2/22' },
{ price: '$40', name: 'flowers', paidOn: '1/2/22' },
{ price: '$40', name: 'flowers', paidOn: '1/2/22' },
{ price: '$40', name: 'flowers', paidOn: '1/2/22' },
{ price: '$40', name: 'flowers', paidOn: '1/2/22' },
{ price: '$40', name: 'flowers', paidOn: '1/2/22' },
{ price: '$40', name: 'flowers', paidOn: '1/2/22' },
{ price: '$40', name: 'flowers', paidOn: '1/2/22' },
{ price: '$40', name: 'flowers', paidOn: '1/2/22' },
{ price: '$40', name: 'flowers', paidOn: '1/2/22' },
{ price: '$40', name: 'flowers', paidOn: '1/2/22' },
{ price: '$40', name: 'flowers', paidOn: '1/2/22' },
{ price: '$40', name: 'flowers', paidOn: '1/2/22' },
{ price: '$40', name: 'flowers', paidOn: '1/2/22' },
{ price: '$40', name: 'flowers', paidOn: '1/2/22' },
{ price: '$40', name: 'flowers', paidOn: '1/2/22' },
{ price: '$40', name: 'flowers', paidOn: '1/2/22' },
{ price: '$40', name: 'flowers', paidOn: '1/2/22' },
{ price: '$40', name: 'flowers', paidOn: '1/2/22' },
{ price: '$40', name: 'flowers', paidOn: '1/2/22' },
{ price: '$40', name: 'flowers', paidOn: '1/2/22' },
{ price: '$40', name: 'flowers', paidOn: '1/2/22' },
{ price: '$40', name: 'flowers', paidOn: '1/2/22' },
{ price: '$40', name: 'flowers', paidOn: '1/2/22' },
{ price: '$40', name: 'flowers', paidOn: '1/2/22' },
{ price: '$40', name: 'flowers', paidOn: '1/2/22' },
{ price: '$40', name: 'flowers', paidOn: '1/2/22' },
{ price: '$40', name: 'flowers', paidOn: '1/2/22' },
{ price: '$40', name: 'flowers', paidOn: '1/2/22' },
{ price: '$40', name: 'flowers', paidOn: '1/2/22' },
];
const generateTable = (doc, orders) => {
const tableTop = 150;
const descriptionX = 270;
const dateX = 400;
const amountX = 139;
const numX = 50;
doc
.fontSize(10)
.text('Num', numX, tableTop, { bold: true })
.text('Amount', amountX, tableTop, { bold: true })
.text('Description', descriptionX, tableTop)
.text('Date', dateX, tableTop);
let i = 0;
for (i = 0; i < orders.length; i++) {
const order = orders[i];
const y = tableTop + 25 + i * 25;
doc
.fontSize(10)
.text(i + 1, numX, y)
.text(order.price, amountX, y)
.text(order.name, descriptionX, y)
.text(order.paidOn, dateX, y);
}
};
const theOutput = new PDFGenerator();
const fileName = `Tax-info.pdf`;
// pipe to a writable stream which would save the result into the same directory
theOutput.pipe(fs.createWriteStream(fileName));
generateTable(theOutput, dummyOrders);
// write out file
theOutput.end();
