pdfmake 0.3.11 → 0.3.13

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 (45) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/build/pdfmake.js +46900 -62599
  3. package/build/pdfmake.js.map +1 -1
  4. package/build/pdfmake.min.js +2 -2
  5. package/build/pdfmake.min.js.map +1 -1
  6. package/build/standard-fonts/Courier.js +1 -19
  7. package/build/standard-fonts/Helvetica.js +1 -19
  8. package/build/standard-fonts/Symbol.js +1 -13
  9. package/build/standard-fonts/Times.js +1 -19
  10. package/build/standard-fonts/ZapfDingbats.js +1 -13
  11. package/js/DocumentContext.js +3 -1
  12. package/js/ElementWriter.js +5 -2
  13. package/js/LayoutBuilder.js +12 -4
  14. package/js/OutputDocument.js +18 -9
  15. package/js/OutputDocumentServer.js +5 -0
  16. package/js/PDFDocument.js +7 -8
  17. package/js/PageElementWriter.js +5 -1
  18. package/js/Renderer.js +5 -0
  19. package/js/TableProcessor.js +13 -0
  20. package/js/TextDecorator.js +3 -3
  21. package/js/browser-extensions/index.js +7 -1
  22. package/js/browser-extensions/standard-fonts/Courier.js +1 -19
  23. package/js/browser-extensions/standard-fonts/Helvetica.js +1 -19
  24. package/js/browser-extensions/standard-fonts/Symbol.js +1 -7
  25. package/js/browser-extensions/standard-fonts/Times.js +1 -19
  26. package/js/browser-extensions/standard-fonts/ZapfDingbats.js +1 -7
  27. package/js/columnCalculator.js +10 -4
  28. package/package.json +3 -9
  29. package/src/DocumentContext.js +1 -1
  30. package/src/ElementWriter.js +6 -2
  31. package/src/LayoutBuilder.js +12 -4
  32. package/src/OutputDocument.js +20 -9
  33. package/src/OutputDocumentServer.js +6 -0
  34. package/src/PDFDocument.js +7 -8
  35. package/src/PageElementWriter.js +5 -1
  36. package/src/Renderer.js +6 -0
  37. package/src/TableProcessor.js +15 -0
  38. package/src/TextDecorator.js +4 -3
  39. package/src/browser-extensions/index.js +7 -1
  40. package/src/browser-extensions/standard-fonts/Courier.js +6 -8
  41. package/src/browser-extensions/standard-fonts/Helvetica.js +6 -8
  42. package/src/browser-extensions/standard-fonts/Symbol.js +1 -5
  43. package/src/browser-extensions/standard-fonts/Times.js +6 -8
  44. package/src/browser-extensions/standard-fonts/ZapfDingbats.js +1 -5
  45. package/src/columnCalculator.js +4 -4
