@wirabyte/w-pdfmaker 0.0.0-stage → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +53 -0
- package/LICENSE +22 -0
- package/NOTICE.md +39 -0
- package/README.md +106 -2
- package/build/fonts/Roboto/Roboto-Italic.ttf +0 -0
- package/build/fonts/Roboto/Roboto-Medium.ttf +0 -0
- package/build/fonts/Roboto/Roboto-MediumItalic.ttf +0 -0
- package/build/fonts/Roboto/Roboto-Regular.ttf +0 -0
- package/build/fonts/Roboto.js +27 -0
- package/build/pdfmake.js +103161 -0
- package/build/pdfmake.js.map +1 -0
- package/build/pdfmake.min.js +3 -0
- package/build/pdfmake.min.js.map +1 -0
- package/build/standard-fonts/Courier.js +9 -0
- package/build/standard-fonts/Helvetica.js +9 -0
- package/build/standard-fonts/Symbol.js +9 -0
- package/build/standard-fonts/Times.js +9 -0
- package/build/standard-fonts/ZapfDingbats.js +9 -0
- package/build/vfs_fonts.js +6 -0
- package/build-vfs.js +44 -0
- package/fonts/Roboto/Roboto-Italic.ttf +0 -0
- package/fonts/Roboto/Roboto-Medium.ttf +0 -0
- package/fonts/Roboto/Roboto-MediumItalic.ttf +0 -0
- package/fonts/Roboto/Roboto-Regular.ttf +0 -0
- package/fonts/Roboto.js +8 -0
- package/fonts/THSarabunIT9/LICENSE.txt +7 -0
- package/fonts/THSarabunIT9/THSarabunIT9-Bold.ttf +0 -0
- package/fonts/THSarabunIT9/THSarabunIT9-BoldItalic.ttf +0 -0
- package/fonts/THSarabunIT9/THSarabunIT9-Italic.ttf +0 -0
- package/fonts/THSarabunIT9/THSarabunIT9.ttf +0 -0
- package/fonts/THSarabunNew/COPYING.GPLv2 +339 -0
- package/fonts/THSarabunNew/LICENSE.txt +13 -0
- package/fonts/THSarabunNew/THSarabunNew-Bold.ttf +0 -0
- package/fonts/THSarabunNew/THSarabunNew-BoldItalic.ttf +0 -0
- package/fonts/THSarabunNew/THSarabunNew-Italic.ttf +0 -0
- package/fonts/THSarabunNew/THSarabunNew.ttf +0 -0
- package/index.d.ts +167 -0
- package/interfaces.d.ts +2779 -0
- package/js/DocMeasure.js +695 -0
- package/js/DocPreprocessor.js +263 -0
- package/js/DocumentContext.js +563 -0
- package/js/ElementWriter.js +488 -0
- package/js/LayoutBuilder.js +1525 -0
- package/js/Line.js +105 -0
- package/js/OutputDocument.js +73 -0
- package/js/OutputDocumentServer.js +34 -0
- package/js/PDFDocument.js +322 -0
- package/js/PageElementWriter.js +302 -0
- package/js/PageSize.js +74 -0
- package/js/Printer.js +275 -0
- package/js/Renderer.js +439 -0
- package/js/SVGMeasure.js +94 -0
- package/js/StyleContextStack.js +191 -0
- package/js/TableProcessor.js +658 -0
- package/js/TextBreaker.js +164 -0
- package/js/TextDecorator.js +164 -0
- package/js/TextInlines.js +212 -0
- package/js/URLResolver.js +122 -0
- package/js/base.js +125 -0
- package/js/browser-extensions/OutputDocumentBrowser.js +82 -0
- package/js/browser-extensions/fonts/Roboto.js +38 -0
- package/js/browser-extensions/fonts/thai.js +28 -0
- package/js/browser-extensions/index.js +88 -0
- package/js/browser-extensions/pageUrls.js +79 -0
- package/js/browser-extensions/pdfMake.js +3 -0
- package/js/browser-extensions/standard-fonts/Courier.js +20 -0
- package/js/browser-extensions/standard-fonts/Helvetica.js +20 -0
- package/js/browser-extensions/standard-fonts/Symbol.js +17 -0
- package/js/browser-extensions/standard-fonts/Times.js +20 -0
- package/js/browser-extensions/standard-fonts/ZapfDingbats.js +17 -0
- package/js/browser-extensions/virtual-fs-cjs.js +3 -0
- package/js/columnCalculator.js +159 -0
- package/js/helpers/node.js +122 -0
- package/js/helpers/tools.js +46 -0
- package/js/helpers/variableType.js +59 -0
- package/js/imageFallback.js +23 -0
- package/js/index.js +45 -0
- package/js/mergeStamp.js +194 -0
- package/js/pageNumbering.js +48 -0
- package/js/qrEnc.js +721 -0
- package/js/standardPageSizes.js +56 -0
- package/js/tableLayouts.js +98 -0
- package/js/thai.js +201 -0
- package/js/virtual-fs.js +82 -0
- package/package.json +93 -4
- package/skills/w-pdfmaker/SKILL.md +171 -0
- package/skills/w-pdfmaker/references/browser.md +36 -0
- package/skills/w-pdfmaker/references/document-definition.md +241 -0
- package/skills/w-pdfmaker/references/images.md +90 -0
- package/skills/w-pdfmaker/references/merge-stamp.md +94 -0
- package/skills/w-pdfmaker/references/thai.md +67 -0
- package/src/DocMeasure.js +780 -0
- package/src/DocPreprocessor.js +293 -0
- package/src/DocumentContext.js +605 -0
- package/src/ElementWriter.js +550 -0
- package/src/LayoutBuilder.js +1689 -0
- package/src/Line.js +114 -0
- package/src/OutputDocument.js +75 -0
- package/src/OutputDocumentServer.js +38 -0
- package/src/PDFDocument.js +352 -0
- package/src/PageElementWriter.js +343 -0
- package/src/PageSize.js +53 -0
- package/src/Printer.js +289 -0
- package/src/Renderer.js +468 -0
- package/src/SVGMeasure.js +109 -0
- package/src/StyleContextStack.js +208 -0
- package/src/TableProcessor.js +719 -0
- package/src/TextBreaker.js +166 -0
- package/src/TextDecorator.js +191 -0
- package/src/TextInlines.js +229 -0
- package/src/URLResolver.js +122 -0
- package/src/base.js +143 -0
- package/src/browser-extensions/OutputDocumentBrowser.js +80 -0
- package/src/browser-extensions/fonts/Roboto.js +27 -0
- package/src/browser-extensions/fonts/thai.js +23 -0
- package/src/browser-extensions/index.js +91 -0
- package/src/browser-extensions/pageUrls.js +67 -0
- package/src/browser-extensions/pdfMake.js +1 -0
- package/src/browser-extensions/standard-fonts/Courier.js +25 -0
- package/src/browser-extensions/standard-fonts/Helvetica.js +25 -0
- package/src/browser-extensions/standard-fonts/Symbol.js +17 -0
- package/src/browser-extensions/standard-fonts/Times.js +25 -0
- package/src/browser-extensions/standard-fonts/ZapfDingbats.js +17 -0
- package/src/browser-extensions/virtual-fs-cjs.js +1 -0
- package/src/columnCalculator.js +160 -0
- package/src/helpers/node.js +134 -0
- package/src/helpers/tools.js +44 -0
- package/src/helpers/variableType.js +50 -0
- package/src/imageFallback.js +18 -0
- package/src/index.js +45 -0
- package/src/mergeStamp.js +132 -0
- package/src/pageNumbering.js +45 -0
- package/src/qrEnc.js +796 -0
- package/src/standardPageSizes.js +52 -0
- package/src/tableLayouts.js +100 -0
- package/src/thai.js +170 -0
- package/src/virtual-fs.js +92 -0
- package/standard-fonts/Courier.js +8 -0
- package/standard-fonts/Helvetica.js +8 -0
- package/standard-fonts/Symbol.js +5 -0
- package/standard-fonts/Times.js +8 -0
- package/standard-fonts/ZapfDingbats.js +5 -0
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { isString } from './helpers/variableType';
|
|
2
|
+
|
|
3
|
+
function buildColumnWidths(columns, availableWidth, offsetTotal = 0, tableNode) {
|
|
4
|
+
let autoColumns = [];
|
|
5
|
+
let autoMin = 0;
|
|
6
|
+
let autoMax = 0;
|
|
7
|
+
let starColumns = [];
|
|
8
|
+
let starMaxMin = 0;
|
|
9
|
+
let starMaxMax = 0;
|
|
10
|
+
let fixedColumns = [];
|
|
11
|
+
let initial_availableWidth = availableWidth;
|
|
12
|
+
|
|
13
|
+
columns.forEach((column, colIndex) => {
|
|
14
|
+
if (isAutoColumn(column)) {
|
|
15
|
+
autoColumns.push(column);
|
|
16
|
+
autoMin += column._minWidth;
|
|
17
|
+
autoMax += column._maxWidth;
|
|
18
|
+
} else if (isStarColumn(column)) {
|
|
19
|
+
starColumns.push(column);
|
|
20
|
+
starMaxMin = Math.max(starMaxMin, column._minWidth);
|
|
21
|
+
starMaxMax = Math.max(starMaxMax, column._maxWidth);
|
|
22
|
+
} else {
|
|
23
|
+
fixedColumns.push({ column, colIndex });
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
fixedColumns.forEach(({ column: col, colIndex }) => {
|
|
28
|
+
let width = col.width;
|
|
29
|
+
// width specified as %
|
|
30
|
+
if (isPercentageColumn(col)) {
|
|
31
|
+
// In tables we have to take into consideration the reserved width for paddings and borders
|
|
32
|
+
let reservedWidth = 0;
|
|
33
|
+
if (tableNode) {
|
|
34
|
+
const paddingLeft = tableNode._layout.paddingLeft(colIndex, tableNode);
|
|
35
|
+
const paddingRight = tableNode._layout.paddingRight(colIndex, tableNode);
|
|
36
|
+
const borderLeft = tableNode._layout.vLineWidth(colIndex, tableNode);
|
|
37
|
+
const borderRight = tableNode._layout.vLineWidth(colIndex + 1, tableNode);
|
|
38
|
+
if (colIndex === 0) {
|
|
39
|
+
// first column assumes whole borderLeft and half of border right
|
|
40
|
+
reservedWidth = paddingLeft + paddingRight + borderLeft + (borderRight / 2);
|
|
41
|
+
|
|
42
|
+
} else if (colIndex === columns.length - 1) {
|
|
43
|
+
// last column assumes whole borderRight and half of border left
|
|
44
|
+
reservedWidth = paddingLeft + paddingRight + (borderLeft / 2) + borderRight;
|
|
45
|
+
|
|
46
|
+
} else {
|
|
47
|
+
// Columns in the middle assume half of each border
|
|
48
|
+
reservedWidth = paddingLeft + paddingRight + (borderLeft / 2) + (borderRight / 2);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
const totalAvailableWidth = initial_availableWidth + offsetTotal;
|
|
52
|
+
width = (parseFloat(col.width) * totalAvailableWidth / 100) - reservedWidth;
|
|
53
|
+
}
|
|
54
|
+
if (width < (col._minWidth) && col.elasticWidth) {
|
|
55
|
+
col._calcWidth = col._minWidth;
|
|
56
|
+
} else {
|
|
57
|
+
col._calcWidth = width;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
availableWidth -= col._calcWidth;
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
// http://www.freesoft.org/CIE/RFC/1942/18.htm
|
|
64
|
+
// http://www.w3.org/TR/CSS2/tables.html#width-layout
|
|
65
|
+
// http://dev.w3.org/csswg/css3-tables-algorithms/Overview.src.htm
|
|
66
|
+
let minW = autoMin + starMaxMin * starColumns.length;
|
|
67
|
+
let maxW = autoMax + starMaxMax * starColumns.length;
|
|
68
|
+
if (minW >= availableWidth) {
|
|
69
|
+
// case 1 - there's no way to fit all columns within available width
|
|
70
|
+
// that's actually pretty bad situation with PDF as we have no horizontal scroll
|
|
71
|
+
// no easy workaround (unless we decide, in the future, to split single words)
|
|
72
|
+
// currently we simply use minWidths for all columns
|
|
73
|
+
autoColumns.forEach(col => {
|
|
74
|
+
col._calcWidth = col._minWidth;
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
starColumns.forEach(col => {
|
|
78
|
+
col._calcWidth = starMaxMin; // starMaxMin already contains padding
|
|
79
|
+
});
|
|
80
|
+
} else {
|
|
81
|
+
if (maxW < availableWidth) {
|
|
82
|
+
// case 2 - we can fit rest of the table within available space
|
|
83
|
+
autoColumns.forEach(col => {
|
|
84
|
+
col._calcWidth = col._maxWidth;
|
|
85
|
+
availableWidth -= col._calcWidth;
|
|
86
|
+
});
|
|
87
|
+
} else {
|
|
88
|
+
// maxW is too large, but minW fits within available width
|
|
89
|
+
let W = availableWidth - minW;
|
|
90
|
+
let D = maxW - minW;
|
|
91
|
+
|
|
92
|
+
autoColumns.forEach(col => {
|
|
93
|
+
let d = col._maxWidth - col._minWidth;
|
|
94
|
+
col._calcWidth = col._minWidth + d * W / D;
|
|
95
|
+
availableWidth -= col._calcWidth;
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (starColumns.length > 0) {
|
|
100
|
+
let starSize = availableWidth / starColumns.length;
|
|
101
|
+
|
|
102
|
+
starColumns.forEach(col => {
|
|
103
|
+
col._calcWidth = starSize;
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function isAutoColumn(column) {
|
|
110
|
+
return column.width === 'auto';
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function isStarColumn(column) {
|
|
114
|
+
return column.width === null || column.width === undefined || column.width === '*' || column.width === 'star';
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function isPercentageColumn(column) {
|
|
118
|
+
return isString(column.width) && /\d+%/.test(column.width);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
//TODO: refactor and reuse in measureTable
|
|
122
|
+
function measureMinMax(columns) {
|
|
123
|
+
let result = { min: 0, max: 0 };
|
|
124
|
+
let maxStar = { min: 0, max: 0 };
|
|
125
|
+
let starCount = 0;
|
|
126
|
+
|
|
127
|
+
for (let i = 0, l = columns.length; i < l; i++) {
|
|
128
|
+
let c = columns[i];
|
|
129
|
+
|
|
130
|
+
if (isStarColumn(c)) {
|
|
131
|
+
maxStar.min = Math.max(maxStar.min, c._minWidth);
|
|
132
|
+
maxStar.max = Math.max(maxStar.max, c._maxWidth);
|
|
133
|
+
starCount++;
|
|
134
|
+
} else if (isAutoColumn(c) || isPercentageColumn(c)) {
|
|
135
|
+
result.min += c._minWidth;
|
|
136
|
+
result.max += c._maxWidth;
|
|
137
|
+
} else {
|
|
138
|
+
result.min += ((c.width !== undefined && c.width) || c._minWidth);
|
|
139
|
+
result.max += ((c.width !== undefined && c.width) || c._maxWidth);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (starCount) {
|
|
144
|
+
result.min += starCount * maxStar.min;
|
|
145
|
+
result.max += starCount * maxStar.max;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return result;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Calculates column widths
|
|
153
|
+
*/
|
|
154
|
+
export default {
|
|
155
|
+
buildColumnWidths: buildColumnWidths,
|
|
156
|
+
measureMinMax: measureMinMax,
|
|
157
|
+
isAutoColumn: isAutoColumn,
|
|
158
|
+
isStarColumn: isStarColumn,
|
|
159
|
+
isPercentageColumn: isPercentageColumn
|
|
160
|
+
};
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { isNumber, isString } from './variableType';
|
|
2
|
+
|
|
3
|
+
function fontStringify(key, val) {
|
|
4
|
+
if (key === 'font') {
|
|
5
|
+
return 'font';
|
|
6
|
+
}
|
|
7
|
+
return val;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Convert node to readable string
|
|
12
|
+
*
|
|
13
|
+
* @param {object} node
|
|
14
|
+
* @returns {string}
|
|
15
|
+
*/
|
|
16
|
+
export function stringifyNode(node) {
|
|
17
|
+
return JSON.stringify(node, fontStringify);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @param {object} node
|
|
22
|
+
* @returns {?string}
|
|
23
|
+
*/
|
|
24
|
+
export function getNodeId(node) {
|
|
25
|
+
if (node.id) {
|
|
26
|
+
return node.id;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (Array.isArray(node.text)) {
|
|
30
|
+
for (let n of node.text) {
|
|
31
|
+
let nodeId = getNodeId(n);
|
|
32
|
+
if (nodeId) {
|
|
33
|
+
return nodeId;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* @param {object} node
|
|
43
|
+
* @param {object} styleStack object is instance of PDFDocument
|
|
44
|
+
* @returns {?Array}
|
|
45
|
+
*/
|
|
46
|
+
export function getNodeMargin(node, styleStack) {
|
|
47
|
+
|
|
48
|
+
function processSingleMargins(node, currentMargin, defaultMargin = 0) {
|
|
49
|
+
if (node.marginLeft !== undefined || node.marginTop !== undefined || node.marginRight !== undefined || node.marginBottom !== undefined) {
|
|
50
|
+
return [
|
|
51
|
+
node.marginLeft ?? currentMargin[0] ?? defaultMargin,
|
|
52
|
+
node.marginTop ?? currentMargin[1] ?? defaultMargin,
|
|
53
|
+
node.marginRight ?? currentMargin[2] ?? defaultMargin,
|
|
54
|
+
node.marginBottom ?? currentMargin[3] ?? defaultMargin
|
|
55
|
+
];
|
|
56
|
+
}
|
|
57
|
+
return currentMargin;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function flattenStyleArray(styleArray, styleStack, visited = new Set()) {
|
|
61
|
+
styleArray = Array.isArray(styleArray) ? styleArray : [styleArray];
|
|
62
|
+
|
|
63
|
+
// style is not valid array of strings
|
|
64
|
+
if (!styleArray.every(item => isString(item))) {
|
|
65
|
+
return {};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
let flattenedStyles = {};
|
|
69
|
+
for (let index = 0; index < styleArray.length; index++) {
|
|
70
|
+
let styleName = styleArray[index];
|
|
71
|
+
let style = styleStack.styleDictionary[styleName];
|
|
72
|
+
|
|
73
|
+
// style not found
|
|
74
|
+
if (style === undefined) {
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (visited.has(styleName)) {
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (style.extends !== undefined) {
|
|
83
|
+
flattenedStyles = { ...flattenedStyles, ...flattenStyleArray(style.extends, styleStack, new Set([...visited, styleName])) };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (style.margin !== undefined) {
|
|
87
|
+
flattenedStyles = { margin: convertMargin(style.margin) };
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
flattenedStyles = { margin: processSingleMargins(style, flattenedStyles.margin ?? {}, undefined) };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return flattenedStyles;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function convertMargin(margin) {
|
|
98
|
+
if (isNumber(margin)) {
|
|
99
|
+
margin = [margin, margin, margin, margin];
|
|
100
|
+
} else if (Array.isArray(margin)) {
|
|
101
|
+
if (margin.length === 2) {
|
|
102
|
+
margin = [margin[0], margin[1], margin[0], margin[1]];
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return margin;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
let margin = [undefined, undefined, undefined, undefined];
|
|
109
|
+
|
|
110
|
+
if (node.style) {
|
|
111
|
+
let styleArray = Array.isArray(node.style) ? node.style : [node.style];
|
|
112
|
+
let flattenedStyleArray = flattenStyleArray(styleArray, styleStack);
|
|
113
|
+
|
|
114
|
+
if (flattenedStyleArray) {
|
|
115
|
+
margin = processSingleMargins(flattenedStyleArray, margin);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (flattenedStyleArray.margin) {
|
|
119
|
+
margin = convertMargin(flattenedStyleArray.margin);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
margin = processSingleMargins(node, margin);
|
|
124
|
+
|
|
125
|
+
if (node.margin !== undefined) {
|
|
126
|
+
margin = convertMargin(node.margin);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (margin[0] === undefined && margin[1] === undefined && margin[2] === undefined && margin[3] === undefined) {
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return margin;
|
|
134
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
export function pack(...args) {
|
|
2
|
+
let result = {};
|
|
3
|
+
|
|
4
|
+
for (let i = 0, l = args.length; i < l; i++) {
|
|
5
|
+
let obj = args[i];
|
|
6
|
+
|
|
7
|
+
if (obj) {
|
|
8
|
+
for (let key in obj) {
|
|
9
|
+
if (obj.hasOwnProperty(key)) {
|
|
10
|
+
result[key] = obj[key];
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return result;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function offsetVector(vector, x, y) {
|
|
20
|
+
switch (vector.type) {
|
|
21
|
+
case 'ellipse':
|
|
22
|
+
case 'rect':
|
|
23
|
+
vector.x += x;
|
|
24
|
+
vector.y += y;
|
|
25
|
+
break;
|
|
26
|
+
case 'line':
|
|
27
|
+
vector.x1 += x;
|
|
28
|
+
vector.x2 += x;
|
|
29
|
+
vector.y1 += y;
|
|
30
|
+
vector.y2 += y;
|
|
31
|
+
break;
|
|
32
|
+
case 'polyline':
|
|
33
|
+
for (let i = 0, l = vector.points.length; i < l; i++) {
|
|
34
|
+
vector.points[i].x += x;
|
|
35
|
+
vector.points[i].y += y;
|
|
36
|
+
}
|
|
37
|
+
break;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function convertToDynamicContent(staticContent) {
|
|
42
|
+
return () => // copy to new object
|
|
43
|
+
JSON.parse(JSON.stringify(staticContent));
|
|
44
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @param {any} variable
|
|
3
|
+
* @returns {boolean}
|
|
4
|
+
*/
|
|
5
|
+
export function isString(variable) {
|
|
6
|
+
return (typeof variable === 'string') || (variable instanceof String);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @param {any} variable
|
|
11
|
+
* @returns {boolean}
|
|
12
|
+
*/
|
|
13
|
+
export function isNumber(variable) {
|
|
14
|
+
return ((typeof variable === 'number') || (variable instanceof Number)) && !Number.isNaN(variable);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @param {any} variable
|
|
19
|
+
* @returns {boolean}
|
|
20
|
+
*/
|
|
21
|
+
export function isPositiveInteger(variable) {
|
|
22
|
+
if (!isNumber(variable) || !Number.isInteger(variable) || variable <= 0) {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @param {any} variable
|
|
30
|
+
* @returns {boolean}
|
|
31
|
+
*/
|
|
32
|
+
export function isObject(variable) {
|
|
33
|
+
return (variable !== null) && !Array.isArray(variable) && !isString(variable) && !isNumber(variable) && (typeof variable === 'object');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* @param {any} variable
|
|
38
|
+
* @returns {boolean}
|
|
39
|
+
*/
|
|
40
|
+
export function isEmptyObject(variable) {
|
|
41
|
+
return isObject(variable) && (Object.keys(variable).length === 0);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* @param {any} variable
|
|
46
|
+
* @returns {boolean}
|
|
47
|
+
*/
|
|
48
|
+
export function isValue(variable) {
|
|
49
|
+
return (variable !== undefined) && (variable !== null);
|
|
50
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// w-pdfmaker: `fallback: true` on an image or svg node draws nothing in its place (an empty SVG keeping the node's size)
|
|
2
|
+
// when its source is missing: no value, a missing file, or a failed download. Errors marked with code ENOENT
|
|
3
|
+
// (failed downloads are marked too) are "not found"; access policy denials and broken files are not.
|
|
4
|
+
|
|
5
|
+
export const EMPTY_SVG = '<svg xmlns="http://www.w3.org/2000/svg" width="1" height="1"></svg>';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @param {Error} error
|
|
9
|
+
* @returns {boolean}
|
|
10
|
+
*/
|
|
11
|
+
export const isNotFound = error => {
|
|
12
|
+
for (; error; error = error.cause) {
|
|
13
|
+
if (error.code === 'ENOENT') {
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return false;
|
|
18
|
+
};
|
package/src/index.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const pdfmakeBase = require('./base').default;
|
|
4
|
+
const OutputDocumentServer = require('./OutputDocumentServer').default;
|
|
5
|
+
const { thaiFonts } = require('./thai');
|
|
6
|
+
|
|
7
|
+
class pdfmake extends pdfmakeBase {
|
|
8
|
+
constructor() {
|
|
9
|
+
super();
|
|
10
|
+
// w-pdfmaker defaults: bundled Thai fonts, no server-side URL downloads (SSRF), local files allowed like 0.2
|
|
11
|
+
this.fonts = thaiFonts(path.join(__dirname, '../fonts/'));
|
|
12
|
+
this.urlAccessPolicy = () => false;
|
|
13
|
+
this.localAccessPolicy = () => true;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @param {(path: string) => boolean} callback
|
|
18
|
+
*/
|
|
19
|
+
setLocalAccessPolicy(callback) {
|
|
20
|
+
if (callback !== undefined && typeof callback !== 'function') {
|
|
21
|
+
throw new Error("Parameter 'callback' has an invalid type. Function or undefined expected.");
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
this.localAccessPolicy = callback;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* w-pdfmaker: reads a file passed by path to merge() / stamp().
|
|
29
|
+
*
|
|
30
|
+
* @param {string} path
|
|
31
|
+
* @returns {Promise<Buffer>}
|
|
32
|
+
*/
|
|
33
|
+
async _readFile(path) {
|
|
34
|
+
if (typeof this.localAccessPolicy !== 'undefined' && this.localAccessPolicy(path) !== true) {
|
|
35
|
+
throw new Error(`Access to local file denied by resource access policy: ${path}`);
|
|
36
|
+
}
|
|
37
|
+
return fs.promises.readFile(path);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
_transformToDocument(doc) {
|
|
41
|
+
return new OutputDocumentServer(doc);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
module.exports = new pdfmake();
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { PDFDocument, degrees } from '@cantoo/pdf-lib';
|
|
2
|
+
|
|
3
|
+
// w-pdfmaker: merging pages and stamping existing PDFs. Only Buffers (or file paths) go in and Buffers come out;
|
|
4
|
+
// no pdf-lib objects are exposed.
|
|
5
|
+
|
|
6
|
+
// A string is a file path, read by readFile (pdf-lib itself would take a string as base64).
|
|
7
|
+
// Password '' opens PDFs locked with an owner password only (they open without a password). The output is not encrypted.
|
|
8
|
+
const load = async (pdf, readFile) => PDFDocument.load(typeof pdf === 'string' ? await readFile(pdf) : pdf, { password: '' });
|
|
9
|
+
|
|
10
|
+
const save = async doc => Buffer.from(await doc.save());
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Joins pages of PDFs in the given order.
|
|
14
|
+
*
|
|
15
|
+
* @param {Array<Buffer|Uint8Array|string|{pdf: Buffer|Uint8Array|string, from?: number, to?: number}>} parts a whole PDF,
|
|
16
|
+
* or pages from..to (numbered from 1, inclusive; a range past the last page is cut at the last page)
|
|
17
|
+
* @param {Function} readFile path => Promise<Buffer|Uint8Array>
|
|
18
|
+
* @returns {Promise<Buffer>}
|
|
19
|
+
*/
|
|
20
|
+
export async function merge(parts, readFile) {
|
|
21
|
+
if (!Array.isArray(parts)) {
|
|
22
|
+
throw new Error("Parameter 'parts' has an invalid type. Array expected.");
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const sources = new Map();
|
|
26
|
+
const ranges = [];
|
|
27
|
+
for (const part of parts) {
|
|
28
|
+
const { pdf, from = 1, to = Infinity } = part && part.pdf ? part : { pdf: part };
|
|
29
|
+
if (!Number.isInteger(from) || from < 1 || !(to === Infinity || (Number.isInteger(to) && to >= 1))) {
|
|
30
|
+
throw new Error('Page numbers in merge() must be whole numbers starting at 1.');
|
|
31
|
+
}
|
|
32
|
+
if (!sources.has(pdf)) {
|
|
33
|
+
sources.set(pdf, { doc: await load(pdf, readFile), indices: [] });
|
|
34
|
+
}
|
|
35
|
+
const source = sources.get(pdf);
|
|
36
|
+
const length = Math.max(Math.min(to, source.doc.getPageCount()) - from + 1, 0);
|
|
37
|
+
source.indices.push(...Array.from({ length }, (_, i) => from - 1 + i));
|
|
38
|
+
ranges.push({ source, length });
|
|
39
|
+
}
|
|
40
|
+
if (!ranges.some(({ length }) => length)) {
|
|
41
|
+
// pdf-lib would save an empty document with a blank page
|
|
42
|
+
throw new Error('merge() selected no pages.');
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const out = await PDFDocument.create();
|
|
46
|
+
// One copyPages per source: fonts and images of a PDF split around other parts are copied once.
|
|
47
|
+
for (const source of sources.values()) {
|
|
48
|
+
source.pages = await out.copyPages(source.doc, source.indices);
|
|
49
|
+
}
|
|
50
|
+
for (const { source, length } of ranges) {
|
|
51
|
+
source.pages.splice(0, length).forEach(page => out.addPage(page));
|
|
52
|
+
}
|
|
53
|
+
return save(out);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// pdfmake writes layout state into content nodes, so a node used on several pages (e.g. one watermark object)
|
|
57
|
+
// renders only once. Content is copied per page; functions and binary data are kept as they are.
|
|
58
|
+
const copy = value => {
|
|
59
|
+
if (Array.isArray(value)) {
|
|
60
|
+
return value.map(copy);
|
|
61
|
+
}
|
|
62
|
+
if (value && Object.getPrototypeOf(value) === Object.prototype) {
|
|
63
|
+
return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, copy(item)]));
|
|
64
|
+
}
|
|
65
|
+
return value;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Draws pdfmake content over the pages of an existing PDF. pdfmake renders a transparent page per stamped page
|
|
70
|
+
* (same size, no margins), which is placed over the original page.
|
|
71
|
+
*
|
|
72
|
+
* @param {Buffer|Uint8Array|string} pdf
|
|
73
|
+
* @param {Function} fn ({ pageNumber, pageCount, width, height }) => pdfmake content, or null to leave the page as it is;
|
|
74
|
+
* width and height are the page as shown (rotated pages included), coordinates start at its top left corner
|
|
75
|
+
* @param {Function} render docDefinition => Promise<Buffer>
|
|
76
|
+
* @param {Function} readFile path => Promise<Buffer|Uint8Array>
|
|
77
|
+
* @returns {Promise<Buffer>}
|
|
78
|
+
*/
|
|
79
|
+
export async function stamp(pdf, fn, render, readFile) {
|
|
80
|
+
if (typeof fn !== 'function') {
|
|
81
|
+
throw new Error("Parameter 'fn' has an invalid type. Function expected.");
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
let doc = await load(pdf, readFile);
|
|
85
|
+
if (doc.context.isDecrypted) {
|
|
86
|
+
// A decrypted PDF keeps its old /Encrypt objects when saved, and pdf-lib (ours included) then refuses the
|
|
87
|
+
// unencrypted file as encrypted. Its pages are copied to a new document, like merge(); outlines and forms are lost.
|
|
88
|
+
const decrypted = doc;
|
|
89
|
+
doc = await PDFDocument.create();
|
|
90
|
+
(await doc.copyPages(decrypted, decrypted.getPageIndices())).forEach(page => doc.addPage(page));
|
|
91
|
+
}
|
|
92
|
+
const pages = doc.getPages();
|
|
93
|
+
const stamps = [];
|
|
94
|
+
for (const [i, page] of pages.entries()) {
|
|
95
|
+
const box = page.getCropBox(); // the visible area
|
|
96
|
+
let rotation = ((page.getRotation().angle % 360) + 360) % 360;
|
|
97
|
+
if (rotation % 90) {
|
|
98
|
+
rotation = 0; // like viewers, ignore an invalid /Rotate
|
|
99
|
+
}
|
|
100
|
+
const sideways = rotation === 90 || rotation === 270;
|
|
101
|
+
const width = sideways ? box.height : box.width;
|
|
102
|
+
const height = sideways ? box.width : box.height;
|
|
103
|
+
const content = await fn({ pageNumber: i + 1, pageCount: pages.length, width, height });
|
|
104
|
+
if (content !== null && content !== undefined) {
|
|
105
|
+
stamps.push({ page, box, rotation, width, height, content });
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (stamps.length) {
|
|
110
|
+
const overlay = await load(await render({
|
|
111
|
+
pageMargins: 0,
|
|
112
|
+
// One section (page) per stamp. The empty text gives every section an item, otherwise the section
|
|
113
|
+
// after a stamp that draws nothing would reuse its page.
|
|
114
|
+
content: stamps.map(({ width, height, content }) => ({
|
|
115
|
+
section: [{ text: '', absolutePosition: { x: 0, y: 0 } }, copy(content)],
|
|
116
|
+
pageSize: { width, height }
|
|
117
|
+
}))
|
|
118
|
+
}));
|
|
119
|
+
if (overlay.getPageCount() !== stamps.length) {
|
|
120
|
+
throw new Error('Content returned to stamp() must fit on its page.');
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const embedded = await doc.embedPdf(overlay, stamps.map((_, i) => i));
|
|
124
|
+
stamps.forEach(({ page, box: { x, y, width, height }, rotation }, i) => {
|
|
125
|
+
// The viewer turns the page clockwise by /Rotate, so the overlay is turned back counter-clockwise
|
|
126
|
+
// around the box corner that is shown at the bottom left.
|
|
127
|
+
const [originX, originY] = { 0: [x, y], 90: [x + width, y], 180: [x + width, y + height], 270: [x, y + height] }[rotation];
|
|
128
|
+
page.drawPage(embedded[i], { x: originX, y: originY, rotate: degrees(rotation) });
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
return save(doc);
|
|
132
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// w-pdfmaker: Word-like page numbering per section (section `pageNumbering: { start, format }`).
|
|
2
|
+
|
|
3
|
+
const THAI_LETTERS = 'กขคฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรลวศษสหฬอฮ'; // without obsolete ฃ ฅ, as in Thai theses
|
|
4
|
+
const ROMAN = [[1000, 'M'], [900, 'CM'], [500, 'D'], [400, 'CD'], [100, 'C'], [90, 'XC'], [50, 'L'], [40, 'XL'], [10, 'X'], [9, 'IX'], [5, 'V'], [4, 'IV'], [1, 'I']];
|
|
5
|
+
|
|
6
|
+
function toRoman(n) {
|
|
7
|
+
let result = '';
|
|
8
|
+
for (const [value, numeral] of ROMAN) {
|
|
9
|
+
for (; n >= value; n -= value) {
|
|
10
|
+
result += numeral;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
return result;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export const pageNumberFormats = {
|
|
17
|
+
'1': n => String(n),
|
|
18
|
+
'i': n => toRoman(n).toLowerCase(),
|
|
19
|
+
'I': toRoman,
|
|
20
|
+
'๑': n => String(n).replace(/\d/g, digit => '๐๑๒๓๔๕๖๗๘๙'[digit]),
|
|
21
|
+
// after ฮ, repeat the letter like Word does after z: กก, ขข, ...
|
|
22
|
+
'ก': n => (n > 0 ? THAI_LETTERS[(n - 1) % THAI_LETTERS.length].repeat(Math.ceil(n / THAI_LETTERS.length)) : '')
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Sets `pageLabel` on every page. A section's first page may carry `pageNumbering`:
|
|
27
|
+
* `start` restarts the count, `format` changes it; otherwise numbering continues from the previous page.
|
|
28
|
+
*/
|
|
29
|
+
export function labelPages(pages) {
|
|
30
|
+
let number = 0;
|
|
31
|
+
let format = pageNumberFormats['1'];
|
|
32
|
+
for (const page of pages) {
|
|
33
|
+
const numbering = page.pageNumbering;
|
|
34
|
+
if (numbering && numbering.format !== undefined) {
|
|
35
|
+
format = typeof numbering.format === 'function' ? numbering.format : pageNumberFormats[numbering.format];
|
|
36
|
+
if (!format) {
|
|
37
|
+
throw new Error(`Unknown page number format '${numbering.format}', use one of '${Object.keys(pageNumberFormats).join('\', \'')}' or a function`);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
if (numbering && numbering.start !== undefined) {
|
|
41
|
+
number = numbering.start - 1;
|
|
42
|
+
}
|
|
43
|
+
page.pageLabel = String(format(++number));
|
|
44
|
+
}
|
|
45
|
+
}
|