@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
package/src/Line.js
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
class Line {
|
|
2
|
+
/**
|
|
3
|
+
* @param {number} maxWidth Maximum width this line can have
|
|
4
|
+
*/
|
|
5
|
+
constructor(maxWidth) {
|
|
6
|
+
this.maxWidth = maxWidth;
|
|
7
|
+
this.leadingCut = 0;
|
|
8
|
+
this.trailingCut = 0;
|
|
9
|
+
this.inlineWidths = 0;
|
|
10
|
+
this.inlines = [];
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @param {object} inline
|
|
15
|
+
*/
|
|
16
|
+
addInline(inline) {
|
|
17
|
+
if (this.inlines.length === 0) {
|
|
18
|
+
this.leadingCut = inline.leadingCut || 0;
|
|
19
|
+
}
|
|
20
|
+
this.trailingCut = inline.trailingCut || 0;
|
|
21
|
+
|
|
22
|
+
inline.x = this.inlineWidths - this.leadingCut;
|
|
23
|
+
|
|
24
|
+
this.inlines.push(inline);
|
|
25
|
+
this.inlineWidths += inline.width;
|
|
26
|
+
|
|
27
|
+
if (inline.lineEnd) {
|
|
28
|
+
this.newLineForced = true;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @returns {number}
|
|
34
|
+
*/
|
|
35
|
+
getHeight() {
|
|
36
|
+
let max = 0;
|
|
37
|
+
|
|
38
|
+
this.inlines.forEach(item => {
|
|
39
|
+
max = Math.max(max, item.height || 0);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
return max;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* @returns {number}
|
|
47
|
+
*/
|
|
48
|
+
getAscenderHeight() {
|
|
49
|
+
let y = 0;
|
|
50
|
+
|
|
51
|
+
this.inlines.forEach(inline => {
|
|
52
|
+
y = Math.max(y, inline.font.ascender / 1000 * inline.fontSize);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
return y;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* @returns {number}
|
|
60
|
+
*/
|
|
61
|
+
getWidth() {
|
|
62
|
+
return this.inlineWidths - this.leadingCut - this.trailingCut;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* @returns {number}
|
|
67
|
+
*/
|
|
68
|
+
getAvailableWidth() {
|
|
69
|
+
return this.maxWidth - this.getWidth();
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* @param {object} inline
|
|
74
|
+
* @param {Array} nextInlines
|
|
75
|
+
* @returns {boolean}
|
|
76
|
+
*/
|
|
77
|
+
hasEnoughSpaceForInline(inline, nextInlines = []) {
|
|
78
|
+
if (this.inlines.length === 0) {
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
if (this.newLineForced) {
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
let inlineWidth = inline.width;
|
|
86
|
+
let inlineTrailingCut = inline.trailingCut || 0;
|
|
87
|
+
if (inline.noNewLine) {
|
|
88
|
+
for (let i = 0, l = nextInlines.length; i < l; i++) {
|
|
89
|
+
let nextInline = nextInlines[i];
|
|
90
|
+
inlineWidth += nextInline.width;
|
|
91
|
+
inlineTrailingCut += nextInline.trailingCut || 0;
|
|
92
|
+
if (!nextInline.noNewLine) {
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return (this.inlineWidths + inlineWidth - this.leadingCut - inlineTrailingCut) <= this.maxWidth;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
clone() {
|
|
102
|
+
let result = new Line(this.maxWidth);
|
|
103
|
+
|
|
104
|
+
for (let key in this) {
|
|
105
|
+
if (this.hasOwnProperty(key)) {
|
|
106
|
+
result[key] = this[key];
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return result;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export default Line;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
class OutputDocument {
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @param {Promise<object>} pdfDocumentPromise
|
|
5
|
+
*/
|
|
6
|
+
constructor(pdfDocumentPromise) {
|
|
7
|
+
this.pdfDocumentPromise = pdfDocumentPromise;
|
|
8
|
+
this.bufferPromise = null;
|
|
9
|
+
this.streamEnded = false;
|
|
10
|
+
this.streamEndListenerAttached = false;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @returns {Promise<object>}
|
|
15
|
+
*/
|
|
16
|
+
async getStream() {
|
|
17
|
+
const stream = await this.pdfDocumentPromise;
|
|
18
|
+
if (this.streamEnded) {
|
|
19
|
+
throw new Error('PDF stream has already been consumed.');
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (!this.streamEndListenerAttached) {
|
|
23
|
+
stream.once('end', () => {
|
|
24
|
+
this.streamEnded = true;
|
|
25
|
+
});
|
|
26
|
+
this.streamEndListenerAttached = true;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return stream;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @returns {Promise<Buffer>}
|
|
34
|
+
*/
|
|
35
|
+
getBuffer() {
|
|
36
|
+
const getBufferInternal = (async () => {
|
|
37
|
+
const stream = await this.getStream();
|
|
38
|
+
return new Promise((resolve, reject) => {
|
|
39
|
+
let chunks = [];
|
|
40
|
+
stream.on('data', chunk => {
|
|
41
|
+
chunks.push(chunk);
|
|
42
|
+
});
|
|
43
|
+
stream.on('end', () => {
|
|
44
|
+
resolve(Buffer.concat(chunks));
|
|
45
|
+
});
|
|
46
|
+
stream.on('error', reject);
|
|
47
|
+
stream.end();
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
if (this.bufferPromise === null) {
|
|
52
|
+
this.bufferPromise = getBufferInternal();
|
|
53
|
+
}
|
|
54
|
+
return this.bufferPromise;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* @returns {Promise<string>}
|
|
59
|
+
*/
|
|
60
|
+
async getBase64() {
|
|
61
|
+
const buffer = await this.getBuffer();
|
|
62
|
+
return buffer.toString('base64');
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* @returns {Promise<string>}
|
|
67
|
+
*/
|
|
68
|
+
async getDataUrl() {
|
|
69
|
+
const data = await this.getBase64();
|
|
70
|
+
return 'data:application/pdf;base64,' + data;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export default OutputDocument;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import OutputDocument from './OutputDocument';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
|
|
4
|
+
class OutputDocumentServer extends OutputDocument {
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @param {string} filename
|
|
8
|
+
* @returns {Promise}
|
|
9
|
+
*/
|
|
10
|
+
async write(filename) {
|
|
11
|
+
await this.pdfDocumentPromise;
|
|
12
|
+
if (this.bufferPromise !== null) {
|
|
13
|
+
await fs.promises.writeFile(filename, await this.getBuffer());
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const stream = await this.getStream();
|
|
18
|
+
const writeStream = fs.createWriteStream(filename);
|
|
19
|
+
|
|
20
|
+
const streamEnded = new Promise((resolve, reject) => {
|
|
21
|
+
stream.on('end', resolve);
|
|
22
|
+
stream.on('error', reject);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
const writeClosed = new Promise((resolve, reject) => {
|
|
26
|
+
writeStream.on('close', resolve);
|
|
27
|
+
writeStream.on('error', reject);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
stream.pipe(writeStream);
|
|
31
|
+
stream.end();
|
|
32
|
+
|
|
33
|
+
await Promise.all([streamEnded, writeClosed]);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export default OutputDocumentServer;
|
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
import { PDFDocument as PDFKitDocument } from 'pdfkit';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import { sha256 } from '@noble/hashes/sha2.js';
|
|
4
|
+
import { isString } from './helpers/variableType';
|
|
5
|
+
import { actualTextRuns, needsActualText } from './thai';
|
|
6
|
+
|
|
7
|
+
const typeName = (bold, italics) => {
|
|
8
|
+
let type = 'normal';
|
|
9
|
+
if (bold && italics) {
|
|
10
|
+
type = 'bolditalics';
|
|
11
|
+
} else if (bold) {
|
|
12
|
+
type = 'bold';
|
|
13
|
+
} else if (italics) {
|
|
14
|
+
type = 'italics';
|
|
15
|
+
}
|
|
16
|
+
return type;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
class PDFDocument extends PDFKitDocument {
|
|
20
|
+
constructor(fonts = {}, images = {}, patterns = {}, attachments = {}, options = {}, virtualfs = null, localAccessPolicy = undefined, downloadErrors = new Map()) {
|
|
21
|
+
super(options);
|
|
22
|
+
this.imageCache = new Map();
|
|
23
|
+
this.svgCache = new Map();
|
|
24
|
+
|
|
25
|
+
this.fonts = {};
|
|
26
|
+
this.fontCache = {};
|
|
27
|
+
for (let font in fonts) {
|
|
28
|
+
if (fonts.hasOwnProperty(font)) {
|
|
29
|
+
let fontDef = fonts[font];
|
|
30
|
+
|
|
31
|
+
this.fonts[font] = {
|
|
32
|
+
normal: fontDef.normal,
|
|
33
|
+
bold: fontDef.bold,
|
|
34
|
+
italics: fontDef.italics,
|
|
35
|
+
bolditalics: fontDef.bolditalics
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
this.patterns = {};
|
|
41
|
+
for (let pattern in patterns) {
|
|
42
|
+
if (patterns.hasOwnProperty(pattern)) {
|
|
43
|
+
let patternDef = patterns[pattern];
|
|
44
|
+
this.patterns[pattern] = this.pattern(patternDef.boundingBox, patternDef.xStep, patternDef.yStep, patternDef.pattern, patternDef.colored);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
this.images = images;
|
|
49
|
+
this.attachments = attachments;
|
|
50
|
+
this.virtualfs = virtualfs;
|
|
51
|
+
this.localAccessPolicy = localAccessPolicy;
|
|
52
|
+
this.downloadErrors = downloadErrors || new Map(); // w-pdfmaker: url => failed download of an images entry
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
getFontType(bold, italics) {
|
|
56
|
+
return typeName(bold, italics);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
getFontFile(familyName, bold, italics) {
|
|
60
|
+
let type = this.getFontType(bold, italics);
|
|
61
|
+
if (!this.fonts[familyName] || !this.fonts[familyName][type]) {
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return this.fonts[familyName][type];
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
provideFont(familyName, bold, italics) {
|
|
69
|
+
let type = this.getFontType(bold, italics);
|
|
70
|
+
if (this.getFontFile(familyName, bold, italics) === null) {
|
|
71
|
+
throw new Error(`Font '${familyName}' in style '${type}' is not defined in the font section of the document definition.`);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
this.fontCache[familyName] = this.fontCache[familyName] || {};
|
|
75
|
+
|
|
76
|
+
if (!this.fontCache[familyName][type]) {
|
|
77
|
+
let def = this.fonts[familyName][type];
|
|
78
|
+
if (!Array.isArray(def)) {
|
|
79
|
+
def = [def];
|
|
80
|
+
} else {
|
|
81
|
+
def = def.slice();
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (typeof def[0] === 'object' && def[0].url) {
|
|
85
|
+
def[0] = def[0].url;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (this.virtualfs && this.virtualfs.existsSync(def[0])) {
|
|
89
|
+
def[0] = this.virtualfs.readFileSync(def[0]);
|
|
90
|
+
} else {
|
|
91
|
+
this.validateLocalFile(def[0]);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
this.fontCache[familyName][type] = this.font(...def)._font;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return this.fontCache[familyName][type];
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
provideImage(src) {
|
|
101
|
+
const realImageSrc = src => {
|
|
102
|
+
let image = this.images[src] || src;
|
|
103
|
+
|
|
104
|
+
if (typeof image === 'object' && image.url) {
|
|
105
|
+
image = image.url;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (this.virtualfs && this.virtualfs.existsSync(image)) {
|
|
109
|
+
return this.virtualfs.readFileSync(image);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (typeof image !== 'string') {
|
|
113
|
+
return image;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
let index = image.indexOf('base64,');
|
|
117
|
+
if (index < 0) {
|
|
118
|
+
return image;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return Buffer.from(image.substring(index + 7), 'base64');
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
if (typeof src === 'string' && this._imageRegistry[src]) {
|
|
125
|
+
return this._imageRegistry[src];
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
let image;
|
|
129
|
+
let hash;
|
|
130
|
+
|
|
131
|
+
this.throwIfNoSource(src);
|
|
132
|
+
let imageSrc = realImageSrc(src);
|
|
133
|
+
|
|
134
|
+
this.throwDownloadError(imageSrc);
|
|
135
|
+
this.validateLocalFile(imageSrc);
|
|
136
|
+
|
|
137
|
+
try {
|
|
138
|
+
if (typeof imageSrc !== 'string') {
|
|
139
|
+
const data = imageSrc instanceof ArrayBuffer ? new Uint8Array(imageSrc) : imageSrc;
|
|
140
|
+
hash = Buffer.from(sha256(data)).toString('base64');
|
|
141
|
+
}
|
|
142
|
+
image = this.imageCache.get(hash) || this.openImage(imageSrc);
|
|
143
|
+
if (!image) {
|
|
144
|
+
throw new Error('No image');
|
|
145
|
+
}
|
|
146
|
+
} catch (error) {
|
|
147
|
+
throw new Error(`Invalid image: ${error.toString()}\nImages dictionary should contain dataURL entries (or local file paths in node.js)`, { cause: error });
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (!image.obj) {
|
|
151
|
+
image.embed(this);
|
|
152
|
+
}
|
|
153
|
+
if (hash) {
|
|
154
|
+
this.imageCache.set(hash, image);
|
|
155
|
+
}
|
|
156
|
+
if (typeof src === 'string') {
|
|
157
|
+
this._imageRegistry[src] = image;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return image;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* w-pdfmaker: the source of an image or SVG: its entry in the images dictionary, or the value itself.
|
|
165
|
+
*
|
|
166
|
+
* @param {*} src
|
|
167
|
+
* @returns {*}
|
|
168
|
+
*/
|
|
169
|
+
imageSource(src) {
|
|
170
|
+
const source = (isString(src) && this.images[src]) || src;
|
|
171
|
+
return source && typeof source === 'object' && source.url ? source.url : source;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* w-pdfmaker: whether an image is an SVG file (a path or URL ending with .svg).
|
|
176
|
+
*
|
|
177
|
+
* @param {*} src
|
|
178
|
+
* @returns {boolean}
|
|
179
|
+
*/
|
|
180
|
+
isSVGFile(src) {
|
|
181
|
+
const source = this.imageSource(src);
|
|
182
|
+
return isString(source) && !/^data:/i.test(source) && /\.svg(?:[?#]|$)/i.test(source);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* w-pdfmaker: a failed download of an images entry is thrown when the image is used.
|
|
187
|
+
*
|
|
188
|
+
* @param {*} src
|
|
189
|
+
*/
|
|
190
|
+
throwDownloadError(src) {
|
|
191
|
+
if (isString(src) && this.downloadErrors.has(src)) {
|
|
192
|
+
throw this.downloadErrors.get(src);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* w-pdfmaker: a name in the images dictionary with an empty value (e.g. no signature) is a missing image.
|
|
198
|
+
*
|
|
199
|
+
* @param {*} src
|
|
200
|
+
*/
|
|
201
|
+
throwIfNoSource(src) {
|
|
202
|
+
if (isString(src) && Object.prototype.hasOwnProperty.call(this.images, src) && !this.images[src]) {
|
|
203
|
+
throw Object.assign(new Error(`Image '${src}' has no source in the images dictionary`), { code: 'ENOENT' });
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* w-pdfmaker: SVG content. A string not starting with '<' is a file: a path, a name in the images dictionary,
|
|
209
|
+
* or a file in the virtual file system (downloads included).
|
|
210
|
+
*
|
|
211
|
+
* @param {string|object} src
|
|
212
|
+
* @returns {string|object}
|
|
213
|
+
*/
|
|
214
|
+
provideSVG(src) {
|
|
215
|
+
this.throwIfNoSource(src);
|
|
216
|
+
const svg = this.imageSource(src);
|
|
217
|
+
if (!isString(svg) || svg.trim().startsWith('<')) {
|
|
218
|
+
return svg;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
this.throwDownloadError(svg);
|
|
222
|
+
if (!this.svgCache.has(svg)) {
|
|
223
|
+
let data;
|
|
224
|
+
if (this.virtualfs && this.virtualfs.existsSync(svg)) {
|
|
225
|
+
data = this.virtualfs.readFileSync(svg);
|
|
226
|
+
} else {
|
|
227
|
+
this.validateLocalFile(svg);
|
|
228
|
+
data = fs.readFileSync(svg);
|
|
229
|
+
}
|
|
230
|
+
// downloads are stored as ArrayBuffer; TextDecoder also drops a BOM
|
|
231
|
+
this.svgCache.set(svg, new TextDecoder().decode(data));
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
return this.svgCache.get(svg);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* @param {Array} color pdfmake format: [<pattern name>, <color>]
|
|
239
|
+
* @returns {Array} pdfkit format: [<pattern object>, <color>]
|
|
240
|
+
*/
|
|
241
|
+
providePattern(color) {
|
|
242
|
+
if (Array.isArray(color) && color.length === 2) {
|
|
243
|
+
return [this.patterns[color[0]], color[1]];
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
return null;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
provideAttachment(src) {
|
|
250
|
+
const checkRequired = obj => {
|
|
251
|
+
if (!obj) {
|
|
252
|
+
throw new Error('No attachment');
|
|
253
|
+
}
|
|
254
|
+
if (!obj.src) {
|
|
255
|
+
throw new Error('The "src" key is required for attachments');
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
return obj;
|
|
259
|
+
};
|
|
260
|
+
|
|
261
|
+
let attachment = checkRequired(typeof src === 'object' ? src : this.attachments[src]);
|
|
262
|
+
|
|
263
|
+
if (typeof attachment.src === 'object' && attachment.src.url) {
|
|
264
|
+
attachment = { ...attachment, src: attachment.src.url };
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
if (this.virtualfs && this.virtualfs.existsSync(attachment.src)) {
|
|
268
|
+
return {
|
|
269
|
+
...attachment,
|
|
270
|
+
src: this.virtualfs.readFileSync(attachment.src)
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
this.validateLocalFile(attachment.src);
|
|
275
|
+
|
|
276
|
+
return attachment;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
text(text, ...args) {
|
|
280
|
+
if (!needsActualText(text)) {
|
|
281
|
+
return super.text(text, ...args);
|
|
282
|
+
}
|
|
283
|
+
const [x, y, options] = args;
|
|
284
|
+
if (options?.lineBreak !== false) { // wrapped text cannot be drawn part by part
|
|
285
|
+
this.markContent('Span', { actual: text });
|
|
286
|
+
super.text(text, ...args);
|
|
287
|
+
return this.endMarkedContent();
|
|
288
|
+
}
|
|
289
|
+
let partX = x;
|
|
290
|
+
actualTextRuns(text).forEach((run, i) => {
|
|
291
|
+
if (run.actual) {
|
|
292
|
+
this.markContent('Span', { actual: run.text });
|
|
293
|
+
}
|
|
294
|
+
super.text(run.text, partX, y, i === 0 ? options : { ...options, destination: undefined });
|
|
295
|
+
if (run.actual) {
|
|
296
|
+
this.endMarkedContent();
|
|
297
|
+
}
|
|
298
|
+
// pdfkit advances each glyph by its xAdvance plus characterSpacing (Tc)
|
|
299
|
+
const [, positions] = this._font.encode(run.text, options.features);
|
|
300
|
+
partX += positions.reduce((sum, pos) => sum + pos.xAdvance * this._fontSize / 1000 + (options.characterSpacing || 0), 0);
|
|
301
|
+
});
|
|
302
|
+
return this;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
resolveColor(color, defaultColor) {
|
|
306
|
+
color = color || defaultColor;
|
|
307
|
+
|
|
308
|
+
if (typeof this._normalizeColor === 'function') {
|
|
309
|
+
if (isString(color) && this._normalizeColor(color) === null) { // color is not valid
|
|
310
|
+
return defaultColor;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
return color;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
setOpenActionAsPrint() {
|
|
318
|
+
let printActionRef = this.ref({
|
|
319
|
+
Type: 'Action',
|
|
320
|
+
S: 'Named',
|
|
321
|
+
N: 'Print'
|
|
322
|
+
});
|
|
323
|
+
this._root.data.OpenAction = printActionRef;
|
|
324
|
+
printActionRef.end();
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
file(src, options = {}) {
|
|
328
|
+
this.validateLocalFile(src);
|
|
329
|
+
|
|
330
|
+
return super.file(src, options);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
validateLocalFile(path) {
|
|
334
|
+
if (typeof this.localAccessPolicy === 'undefined') {
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
if (!isString(path)) {
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
if (/^data:/.test(path)) {
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
if (this.localAccessPolicy(path) !== true) {
|
|
347
|
+
throw new Error(`Access to local file denied by resource access policy: ${path}`);
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
export default PDFDocument;
|