Reputation: 33
I'm generating pdf with data from database. It works fine until there is too much data which do not fit on the first page. How do I create new page when the first one is full and continue adding data on new page?
Function to generate inner
function generateInnerPdf(doc, missions) {
generateHeader(doc, missions);
generateTableRow(
doc,
tableStartPos,
'Název mise',
'Uav',
'GPS',
'Doba letu',
'Teplota',
'Vítr',
'Start mise',
'Konec mise',
'Použité baterie',
'Popis'
);
generateHr(doc, tableStartPos + 45);
generateBody(doc, missions);
generateFooter(doc);
}
Function to generate body
function generateBody(doc, missions) {
for (i = 0; i < missions.length; i++) {
const item = missions[i];
let position = tableStartPos + (i + 1) * 60;
generateTableBody(
doc,
position,
item.missionName,
item.uav,
item.gps,
item.flightTime,
item.tmp,
item.wind,
moment(item.missionStart).format('lll'),
moment(item.missionEnd).format('lll'),
item.usedBatteries,
item.desc
);
}
}
Generate table body
function generateTableBody(doc, y, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10) {
doc
.fontSize(10)
.text(c1, 50, y)
.text(c2, 150, y)
.text(c3, 250, y)
.text(`${c4} min`, 480, y)
.moveDown()
.text(`${c5} °C`, 50, y + 15)
.text(`${c6} m/s`, 150, y + 15)
.text(c7, 250, y + 15)
.text(c8, 450, y + 15)
.text(c9, 50, y + 30)
.text(c10, 150, y + 30);
}
When there is too much data, it creates a lot of pages with random data all around. How to solve it. Thank you!
Output range it adds everytime 10 pages
EDIT: Tried this condition
if (position > 650) {
doc.addPage();
position = 130;
}
But it adds every new record on a new page
Upvotes: 0
Views: 6122
Reputation: 33
Solution
function generateBody(doc, missions) {
let index = 1;
for (i = 0; i < missions.length; i++) {
const item = missions[i];
let position = tableStartPos + index * 60;
index++;
if (position > 650) {
index = 0;
tableStartPos = 20;
doc.addPage();
}
generateTableBody(
doc,
position,
item.missionName,
item.uav,
item.gps,
item.flightTime,
item.tmp,
item.wind,
moment(item.missionStart).format('lll'),
moment(item.missionEnd).format('lll'),
item.usedBatteries,
item.desc
);
}
}
Upvotes: 1