Skip to content

Commit

Permalink
gridline improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
herman26 committed Nov 30, 2021
1 parent 12db7d1 commit b65468f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
12 changes: 10 additions & 2 deletions app/line-types/grid-line.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,18 @@ function generateLineThatIsGrid(doc, x, y, text, value, isNewPageHeader, headerC
const {fontSize, boldFont, lightFont} = utils.setComponentFont("OpenSansSemiBold", "OpenSansLight", constants.NORMAL_FONT_SIZE, font);

let index = 0;

const pageTotalSize = doc.page.width - (constants.PD.MARGIN * 2);
const leftOverSize = Object.keys(gridHeaders).reduce((acc, item) => {
return acc - gridHeaders[item].size;
}, pageTotalSize);

const amountToAddToColumns = leftOverSize / (Object.keys(gridHeaders).length - 1);

for (const gridHeader in gridHeaders) {
if (Object.prototype.hasOwnProperty.call(gridHeaders, gridHeader)) {
const header = gridHeaders[gridHeader];
const width = (header.size + 100);
x = index === 0 ? x : x + width; // TODO: the + 100 can differ for size ranges perhaps
const width = (header.size + amountToAddToColumns);
columnXStart[gridHeader] = x;
columnWidth[gridHeader] = width;
doc
Expand All @@ -53,6 +60,7 @@ function generateLineThatIsGrid(doc, x, y, text, value, isNewPageHeader, headerC
align: "left",
});
index++;
x = index === 0 ? x : x + width;
}
}
y += incrementY;
Expand Down
23 changes: 23 additions & 0 deletions app/pdf-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,29 @@ class PDFHelpers {
pdfType: constants.PDFType.DEFAULT,
};
}

setColumnNameAndWidth(columns, key, value, pdfLimiter) { // ensure length more than actual column name
const keyValue = this.getDisplayableTextValue(key);
const keyDocWidth = pdfLimiter.getStringWidth(keyValue);
// console.log(`key width: ${keyDocWidth}`)
let length = !value ? 0 : pdfLimiter.getStringWidth(value);
// console.log(`value ${value}, width: ${length}`)
length = length > keyDocWidth ? length : keyDocWidth;
if (Object.prototype.hasOwnProperty.call(columns, key)) {
// just do value size check and ensure value is the largest one available
const size = pdfLimiter.getStringWidth(columns[key]);
if (length > size) {
columns[key].size = length;
}
} else {
// value size check and name
columns[key] = {
name: this.getDisplayableTextValue(key),
size: length,
};
}
return columns;
}
}

module.exports.PDFHelpers = PDFHelpers;
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "@thisisme/pdfgeneration-lib",
"version": "6.5.0",
"version": "6.6.0",
"private": true,
"main": "app.js",
"dependencies": {
"aws-sdk": "2.978.0",
"aws-sdk": "2.1039.0",
"bwip-js": "3.0.4",
"cloc": "^2.8.0",
"concat-stream": "2.0.0",
Expand Down

0 comments on commit b65468f

Please sign in to comment.