@@ -4,16 +4,29 @@ class OutputDocument {
4
4
  * @param {Promise<object>} pdfDocumentPromise
5
5
  */
6
6
  constructor(pdfDocumentPromise) {
7
- this.bufferSize = 1073741824;
8
7
  this.pdfDocumentPromise = pdfDocumentPromise;
9
8
  this.bufferPromise = null;
9
+ this.streamEnded = false;
10
+ this.streamEndListenerAttached = false;
10
11
  }
11
12
 
12
13
  /**
13
14
  * @returns {Promise<object>}
14
15
  */
15
- getStream() {
16
- return this.pdfDocumentPromise;
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;
17
30
  }
18
31
 
19
32
  /**
@@ -22,17 +35,15 @@ class OutputDocument {
22
35
  getBuffer() {
23
36
  const getBufferInternal = (async () => {
24
37
  const stream = await this.getStream();
25
- return new Promise((resolve) => {
38
+ return new Promise((resolve, reject) => {
26
39
  let chunks = [];
27
- stream.on('readable', () => {
28
- let chunk;
29
- while ((chunk = stream.read(this.bufferSize)) !== null) {
30
- chunks.push(chunk);
31
- }
40
+ stream.on('data', chunk => {
41
+ chunks.push(chunk);
32
42
  });
33
43
  stream.on('end', () => {
34
44
  resolve(Buffer.concat(chunks));
35
45
  });
46
+ stream.on('error', reject);
36
47
  stream.end();
37
48
  });
38
49
  });
@@ -8,6 +8,12 @@ class OutputDocumentServer extends OutputDocument {
8
8
  * @returns {Promise}
9
9
  */
10
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
+
11
17
  const stream = await this.getStream();
12
18
  const writeStream = fs.createWriteStream(filename);
13
19
 
@@ -1,4 +1,4 @@
1
- import PDFKit from 'pdfkit';
1
+ import { PDFDocument as PDFKitDocument } from 'pdfkit';
2
2
  import { isString } from './helpers/variableType';
3
3
 
4
4
  const typeName = (bold, italics) => {
@@ -13,7 +13,7 @@ const typeName = (bold, italics) => {
13
13
  return type;
14
14
  };
15
15
 
16
- class PDFDocument extends PDFKit {
16
+ class PDFDocument extends PDFKitDocument {
17
17
  constructor(fonts = {}, images = {}, patterns = {}, attachments = {}, options = {}, virtualfs = null, localAccessPolicy = undefined) {
18
18
  super(options);
19
19
 
@@ -154,14 +154,13 @@ class PDFDocument extends PDFKit {
154
154
  return obj;
155
155
  };
156
156
 
157
- if (typeof src === 'object') {
158
- return checkRequired(src);
159
- }
160
-
161
- let attachment = checkRequired(this.attachments[src]);
157
+ let attachment = checkRequired(typeof src === 'object' ? src : this.attachments[src]);
162
158
 
163
159
  if (this.virtualfs && this.virtualfs.existsSync(attachment.src)) {
164
- return this.virtualfs.readFileSync(attachment.src);
160
+ return {
161
+ ...attachment,
162
+ src: this.virtualfs.readFileSync(attachment.src)
163
+ };
165
164
  }
166
165
 
167
166
  this.validateLocalFile(attachment.src);
@@ -176,9 +176,13 @@ class PageElementWriter extends ElementWriter {
176
176
  /**
177
177
  * Move to the next column in a column group (snaking columns).
178
178
  * Handles repeatables and emits columnChanged event.
179
+ * @param {Function} [updateContext] - Adjust the new column before inserting repeatables
179
180
  */
180
- moveToNextColumn() {
181
+ moveToNextColumn(updateContext) {
181
182
  let nextColumn = this.context().moveToNextColumn();
183
+ if (updateContext) {
184
+ updateContext(this.context());
185
+ }
182
186
 
183
187
  // Handle repeatables (like table headers) for the new column
184
188
  this.repeatables.forEach(function (rep) {
package/src/Renderer.js CHANGED
@@ -367,6 +367,12 @@ class Renderer {
367
367
 
368
368
  return fontFile;
369
369
  };
370
+ options.imageCallback = link => {
371
+ let src = link.replace(/\s+/g, '');
372
+ this.pdfDocument.validateLocalFile(src);
373
+
374
+ return src;
375
+ };
370
376
 
371
377
  SVGtoPDF(this.pdfDocument, svg.svg, svg.x, svg.y, options);
372
378
 
@@ -109,6 +109,7 @@ class TableProcessor {
109
109
  ColumnCalculator.buildColumnWidths(tableNode.table.widths, availableWidth, this.offsets.total, tableNode);
110
110
 
111
111
  this.tableWidth = tableNode._offsets.total + getTableInnerContentWidth();
112
+ this.alignTable(writer.context());
112
113
  this.rowSpanData = prepareRowSpanData();
113
114
  this.cleanUpRepeatables = false;
114
115
 
@@ -150,6 +151,17 @@ class TableProcessor {
150
151
  prepareCellBorders(this.tableNode.table.body);
151
152
  }
152
153
 
154
+ alignTable(context) {
155
+ const freeWidth = Math.max(0, context.availableWidth - this.tableWidth);
156
+ this.alignmentOffset = this.tableNode.tableAlignment === 'center' ? freeWidth / 2 :
157
+ (this.tableNode.tableAlignment === 'right' ? freeWidth : 0);
158
+ if (this.alignmentOffset) {
159
+ // Keep the container width intact: column widths have already been resolved.
160
+ // Row snapshots preserve this origin across page breaks.
161
+ context.addMargin(this.alignmentOffset, -this.alignmentOffset);
162
+ }
163
+ }
164
+
153
165
  onRowBreak(rowIndex, writer) {
154
166
  return () => {
155
167
  let offset = this.rowPaddingTop + (!this.headerRows ? this.topLineWidth : 0);
@@ -376,6 +388,9 @@ class TableProcessor {
376
388
  if (this.cleanUpRepeatables) {
377
389
  writer.popFromRepeatables();
378
390
  }
391
+ if (this.alignmentOffset) {
392
+ writer.context().addMargin(-this.alignmentOffset, this.alignmentOffset);
393
+ }
379
394
  }
380
395
 
381
396
  endRow(rowIndex, writer, pageBreaks) {
@@ -19,7 +19,8 @@ const groupDecorations = (line, pdfDocument) => {
19
19
  for (let ii = 0, ll = decoration.length; ii < ll; ii++) {
20
20
  let decorationItem = decoration[ii];
21
21
  if (!currentGroup || decorationItem !== currentGroup.decoration ||
22
- style !== currentGroup.decorationStyle || color !== currentGroup.decorationColor) {
22
+ style !== currentGroup.decorationStyle || color !== currentGroup.decorationColor ||
23
+ thickness !== currentGroup.decorationThickness) {
23
24
 
24
25
  currentGroup = {
25
26
  line: line,
@@ -79,7 +80,7 @@ class TextDecorator {
79
80
  let max = 0;
80
81
  for (let i = 0, l = group.inlines.length; i < l; i++) {
81
82
  let inline = group.inlines[i];
82
- max = inline.fontSize > max ? i : max;
83
+ max = inline.fontSize > group.inlines[max].fontSize ? i : max;
83
84
  }
84
85
  return group.inlines[max];
85
86
  };
@@ -155,7 +156,7 @@ class TextDecorator {
155
156
  let sh = 0.7, sv = 1;
156
157
  let nbWaves = Math.ceil(totalWidth / (sh * 2)) + 1;
157
158
  let rwx = x + firstInline.x - 1;
158
- this.pdfDocument.rect(x + firstInline.x, y - sv, totalWidth, y + sv).clip();
159
+ this.pdfDocument.rect(x + firstInline.x, y - sv, totalWidth, 2 * sv).clip();
159
160
  this.pdfDocument.lineWidth(lw / 3);
160
161
  this.pdfDocument.moveTo(rwx, y);
161
162
  for (let i = 0; i < nbWaves; i++) {
@@ -2,6 +2,7 @@ import pdfmakeBase from '../base';
2
2
  import OutputDocumentBrowser from './OutputDocumentBrowser';
3
3
  import fs from 'fs';
4
4
  import configurator from 'core-js/configurator';
5
+ import { registerStdFonts } from 'pdfkit';
5
6
 
6
7
  // core-js: Polyfills will be used only if natives completely unavailable.
7
8
  configurator({
@@ -24,7 +25,12 @@ class pdfmake extends pdfmakeBase {
24
25
  }
25
26
 
26
27
  addFontContainer(fontContainer) {
27
- this.addVirtualFileSystem(fontContainer.vfs);
28
+ if (fontContainer.standardFonts) {
29
+ registerStdFonts(...fontContainer.standardFonts);
30
+ }
31
+ if (fontContainer.vfs) {
32
+ this.addVirtualFileSystem(fontContainer.vfs);
33
+ }
28
34
  this.addFonts(fontContainer.fonts);
29
35
  }
30
36
 
@@ -1,12 +1,10 @@
1
- var fs = require('fs');
2
-
3
1
  var fontContainer = {
4
- vfs: {
5
- 'data/Courier.afm': { data: fs.readFileSync(__dirname + '/../../../node_modules/pdfkit/js/data/Courier.afm', 'utf8'), encoding: 'utf8' },
6
- 'data/Courier-Bold.afm': { data: fs.readFileSync(__dirname + '/../../../node_modules/pdfkit/js/data/Courier-Bold.afm', 'utf8'), encoding: 'utf8' },
7
- 'data/Courier-Oblique.afm': { data: fs.readFileSync(__dirname + '/../../../node_modules/pdfkit/js/data/Courier-Oblique.afm', 'utf8'), encoding: 'utf8' },
8
- 'data/Courier-BoldOblique.afm': { data: fs.readFileSync(__dirname + '/../../../node_modules/pdfkit/js/data/Courier-BoldOblique.afm', 'utf8'), encoding: 'utf8' }
9
- },
2
+ standardFonts: [
3
+ require('pdfkit/standard-fonts/Courier'),
4
+ require('pdfkit/standard-fonts/CourierBold'),
5
+ require('pdfkit/standard-fonts/CourierOblique'),
6
+ require('pdfkit/standard-fonts/CourierBoldOblique')
7
+ ],
10
8
  fonts: {
11
9
  Courier: {
12
10
  normal: 'Courier',
@@ -1,12 +1,10 @@
1
- var fs = require('fs');
2
-
3
1
  var fontContainer = {
4
- vfs: {
5
- 'data/Helvetica.afm': { data: fs.readFileSync(__dirname + '/../../../node_modules/pdfkit/js/data/Helvetica.afm', 'utf8'), encoding: 'utf8' },
6
- 'data/Helvetica-Bold.afm': { data: fs.readFileSync(__dirname + '/../../../node_modules/pdfkit/js/data/Helvetica-Bold.afm', 'utf8'), encoding: 'utf8' },
7
- 'data/Helvetica-Oblique.afm': { data: fs.readFileSync(__dirname + '/../../../node_modules/pdfkit/js/data/Helvetica-Oblique.afm', 'utf8'), encoding: 'utf8' },
8
- 'data/Helvetica-BoldOblique.afm': { data: fs.readFileSync(__dirname + '/../../../node_modules/pdfkit/js/data/Helvetica-BoldOblique.afm', 'utf8'), encoding: 'utf8' }
9
- },
2
+ standardFonts: [
3
+ require('pdfkit/standard-fonts/Helvetica'),
4
+ require('pdfkit/standard-fonts/HelveticaBold'),
5
+ require('pdfkit/standard-fonts/HelveticaOblique'),
6
+ require('pdfkit/standard-fonts/HelveticaBoldOblique')
7
+ ],
10
8
  fonts: {
11
9
  Helvetica: {
12
10
  normal: 'Helvetica',
@@ -1,9 +1,5 @@
1
- var fs = require('fs');
2
-
3
1
  var fontContainer = {
4
- vfs: {
5
- 'data/Symbol.afm': { data: fs.readFileSync(__dirname + '/../../../node_modules/pdfkit/js/data/Symbol.afm', 'utf8'), encoding: 'utf8' }
6
- },
2
+ standardFonts: [require('pdfkit/standard-fonts/Symbol')],
7
3
  fonts: {
8
4
  Symbol: {
9
5
  normal: 'Symbol'
@@ -1,12 +1,10 @@
1
- var fs = require('fs');
2
-
3
1
  var fontContainer = {
4
- vfs: {
5
- 'data/Times-Roman.afm': { data: fs.readFileSync(__dirname + '/../../../node_modules/pdfkit/js/data/Times-Roman.afm', 'utf8'), encoding: 'utf8' },
6
- 'data/Times-Bold.afm': { data: fs.readFileSync(__dirname + '/../../../node_modules/pdfkit/js/data/Times-Bold.afm', 'utf8'), encoding: 'utf8' },
7
- 'data/Times-Italic.afm': { data: fs.readFileSync(__dirname + '/../../../node_modules/pdfkit/js/data/Times-Italic.afm', 'utf8'), encoding: 'utf8' },
8
- 'data/Times-BoldItalic.afm': { data: fs.readFileSync(__dirname + '/../../../node_modules/pdfkit/js/data/Times-BoldItalic.afm', 'utf8'), encoding: 'utf8' }
9
- },
2
+ standardFonts: [
3
+ require('pdfkit/standard-fonts/TimesRoman'),
4
+ require('pdfkit/standard-fonts/TimesBold'),
5
+ require('pdfkit/standard-fonts/TimesItalic'),
6
+ require('pdfkit/standard-fonts/TimesBoldItalic')
7
+ ],
10
8
  fonts: {
11
9
  Times: {
12
10
  normal: 'Times-Roman',
@@ -1,9 +1,5 @@
1
- var fs = require('fs');
2
-
3
1
  var fontContainer = {
4
- vfs: {
5
- 'data/ZapfDingbats.afm': { data: fs.readFileSync(__dirname + '/../../../node_modules/pdfkit/js/data/ZapfDingbats.afm', 'utf8'), encoding: 'utf8' }
6
- },
2
+ standardFonts: [require('pdfkit/standard-fonts/ZapfDingbats')],
7
3
  fonts: {
8
4
  ZapfDingbats: {
9
5
  normal: 'ZapfDingbats'
@@ -10,7 +10,7 @@ function buildColumnWidths(columns, availableWidth, offsetTotal = 0, tableNode)
10
10
  let fixedColumns = [];
11
11
  let initial_availableWidth = availableWidth;
12
12
 
13
- columns.forEach(column => {
13
+ columns.forEach((column, colIndex) => {
14
14
  if (isAutoColumn(column)) {
15
15
  autoColumns.push(column);
16
16
  autoMin += column._minWidth;
@@ -20,11 +20,11 @@ function buildColumnWidths(columns, availableWidth, offsetTotal = 0, tableNode)
20
20
  starMaxMin = Math.max(starMaxMin, column._minWidth);
21
21
  starMaxMax = Math.max(starMaxMax, column._maxWidth);
22
22
  } else {
23
- fixedColumns.push(column);
23
+ fixedColumns.push({ column, colIndex });
24
24
  }
25
25
  });
26
26
 
27
- fixedColumns.forEach((col, colIndex) => {
27
+ fixedColumns.forEach(({ column: col, colIndex }) => {
28
28
  // width specified as %
29
29
  if (isString(col.width) && /\d+%/.test(col.width)) {
30
30
  // In tables we have to take into consideration the reserved width for paddings and borders
@@ -38,7 +38,7 @@ function buildColumnWidths(columns, availableWidth, offsetTotal = 0, tableNode)
38
38
  // first column assumes whole borderLeft and half of border right
39
39
  reservedWidth = paddingLeft + paddingRight + borderLeft + (borderRight / 2);
40
40
 
41
- } else if (colIndex === fixedColumns.length - 1) {
41
+ } else if (colIndex === columns.length - 1) {
42
42
  // last column assumes whole borderRight and half of border left
43
43
  reservedWidth = paddingLeft + paddingRight + (borderLeft / 2) + borderRight;
44
44