Skip to content

Commit

Permalink
Small fix in creating new page
Browse files Browse the repository at this point in the history
  • Loading branch information
edisonneza committed Feb 11, 2021
1 parent bc86b5e commit 0cfc869
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ var props = {
header: ["#", "Description", "Price", "Quantity", "Unit", "Total"],
headerBorder: false,
tableBodyBorder: false,
table: Array.from(Array(10), (x,index)=>({
num: index,
table: Array.from(Array(10), (item, index)=>({
num: index + 1,
desc: "There are many variations ",
price: 200.5,
quantity: 4.5,
Expand Down
36 changes: 23 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@ const OutputType = {
DataUrlNewWindow: "dataurlnewwindow", //opens the data uri in new window
};

export {
OutputType,
jsPDF
}
export { OutputType, jsPDF };

/**
*
* @param { {
* outputType: OutputType | string
* fileName: string,
* orientationLandscape: boolean,
* logo: {
* src: string,
* width: number,
Expand Down Expand Up @@ -249,14 +247,12 @@ function jsPDFInvoiceTemplate(props) {
}
currentHeight -= 2;
};

var addTableBodyBoarder = (lineHeight) => {
for (let i = 0; i < param.invoice.header.length; i++) {
if (i === 0) doc.rect(10, currentHeight, tdWidth, lineHeight);
else doc.rect(tdWidth * i + 10, currentHeight, tdWidth, lineHeight);
}
};

var addTableHeader = () => {
if (param.invoice.headerBorder) addTableHeaderBoarder();

Expand Down Expand Up @@ -296,25 +292,35 @@ function jsPDFInvoiceTemplate(props) {
//td border height
currentHeight += 5;

//pre-increase currentHeight to check the height based on next row
if (index + 1 < tableBodyLength) currentHeight += itemDesc.height;

if (
(param.orientationLandscape && currentHeight > 178) ||
(currentHeight > 168 && doc.getNumberOfPages() > 1)
param.orientationLandscape &&
(currentHeight > 185 ||
(currentHeight > 178 && doc.getNumberOfPages() > 1))
) {
doc.addPage();
currentHeight = 10;
if (index + 1 < tableBodyLength) addTableHeader();
}

if (
(!param.orientationLandscape && currentHeight > 265) ||
(currentHeight > 255 && doc.getNumberOfPages() > 1)
!param.orientationLandscape &&
(currentHeight > 265 ||
(currentHeight > 255 && doc.getNumberOfPages() > 1))
) {
doc.addPage();
currentHeight = 10;
if (index + 1 < tableBodyLength) addTableHeader();
// else
// currentHeight += pdfConfig.subLineHeight + 2 + pdfConfig.subLineHeight - 1; //same as in addtableHeader
}

//reset the height that was increased to check the next row
if (index + 1 < tableBodyLength && currentHeight > 30)
// check if new page
currentHeight -= itemDesc.height;
});
// doc.line(10, currentHeight, docWidth - 10, currentHeight); //nese duam te shfaqim line ne fund te tabeles

Expand All @@ -327,7 +333,7 @@ function jsPDFInvoiceTemplate(props) {
currentHeight = 10;
}

if (!param.orientationLandscape && currentHeight + invDescSize > 260) {
if (!param.orientationLandscape && currentHeight + invDescSize > 270) {
doc.addPage();
currentHeight = 10;
}
Expand All @@ -350,7 +356,7 @@ function jsPDFInvoiceTemplate(props) {
// currentHeight += pdfConfig.subLineHeight;
doc.setFontSize(pdfConfig.labelTextSize);

// if (currentHeight > 250 && doc.getNumberOfPages() > 1) {
//add num of pages at the bottom
if (doc.getNumberOfPages() > 1) {
for (let i = 1; i <= doc.getNumberOfPages(); i++) {
doc.setFontSize(pdfConfig.fieldTextSize - 2);
Expand Down Expand Up @@ -397,6 +403,7 @@ function jsPDFInvoiceTemplate(props) {
};
addInvoiceDesc();

//add num of page at the bottom
if (doc.getNumberOfPages() === 1 && param.pageEnable) {
doc.setFontSize(pdfConfig.fieldTextSize - 2);
doc.setTextColor(colorGray);
Expand All @@ -409,7 +416,10 @@ function jsPDFInvoiceTemplate(props) {
}

if (param.outputType === "save") doc.save(param.fileName);
else doc.output(param.outputType, { filename: param.fileName });
else
doc.output(param.outputType, {
filename: param.fileName,
});

return {
pageNumber: doc.getNumberOfPages(),
Expand Down

0 comments on commit 0cfc869

Please sign in to comment.