pdfmake 0.2.4 → 0.3.0-beta.1

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