pdfmake 0.3.0-beta.7 → 0.3.0-beta.8
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 +4 -0
- package/build/pdfmake.js +2654 -2525
- package/build/pdfmake.js.map +1 -1
- package/build/pdfmake.min.js +2 -2
- package/build/pdfmake.min.js.map +1 -1
- package/package.json +1 -1
- package/js/3rd-party/svg-to-pdfkit/source.js +0 -3626
- package/js/3rd-party/svg-to-pdfkit.js +0 -7
- package/js/DocMeasure.js +0 -627
- package/js/DocPreprocessor.js +0 -238
- package/js/DocumentContext.js +0 -265
- package/js/ElementWriter.js +0 -331
- package/js/LayoutBuilder.js +0 -694
- package/js/Line.js +0 -105
- package/js/OutputDocument.js +0 -76
- package/js/OutputDocumentServer.js +0 -27
- package/js/PDFDocument.js +0 -144
- package/js/PageElementWriter.js +0 -140
- package/js/PageSize.js +0 -74
- package/js/Printer.js +0 -291
- package/js/Renderer.js +0 -375
- package/js/SVGMeasure.js +0 -69
- package/js/StyleContextStack.js +0 -180
- package/js/TableProcessor.js +0 -511
- package/js/TextBreaker.js +0 -139
- package/js/TextDecorator.js +0 -143
- package/js/TextInlines.js +0 -206
- package/js/URLResolver.js +0 -73
- package/js/base.js +0 -52
- package/js/browser-extensions/OutputDocumentBrowser.js +0 -118
- package/js/browser-extensions/URLBrowserResolver.js +0 -76
- package/js/browser-extensions/fonts/Roboto.js +0 -38
- package/js/browser-extensions/index.js +0 -53
- package/js/browser-extensions/pdfMake.js +0 -15
- package/js/browser-extensions/standard-fonts/Courier.js +0 -38
- package/js/browser-extensions/standard-fonts/Helvetica.js +0 -38
- package/js/browser-extensions/standard-fonts/Symbol.js +0 -23
- package/js/browser-extensions/standard-fonts/Times.js +0 -38
- package/js/browser-extensions/standard-fonts/ZapfDingbats.js +0 -23
- package/js/browser-extensions/virtual-fs-cjs.js +0 -3
- package/js/columnCalculator.js +0 -129
- package/js/helpers/node.js +0 -98
- package/js/helpers/tools.js +0 -40
- package/js/helpers/variableType.js +0 -47
- package/js/index.js +0 -15
- package/js/qrEnc.js +0 -721
- package/js/standardPageSizes.js +0 -56
- package/js/tableLayouts.js +0 -98
- package/js/virtual-fs.js +0 -60
package/js/Printer.js
DELETED
|
@@ -1,291 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
exports.__esModule = true;
|
|
4
|
-
exports.default = void 0;
|
|
5
|
-
var _PDFDocument = _interopRequireDefault(require("./PDFDocument"));
|
|
6
|
-
var _LayoutBuilder = _interopRequireDefault(require("./LayoutBuilder"));
|
|
7
|
-
var _SVGMeasure = _interopRequireDefault(require("./SVGMeasure"));
|
|
8
|
-
var _PageSize = require("./PageSize");
|
|
9
|
-
var _tableLayouts = require("./tableLayouts");
|
|
10
|
-
var _Renderer = _interopRequireDefault(require("./Renderer"));
|
|
11
|
-
var _variableType = require("./helpers/variableType");
|
|
12
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
13
|
-
/**
|
|
14
|
-
* Printer which turns document definition into a pdf
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* var fontDescriptors = {
|
|
18
|
-
* Roboto: {
|
|
19
|
-
* normal: 'fonts/Roboto-Regular.ttf',
|
|
20
|
-
* bold: 'fonts/Roboto-Medium.ttf',
|
|
21
|
-
* italics: 'fonts/Roboto-Italic.ttf',
|
|
22
|
-
* bolditalics: 'fonts/Roboto-MediumItalic.ttf'
|
|
23
|
-
* }
|
|
24
|
-
* };
|
|
25
|
-
*
|
|
26
|
-
* var printer = new PdfPrinter(fontDescriptors);
|
|
27
|
-
*/
|
|
28
|
-
class PdfPrinter {
|
|
29
|
-
/**
|
|
30
|
-
* @param {object} fontDescriptors font definition dictionary
|
|
31
|
-
* @param {object} virtualfs
|
|
32
|
-
* @param {object} urlResolver
|
|
33
|
-
*/
|
|
34
|
-
constructor(fontDescriptors, virtualfs = null, urlResolver = null) {
|
|
35
|
-
this.fontDescriptors = fontDescriptors;
|
|
36
|
-
this.virtualfs = virtualfs;
|
|
37
|
-
this.urlResolver = urlResolver;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Executes layout engine for the specified document and renders it into a pdfkit document
|
|
42
|
-
* ready to be saved.
|
|
43
|
-
*
|
|
44
|
-
* @param {object} docDefinition
|
|
45
|
-
* @param {object} options
|
|
46
|
-
* @returns {Promise<PDFDocument>} resolved promise return a pdfkit document
|
|
47
|
-
*/
|
|
48
|
-
createPdfKitDocument(docDefinition, options = {}) {
|
|
49
|
-
return new Promise((resolve, reject) => {
|
|
50
|
-
this.resolveUrls(docDefinition).then(() => {
|
|
51
|
-
try {
|
|
52
|
-
docDefinition.version = docDefinition.version || '1.3';
|
|
53
|
-
docDefinition.compress = typeof docDefinition.compress === 'boolean' ? docDefinition.compress : true;
|
|
54
|
-
docDefinition.images = docDefinition.images || {};
|
|
55
|
-
docDefinition.attachments = docDefinition.attachments || {};
|
|
56
|
-
docDefinition.pageMargins = (0, _variableType.isValue)(docDefinition.pageMargins) ? docDefinition.pageMargins : 40;
|
|
57
|
-
docDefinition.patterns = docDefinition.patterns || {};
|
|
58
|
-
let pageSize = (0, _PageSize.normalizePageSize)(docDefinition.pageSize, docDefinition.pageOrientation);
|
|
59
|
-
let pdfOptions = {
|
|
60
|
-
size: [pageSize.width, pageSize.height],
|
|
61
|
-
pdfVersion: docDefinition.version,
|
|
62
|
-
compress: docDefinition.compress,
|
|
63
|
-
userPassword: docDefinition.userPassword,
|
|
64
|
-
ownerPassword: docDefinition.ownerPassword,
|
|
65
|
-
permissions: docDefinition.permissions,
|
|
66
|
-
lang: docDefinition.language,
|
|
67
|
-
fontLayoutCache: typeof options.fontLayoutCache === 'boolean' ? options.fontLayoutCache : true,
|
|
68
|
-
bufferPages: options.bufferPages || false,
|
|
69
|
-
autoFirstPage: false,
|
|
70
|
-
info: createMetadata(docDefinition),
|
|
71
|
-
font: null
|
|
72
|
-
};
|
|
73
|
-
this.pdfKitDoc = new _PDFDocument.default(this.fontDescriptors, docDefinition.images, docDefinition.patterns, docDefinition.attachments, pdfOptions, this.virtualfs);
|
|
74
|
-
embedFiles(docDefinition, this.pdfKitDoc);
|
|
75
|
-
const builder = new _LayoutBuilder.default(pageSize, (0, _PageSize.normalizePageMargin)(docDefinition.pageMargins), new _SVGMeasure.default());
|
|
76
|
-
builder.registerTableLayouts(_tableLayouts.tableLayouts);
|
|
77
|
-
if (options.tableLayouts) {
|
|
78
|
-
builder.registerTableLayouts(options.tableLayouts);
|
|
79
|
-
}
|
|
80
|
-
let pages = builder.layoutDocument(docDefinition.content, this.pdfKitDoc, docDefinition.styles || {}, docDefinition.defaultStyle || {
|
|
81
|
-
fontSize: 12,
|
|
82
|
-
font: 'Roboto'
|
|
83
|
-
}, docDefinition.background, docDefinition.header, docDefinition.footer, docDefinition.watermark, docDefinition.pageBreakBefore);
|
|
84
|
-
let maxNumberPages = docDefinition.maxPagesNumber || -1;
|
|
85
|
-
if ((0, _variableType.isNumber)(maxNumberPages) && maxNumberPages > -1) {
|
|
86
|
-
pages = pages.slice(0, maxNumberPages);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
// if pageSize.height is set to Infinity, calculate the actual height of the page that
|
|
90
|
-
// was laid out using the height of each of the items in the page.
|
|
91
|
-
if (pageSize.height === Infinity) {
|
|
92
|
-
let pageHeight = calculatePageHeight(pages, docDefinition.pageMargins);
|
|
93
|
-
this.pdfKitDoc.options.size = [pageSize.width, pageHeight];
|
|
94
|
-
}
|
|
95
|
-
const renderer = new _Renderer.default(this.pdfKitDoc, options.progressCallback);
|
|
96
|
-
renderer.renderPages(pages);
|
|
97
|
-
resolve(this.pdfKitDoc);
|
|
98
|
-
} catch (e) {
|
|
99
|
-
reject(e);
|
|
100
|
-
}
|
|
101
|
-
}, result => {
|
|
102
|
-
reject(result);
|
|
103
|
-
});
|
|
104
|
-
});
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* @param {object} docDefinition
|
|
109
|
-
* @returns {Promise}
|
|
110
|
-
*/
|
|
111
|
-
resolveUrls(docDefinition) {
|
|
112
|
-
const getExtendedUrl = url => {
|
|
113
|
-
if (typeof url === 'object') {
|
|
114
|
-
return {
|
|
115
|
-
url: url.url,
|
|
116
|
-
headers: url.headers
|
|
117
|
-
};
|
|
118
|
-
}
|
|
119
|
-
return {
|
|
120
|
-
url: url,
|
|
121
|
-
headers: {}
|
|
122
|
-
};
|
|
123
|
-
};
|
|
124
|
-
return new Promise((resolve, reject) => {
|
|
125
|
-
if (this.urlResolver === null) {
|
|
126
|
-
resolve();
|
|
127
|
-
}
|
|
128
|
-
for (let font in this.fontDescriptors) {
|
|
129
|
-
if (this.fontDescriptors.hasOwnProperty(font)) {
|
|
130
|
-
if (this.fontDescriptors[font].normal) {
|
|
131
|
-
if (Array.isArray(this.fontDescriptors[font].normal)) {
|
|
132
|
-
// TrueType Collection
|
|
133
|
-
let url = getExtendedUrl(this.fontDescriptors[font].normal[0]);
|
|
134
|
-
this.urlResolver.resolve(url.url, url.headers);
|
|
135
|
-
this.fontDescriptors[font].normal[0] = url.url;
|
|
136
|
-
} else {
|
|
137
|
-
let url = getExtendedUrl(this.fontDescriptors[font].normal);
|
|
138
|
-
this.urlResolver.resolve(url.url, url.headers);
|
|
139
|
-
this.fontDescriptors[font].normal = url.url;
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
if (this.fontDescriptors[font].bold) {
|
|
143
|
-
if (Array.isArray(this.fontDescriptors[font].bold)) {
|
|
144
|
-
// TrueType Collection
|
|
145
|
-
let url = getExtendedUrl(this.fontDescriptors[font].bold[0]);
|
|
146
|
-
this.urlResolver.resolve(url.url, url.headers);
|
|
147
|
-
this.fontDescriptors[font].bold[0] = url.url;
|
|
148
|
-
} else {
|
|
149
|
-
let url = getExtendedUrl(this.fontDescriptors[font].bold);
|
|
150
|
-
this.urlResolver.resolve(url.url, url.headers);
|
|
151
|
-
this.fontDescriptors[font].bold = url.url;
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
if (this.fontDescriptors[font].italics) {
|
|
155
|
-
if (Array.isArray(this.fontDescriptors[font].italics)) {
|
|
156
|
-
// TrueType Collection
|
|
157
|
-
let url = getExtendedUrl(this.fontDescriptors[font].italics[0]);
|
|
158
|
-
this.urlResolver.resolve(url.url, url.headers);
|
|
159
|
-
this.fontDescriptors[font].italics[0] = url.url;
|
|
160
|
-
} else {
|
|
161
|
-
let url = getExtendedUrl(this.fontDescriptors[font].italics);
|
|
162
|
-
this.urlResolver.resolve(url.url, url.headers);
|
|
163
|
-
this.fontDescriptors[font].italics = url.url;
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
if (this.fontDescriptors[font].bolditalics) {
|
|
167
|
-
if (Array.isArray(this.fontDescriptors[font].bolditalics)) {
|
|
168
|
-
// TrueType Collection
|
|
169
|
-
let url = getExtendedUrl(this.fontDescriptors[font].bolditalics[0]);
|
|
170
|
-
this.urlResolver.resolve(url.url, url.headers);
|
|
171
|
-
this.fontDescriptors[font].bolditalics[0] = url.url;
|
|
172
|
-
} else {
|
|
173
|
-
let url = getExtendedUrl(this.fontDescriptors[font].bolditalics);
|
|
174
|
-
this.urlResolver.resolve(url.url, url.headers);
|
|
175
|
-
this.fontDescriptors[font].bolditalics = url.url;
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
if (docDefinition.images) {
|
|
181
|
-
for (let image in docDefinition.images) {
|
|
182
|
-
if (docDefinition.images.hasOwnProperty(image)) {
|
|
183
|
-
let url = getExtendedUrl(docDefinition.images[image]);
|
|
184
|
-
this.urlResolver.resolve(url.url, url.headers);
|
|
185
|
-
docDefinition.images[image] = url.url;
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
if (docDefinition.attachments) {
|
|
190
|
-
for (let attachment in docDefinition.attachments) {
|
|
191
|
-
if (docDefinition.attachments.hasOwnProperty(attachment) && docDefinition.attachments[attachment].src) {
|
|
192
|
-
let url = getExtendedUrl(docDefinition.attachments[attachment].src);
|
|
193
|
-
this.urlResolver.resolve(url.url, url.headers);
|
|
194
|
-
docDefinition.attachments[attachment].src = url.url;
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
if (docDefinition.files) {
|
|
199
|
-
for (let file in docDefinition.files) {
|
|
200
|
-
if (docDefinition.files.hasOwnProperty(file) && docDefinition.files[file].src) {
|
|
201
|
-
let url = getExtendedUrl(docDefinition.files[file].src);
|
|
202
|
-
this.urlResolver.resolve(url.url, url.headers);
|
|
203
|
-
docDefinition.files[file].src = url.url;
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
this.urlResolver.resolved().then(() => {
|
|
208
|
-
resolve();
|
|
209
|
-
}, result => {
|
|
210
|
-
reject(result);
|
|
211
|
-
});
|
|
212
|
-
});
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
function createMetadata(docDefinition) {
|
|
216
|
-
// PDF standard has these properties reserved: Title, Author, Subject, Keywords,
|
|
217
|
-
// Creator, Producer, CreationDate, ModDate, Trapped.
|
|
218
|
-
// To keep the pdfmake api consistent, the info field are defined lowercase.
|
|
219
|
-
// Custom properties don't contain a space.
|
|
220
|
-
function standardizePropertyKey(key) {
|
|
221
|
-
let standardProperties = ['Title', 'Author', 'Subject', 'Keywords', 'Creator', 'Producer', 'CreationDate', 'ModDate', 'Trapped'];
|
|
222
|
-
let standardizedKey = key.charAt(0).toUpperCase() + key.slice(1);
|
|
223
|
-
if (standardProperties.includes(standardizedKey)) {
|
|
224
|
-
return standardizedKey;
|
|
225
|
-
}
|
|
226
|
-
return key.replace(/\s+/g, '');
|
|
227
|
-
}
|
|
228
|
-
let info = {
|
|
229
|
-
Producer: 'pdfmake',
|
|
230
|
-
Creator: 'pdfmake'
|
|
231
|
-
};
|
|
232
|
-
if (docDefinition.info) {
|
|
233
|
-
for (let key in docDefinition.info) {
|
|
234
|
-
let value = docDefinition.info[key];
|
|
235
|
-
if (value) {
|
|
236
|
-
key = standardizePropertyKey(key);
|
|
237
|
-
info[key] = value;
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
return info;
|
|
242
|
-
}
|
|
243
|
-
function embedFiles(docDefinition, pdfKitDoc) {
|
|
244
|
-
if (docDefinition.files) {
|
|
245
|
-
for (const key in docDefinition.files) {
|
|
246
|
-
const file = docDefinition.files[key];
|
|
247
|
-
if (!file.src) return;
|
|
248
|
-
if (pdfKitDoc.virtualfs && pdfKitDoc.virtualfs.existsSync(file.src)) {
|
|
249
|
-
file.src = pdfKitDoc.virtualfs.readFileSync(file.src);
|
|
250
|
-
}
|
|
251
|
-
file.name = file.name || key;
|
|
252
|
-
pdfKitDoc.file(file.src, file);
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
function calculatePageHeight(pages, margins) {
|
|
257
|
-
function getItemHeight(item) {
|
|
258
|
-
if (typeof item.item.getHeight === 'function') {
|
|
259
|
-
return item.item.getHeight();
|
|
260
|
-
} else if (item.item._height) {
|
|
261
|
-
return item.item._height;
|
|
262
|
-
} else if (item.type === 'vector') {
|
|
263
|
-
if (typeof item.item.y1 !== 'undefined') {
|
|
264
|
-
return item.item.y1 > item.item.y2 ? item.item.y1 : item.item.y2;
|
|
265
|
-
} else {
|
|
266
|
-
return item.item.h;
|
|
267
|
-
}
|
|
268
|
-
} else {
|
|
269
|
-
// TODO: add support for next item types
|
|
270
|
-
return 0;
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
function getBottomPosition(item) {
|
|
274
|
-
let top = item.item.y || 0;
|
|
275
|
-
let height = getItemHeight(item);
|
|
276
|
-
return top + height;
|
|
277
|
-
}
|
|
278
|
-
let fixedMargins = (0, _PageSize.normalizePageMargin)(margins || 40);
|
|
279
|
-
let height = fixedMargins.top;
|
|
280
|
-
pages.forEach(page => {
|
|
281
|
-
page.items.forEach(item => {
|
|
282
|
-
let bottomPosition = getBottomPosition(item);
|
|
283
|
-
if (bottomPosition > height) {
|
|
284
|
-
height = bottomPosition;
|
|
285
|
-
}
|
|
286
|
-
});
|
|
287
|
-
});
|
|
288
|
-
height += fixedMargins.bottom;
|
|
289
|
-
return height;
|
|
290
|
-
}
|
|
291
|
-
var _default = exports.default = PdfPrinter;
|
package/js/Renderer.js
DELETED
|
@@ -1,375 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
exports.__esModule = true;
|
|
4
|
-
exports.default = void 0;
|
|
5
|
-
var _TextDecorator = _interopRequireDefault(require("./TextDecorator"));
|
|
6
|
-
var _TextInlines = _interopRequireDefault(require("./TextInlines"));
|
|
7
|
-
var _variableType = require("./helpers/variableType");
|
|
8
|
-
var _svgToPdfkit = _interopRequireDefault(require("./3rd-party/svg-to-pdfkit"));
|
|
9
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
10
|
-
const findFont = (fonts, requiredFonts, defaultFont) => {
|
|
11
|
-
for (let i = 0; i < requiredFonts.length; i++) {
|
|
12
|
-
let requiredFont = requiredFonts[i].toLowerCase();
|
|
13
|
-
for (let font in fonts) {
|
|
14
|
-
if (font.toLowerCase() === requiredFont) {
|
|
15
|
-
return font;
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
return defaultFont;
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Shift the "y" height of the text baseline up or down (superscript or subscript,
|
|
24
|
-
* respectively). The exact shift can / should be changed according to standard
|
|
25
|
-
* conventions.
|
|
26
|
-
*
|
|
27
|
-
* @param {number} y
|
|
28
|
-
* @param {object} inline
|
|
29
|
-
* @returns {number}
|
|
30
|
-
*/
|
|
31
|
-
const offsetText = (y, inline) => {
|
|
32
|
-
var newY = y;
|
|
33
|
-
if (inline.sup) {
|
|
34
|
-
newY -= inline.fontSize * 0.75;
|
|
35
|
-
}
|
|
36
|
-
if (inline.sub) {
|
|
37
|
-
newY += inline.fontSize * 0.35;
|
|
38
|
-
}
|
|
39
|
-
return newY;
|
|
40
|
-
};
|
|
41
|
-
class Renderer {
|
|
42
|
-
constructor(pdfDocument, progressCallback) {
|
|
43
|
-
this.pdfDocument = pdfDocument;
|
|
44
|
-
this.progressCallback = progressCallback;
|
|
45
|
-
}
|
|
46
|
-
renderPages(pages) {
|
|
47
|
-
this.pdfDocument._pdfMakePages = pages; // TODO: Why?
|
|
48
|
-
this.pdfDocument.addPage();
|
|
49
|
-
let totalItems = 0;
|
|
50
|
-
if (this.progressCallback) {
|
|
51
|
-
pages.forEach(page => {
|
|
52
|
-
totalItems += page.items.length;
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
let renderedItems = 0;
|
|
56
|
-
for (let i = 0; i < pages.length; i++) {
|
|
57
|
-
if (i > 0) {
|
|
58
|
-
this._updatePageOrientationInOptions(pages[i]);
|
|
59
|
-
this.pdfDocument.addPage(this.pdfDocument.options);
|
|
60
|
-
}
|
|
61
|
-
let page = pages[i];
|
|
62
|
-
for (let ii = 0, il = page.items.length; ii < il; ii++) {
|
|
63
|
-
let item = page.items[ii];
|
|
64
|
-
switch (item.type) {
|
|
65
|
-
case 'vector':
|
|
66
|
-
this.renderVector(item.item);
|
|
67
|
-
break;
|
|
68
|
-
case 'line':
|
|
69
|
-
this.renderLine(item.item, item.item.x, item.item.y);
|
|
70
|
-
break;
|
|
71
|
-
case 'image':
|
|
72
|
-
this.renderImage(item.item);
|
|
73
|
-
break;
|
|
74
|
-
case 'svg':
|
|
75
|
-
this.renderSVG(item.item);
|
|
76
|
-
break;
|
|
77
|
-
case 'attachment':
|
|
78
|
-
this.renderAttachment(item.item);
|
|
79
|
-
break;
|
|
80
|
-
case 'beginClip':
|
|
81
|
-
this.beginClip(item.item);
|
|
82
|
-
break;
|
|
83
|
-
case 'endClip':
|
|
84
|
-
this.endClip();
|
|
85
|
-
break;
|
|
86
|
-
}
|
|
87
|
-
renderedItems++;
|
|
88
|
-
if (this.progressCallback) {
|
|
89
|
-
this.progressCallback(renderedItems / totalItems);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
if (page.watermark) {
|
|
93
|
-
this.renderWatermark(page);
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
renderLine(line, x, y) {
|
|
98
|
-
function preparePageNodeRefLine(_pageNodeRef, inline) {
|
|
99
|
-
let newWidth;
|
|
100
|
-
let diffWidth;
|
|
101
|
-
let textInlines = new _TextInlines.default(null);
|
|
102
|
-
if (_pageNodeRef.positions === undefined) {
|
|
103
|
-
throw new Error('Page reference id not found');
|
|
104
|
-
}
|
|
105
|
-
let pageNumber = _pageNodeRef.positions[0].pageNumber.toString();
|
|
106
|
-
inline.text = pageNumber;
|
|
107
|
-
newWidth = textInlines.widthOfText(inline.text, inline);
|
|
108
|
-
diffWidth = inline.width - newWidth;
|
|
109
|
-
inline.width = newWidth;
|
|
110
|
-
switch (inline.alignment) {
|
|
111
|
-
case 'right':
|
|
112
|
-
inline.x += diffWidth;
|
|
113
|
-
break;
|
|
114
|
-
case 'center':
|
|
115
|
-
inline.x += diffWidth / 2;
|
|
116
|
-
break;
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
if (line._pageNodeRef) {
|
|
120
|
-
preparePageNodeRefLine(line._pageNodeRef, line.inlines[0]);
|
|
121
|
-
}
|
|
122
|
-
x = x || 0;
|
|
123
|
-
y = y || 0;
|
|
124
|
-
let lineHeight = line.getHeight();
|
|
125
|
-
let ascenderHeight = line.getAscenderHeight();
|
|
126
|
-
let descent = lineHeight - ascenderHeight;
|
|
127
|
-
const textDecorator = new _TextDecorator.default(this.pdfDocument);
|
|
128
|
-
textDecorator.drawBackground(line, x, y);
|
|
129
|
-
|
|
130
|
-
//TODO: line.optimizeInlines();
|
|
131
|
-
//TOOD: lines without differently styled inlines should be written to pdf as one stream
|
|
132
|
-
for (let i = 0, l = line.inlines.length; i < l; i++) {
|
|
133
|
-
let inline = line.inlines[i];
|
|
134
|
-
let shiftToBaseline = lineHeight - inline.font.ascender / 1000 * inline.fontSize - descent;
|
|
135
|
-
if (inline._pageNodeRef) {
|
|
136
|
-
preparePageNodeRefLine(inline._pageNodeRef, inline);
|
|
137
|
-
}
|
|
138
|
-
let options = {
|
|
139
|
-
lineBreak: false,
|
|
140
|
-
textWidth: inline.width,
|
|
141
|
-
characterSpacing: inline.characterSpacing,
|
|
142
|
-
wordCount: 1,
|
|
143
|
-
link: inline.link
|
|
144
|
-
};
|
|
145
|
-
if (inline.linkToDestination) {
|
|
146
|
-
options.goTo = inline.linkToDestination;
|
|
147
|
-
}
|
|
148
|
-
if (line.id && i === 0) {
|
|
149
|
-
options.destination = line.id;
|
|
150
|
-
}
|
|
151
|
-
if (inline.fontFeatures) {
|
|
152
|
-
options.features = inline.fontFeatures;
|
|
153
|
-
}
|
|
154
|
-
let opacity = (0, _variableType.isNumber)(inline.opacity) ? inline.opacity : 1;
|
|
155
|
-
this.pdfDocument.opacity(opacity);
|
|
156
|
-
this.pdfDocument.fill(inline.color || 'black');
|
|
157
|
-
this.pdfDocument._font = inline.font;
|
|
158
|
-
this.pdfDocument.fontSize(inline.fontSize);
|
|
159
|
-
let shiftedY = offsetText(y + shiftToBaseline, inline);
|
|
160
|
-
this.pdfDocument.text(inline.text, x + inline.x, shiftedY, options);
|
|
161
|
-
if (inline.linkToPage) {
|
|
162
|
-
this.pdfDocument.ref({
|
|
163
|
-
Type: 'Action',
|
|
164
|
-
S: 'GoTo',
|
|
165
|
-
D: [inline.linkToPage, 0, 0]
|
|
166
|
-
}).end();
|
|
167
|
-
this.pdfDocument.annotate(x + inline.x, shiftedY, inline.width, inline.height, {
|
|
168
|
-
Subtype: 'Link',
|
|
169
|
-
Dest: [inline.linkToPage - 1, 'XYZ', null, null, null]
|
|
170
|
-
});
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
// Decorations won't draw correctly for superscript
|
|
175
|
-
textDecorator.drawDecorations(line, x, y);
|
|
176
|
-
}
|
|
177
|
-
renderVector(vector) {
|
|
178
|
-
//TODO: pdf optimization (there's no need to write all properties everytime)
|
|
179
|
-
this.pdfDocument.lineWidth(vector.lineWidth || 1);
|
|
180
|
-
if (vector.dash) {
|
|
181
|
-
this.pdfDocument.dash(vector.dash.length, {
|
|
182
|
-
space: vector.dash.space || vector.dash.length,
|
|
183
|
-
phase: vector.dash.phase || 0
|
|
184
|
-
});
|
|
185
|
-
} else {
|
|
186
|
-
this.pdfDocument.undash();
|
|
187
|
-
}
|
|
188
|
-
this.pdfDocument.lineJoin(vector.lineJoin || 'miter');
|
|
189
|
-
this.pdfDocument.lineCap(vector.lineCap || 'butt');
|
|
190
|
-
|
|
191
|
-
//TODO: clipping
|
|
192
|
-
|
|
193
|
-
let gradient = null;
|
|
194
|
-
switch (vector.type) {
|
|
195
|
-
case 'ellipse':
|
|
196
|
-
this.pdfDocument.ellipse(vector.x, vector.y, vector.r1, vector.r2);
|
|
197
|
-
if (vector.linearGradient) {
|
|
198
|
-
gradient = this.pdfDocument.linearGradient(vector.x - vector.r1, vector.y, vector.x + vector.r1, vector.y);
|
|
199
|
-
}
|
|
200
|
-
break;
|
|
201
|
-
case 'rect':
|
|
202
|
-
if (vector.r) {
|
|
203
|
-
this.pdfDocument.roundedRect(vector.x, vector.y, vector.w, vector.h, vector.r);
|
|
204
|
-
} else {
|
|
205
|
-
this.pdfDocument.rect(vector.x, vector.y, vector.w, vector.h);
|
|
206
|
-
}
|
|
207
|
-
if (vector.linearGradient) {
|
|
208
|
-
gradient = this.pdfDocument.linearGradient(vector.x, vector.y, vector.x + vector.w, vector.y);
|
|
209
|
-
}
|
|
210
|
-
break;
|
|
211
|
-
case 'line':
|
|
212
|
-
this.pdfDocument.moveTo(vector.x1, vector.y1);
|
|
213
|
-
this.pdfDocument.lineTo(vector.x2, vector.y2);
|
|
214
|
-
break;
|
|
215
|
-
case 'polyline':
|
|
216
|
-
if (vector.points.length === 0) {
|
|
217
|
-
break;
|
|
218
|
-
}
|
|
219
|
-
this.pdfDocument.moveTo(vector.points[0].x, vector.points[0].y);
|
|
220
|
-
for (let i = 1, l = vector.points.length; i < l; i++) {
|
|
221
|
-
this.pdfDocument.lineTo(vector.points[i].x, vector.points[i].y);
|
|
222
|
-
}
|
|
223
|
-
if (vector.points.length > 1) {
|
|
224
|
-
let p1 = vector.points[0];
|
|
225
|
-
let pn = vector.points[vector.points.length - 1];
|
|
226
|
-
if (vector.closePath || p1.x === pn.x && p1.y === pn.y) {
|
|
227
|
-
this.pdfDocument.closePath();
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
break;
|
|
231
|
-
case 'path':
|
|
232
|
-
this.pdfDocument.path(vector.d);
|
|
233
|
-
break;
|
|
234
|
-
}
|
|
235
|
-
if (vector.linearGradient && gradient) {
|
|
236
|
-
let step = 1 / (vector.linearGradient.length - 1);
|
|
237
|
-
for (let i = 0; i < vector.linearGradient.length; i++) {
|
|
238
|
-
gradient.stop(i * step, vector.linearGradient[i]);
|
|
239
|
-
}
|
|
240
|
-
vector.color = gradient;
|
|
241
|
-
}
|
|
242
|
-
let patternColor = this.pdfDocument.providePattern(vector.color);
|
|
243
|
-
if (patternColor !== null) {
|
|
244
|
-
vector.color = patternColor;
|
|
245
|
-
}
|
|
246
|
-
let fillOpacity = (0, _variableType.isNumber)(vector.fillOpacity) ? vector.fillOpacity : 1;
|
|
247
|
-
let strokeOpacity = (0, _variableType.isNumber)(vector.strokeOpacity) ? vector.strokeOpacity : 1;
|
|
248
|
-
if (vector.color && vector.lineColor) {
|
|
249
|
-
this.pdfDocument.fillColor(vector.color, fillOpacity);
|
|
250
|
-
this.pdfDocument.strokeColor(vector.lineColor, strokeOpacity);
|
|
251
|
-
this.pdfDocument.fillAndStroke();
|
|
252
|
-
} else if (vector.color) {
|
|
253
|
-
this.pdfDocument.fillColor(vector.color, fillOpacity);
|
|
254
|
-
this.pdfDocument.fill();
|
|
255
|
-
} else {
|
|
256
|
-
this.pdfDocument.strokeColor(vector.lineColor || 'black', strokeOpacity);
|
|
257
|
-
this.pdfDocument.stroke();
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
renderImage(image) {
|
|
261
|
-
let opacity = (0, _variableType.isNumber)(image.opacity) ? image.opacity : 1;
|
|
262
|
-
this.pdfDocument.opacity(opacity);
|
|
263
|
-
if (image.cover) {
|
|
264
|
-
const align = image.cover.align || 'center';
|
|
265
|
-
const valign = image.cover.valign || 'center';
|
|
266
|
-
const width = image.cover.width ? image.cover.width : image.width;
|
|
267
|
-
const height = image.cover.height ? image.cover.height : image.height;
|
|
268
|
-
this.pdfDocument.save();
|
|
269
|
-
this.pdfDocument.rect(image.x, image.y, width, height).clip();
|
|
270
|
-
this.pdfDocument.image(image.image, image.x, image.y, {
|
|
271
|
-
cover: [width, height],
|
|
272
|
-
align: align,
|
|
273
|
-
valign: valign
|
|
274
|
-
});
|
|
275
|
-
this.pdfDocument.restore();
|
|
276
|
-
} else {
|
|
277
|
-
this.pdfDocument.image(image.image, image.x, image.y, {
|
|
278
|
-
width: image._width,
|
|
279
|
-
height: image._height
|
|
280
|
-
});
|
|
281
|
-
}
|
|
282
|
-
if (image.link) {
|
|
283
|
-
this.pdfDocument.link(image.x, image.y, image._width, image._height, image.link);
|
|
284
|
-
}
|
|
285
|
-
if (image.linkToPage) {
|
|
286
|
-
this.pdfDocument.ref({
|
|
287
|
-
Type: 'Action',
|
|
288
|
-
S: 'GoTo',
|
|
289
|
-
D: [image.linkToPage, 0, 0]
|
|
290
|
-
}).end();
|
|
291
|
-
this.pdfDocument.annotate(image.x, image.y, image._width, image._height, {
|
|
292
|
-
Subtype: 'Link',
|
|
293
|
-
Dest: [image.linkToPage - 1, 'XYZ', null, null, null]
|
|
294
|
-
});
|
|
295
|
-
}
|
|
296
|
-
if (image.linkToDestination) {
|
|
297
|
-
this.pdfDocument.goTo(image.x, image.y, image._width, image._height, image.linkToDestination);
|
|
298
|
-
}
|
|
299
|
-
if (image.linkToFile) {
|
|
300
|
-
const attachment = this.pdfDocument.provideAttachment(image.linkToFile);
|
|
301
|
-
this.pdfDocument.fileAnnotation(image.x, image.y, image._width, image._height, attachment,
|
|
302
|
-
// add empty rectangle as file annotation appearance with the same size as the rendered image
|
|
303
|
-
{
|
|
304
|
-
AP: {
|
|
305
|
-
N: {
|
|
306
|
-
Type: 'XObject',
|
|
307
|
-
Subtype: 'Form',
|
|
308
|
-
FormType: 1,
|
|
309
|
-
BBox: [image.x, image.y, image._width, image._height]
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
});
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
renderSVG(svg) {
|
|
316
|
-
let options = Object.assign({
|
|
317
|
-
width: svg._width,
|
|
318
|
-
height: svg._height,
|
|
319
|
-
assumePt: true
|
|
320
|
-
}, svg.options);
|
|
321
|
-
options.fontCallback = (family, bold, italic) => {
|
|
322
|
-
let fontsFamily = family.split(',').map(f => f.trim().replace(/('|")/g, ''));
|
|
323
|
-
let font = findFont(this.pdfDocument.fonts, fontsFamily, svg.font || 'Roboto');
|
|
324
|
-
let fontFile = this.pdfDocument.getFontFile(font, bold, italic);
|
|
325
|
-
if (fontFile === null) {
|
|
326
|
-
let type = this.pdfDocument.getFontType(bold, italic);
|
|
327
|
-
throw new Error(`Font '${font}' in style '${type}' is not defined in the font section of the document definition.`);
|
|
328
|
-
}
|
|
329
|
-
return fontFile;
|
|
330
|
-
};
|
|
331
|
-
(0, _svgToPdfkit.default)(this.pdfDocument, svg.svg, svg.x, svg.y, options);
|
|
332
|
-
}
|
|
333
|
-
renderAttachment(attachment) {
|
|
334
|
-
const file = this.pdfDocument.provideAttachment(attachment.attachment);
|
|
335
|
-
const options = {};
|
|
336
|
-
if (attachment.icon) {
|
|
337
|
-
options.Name = attachment.icon;
|
|
338
|
-
}
|
|
339
|
-
this.pdfDocument.fileAnnotation(attachment.x, attachment.y, attachment._width, attachment._height, file, options);
|
|
340
|
-
}
|
|
341
|
-
beginClip(rect) {
|
|
342
|
-
this.pdfDocument.save();
|
|
343
|
-
this.pdfDocument.addContent(`${rect.x} ${rect.y} ${rect.width} ${rect.height} re`);
|
|
344
|
-
this.pdfDocument.clip();
|
|
345
|
-
}
|
|
346
|
-
endClip() {
|
|
347
|
-
this.pdfDocument.restore();
|
|
348
|
-
}
|
|
349
|
-
renderWatermark(page) {
|
|
350
|
-
let watermark = page.watermark;
|
|
351
|
-
this.pdfDocument.fill(watermark.color);
|
|
352
|
-
this.pdfDocument.opacity(watermark.opacity);
|
|
353
|
-
this.pdfDocument.save();
|
|
354
|
-
this.pdfDocument.rotate(watermark.angle, {
|
|
355
|
-
origin: [this.pdfDocument.page.width / 2, this.pdfDocument.page.height / 2]
|
|
356
|
-
});
|
|
357
|
-
let x = this.pdfDocument.page.width / 2 - watermark._size.size.width / 2;
|
|
358
|
-
let y = this.pdfDocument.page.height / 2 - watermark._size.size.height / 2;
|
|
359
|
-
this.pdfDocument._font = watermark.font;
|
|
360
|
-
this.pdfDocument.fontSize(watermark.fontSize);
|
|
361
|
-
this.pdfDocument.text(watermark.text, x, y, {
|
|
362
|
-
lineBreak: false
|
|
363
|
-
});
|
|
364
|
-
this.pdfDocument.restore();
|
|
365
|
-
}
|
|
366
|
-
_updatePageOrientationInOptions(currentPage) {
|
|
367
|
-
let previousPageOrientation = this.pdfDocument.options.size[0] > this.pdfDocument.options.size[1] ? 'landscape' : 'portrait';
|
|
368
|
-
if (currentPage.pageSize.orientation !== previousPageOrientation) {
|
|
369
|
-
let width = this.pdfDocument.options.size[0];
|
|
370
|
-
let height = this.pdfDocument.options.size[1];
|
|
371
|
-
this.pdfDocument.options.size = [height, width];
|
|
372
|
-
}
|
|
373
|
-
}
|
|
374
|
-
}
|
|
375
|
-
var _default = exports.default = Renderer;
|