pdfmake 0.3.5 → 0.3.6

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/js/DocMeasure.js CHANGED
@@ -86,9 +86,11 @@ class DocMeasure {
86
86
  node._width = node._minWidth = node._maxWidth = node.cover.width;
87
87
  node._height = node._minHeight = node._maxHeight = node.cover.height;
88
88
  } else {
89
+ let nodeWidth = (0, _variableType.isNumber)(node.width) ? node.width : undefined;
90
+ let nodeHeight = (0, _variableType.isNumber)(node.height) ? node.height : undefined;
89
91
  let ratio = dimensions.width / dimensions.height;
90
- node._width = node._minWidth = node._maxWidth = node.width || (node.height ? node.height * ratio : dimensions.width);
91
- node._height = node.height || (node.width ? node.width / ratio : dimensions.height);
92
+ node._width = node._minWidth = node._maxWidth = nodeWidth || (nodeHeight ? nodeHeight * ratio : dimensions.width);
93
+ node._height = nodeHeight || (nodeWidth ? nodeWidth / ratio : dimensions.height);
92
94
  if ((0, _variableType.isNumber)(node.maxWidth) && node.maxWidth < node._width) {
93
95
  node._width = node._minWidth = node._maxWidth = node.maxWidth;
94
96
  node._height = node._width * dimensions.height / dimensions.width;
package/js/Printer.js CHANGED
@@ -11,28 +11,13 @@ var _Renderer = _interopRequireDefault(require("./Renderer"));
11
11
  var _variableType = require("./helpers/variableType");
12
12
  var _tools = require("./helpers/tools");
13
13
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
14
- /**
15
- * Printer which turns document definition into a pdf
16
- *
17
- * @example
18
- * var fontDescriptors = {
19
- * Roboto: {
20
- * normal: 'fonts/Roboto-Regular.ttf',
21
- * bold: 'fonts/Roboto-Medium.ttf',
22
- * italics: 'fonts/Roboto-Italic.ttf',
23
- * bolditalics: 'fonts/Roboto-MediumItalic.ttf'
24
- * }
25
- * };
26
- *
27
- * var printer = new PdfPrinter(fontDescriptors);
28
- */
29
14
  class PdfPrinter {
30
15
  /**
31
16
  * @param {object} fontDescriptors font definition dictionary
32
17
  * @param {object} virtualfs
33
18
  * @param {object} urlResolver
34
19
  */
35
- constructor(fontDescriptors, virtualfs = null, urlResolver = null) {
20
+ constructor(fontDescriptors, virtualfs, urlResolver) {
36
21
  this.fontDescriptors = fontDescriptors;
37
22
  this.virtualfs = virtualfs;
38
23
  this.urlResolver = urlResolver;
@@ -126,9 +111,6 @@ class PdfPrinter {
126
111
  headers: {}
127
112
  };
128
113
  };
129
- if (this.urlResolver === null) {
130
- return;
131
- }
132
114
  for (let font in this.fontDescriptors) {
133
115
  if (this.fontDescriptors.hasOwnProperty(font)) {
134
116
  if (this.fontDescriptors[font].normal) {
package/js/URLResolver.js CHANGED
@@ -21,6 +21,14 @@ class URLResolver {
21
21
  constructor(fs) {
22
22
  this.fs = fs;
23
23
  this.resolving = {};
24
+ this.urlAccessPolicy = undefined;
25
+ }
26
+
27
+ /**
28
+ * @param {(url: string) => boolean} callback
29
+ */
30
+ setUrlAccessPolicy(callback) {
31
+ this.urlAccessPolicy = callback;
24
32
  }
25
33
  resolve(url, headers = {}) {
26
34
  const resolveUrlInternal = async () => {
@@ -28,6 +36,9 @@ class URLResolver {
28
36
  if (this.fs.existsSync(url)) {
29
37
  return; // url was downloaded earlier
30
38
  }
39
+ if (typeof this.urlAccessPolicy !== 'undefined' && this.urlAccessPolicy(url) !== true) {
40
+ throw new Error(`Access to URL denied by resource access policy: ${url}`);
41
+ }
31
42
  const buffer = await fetchUrl(url, headers);
32
43
  this.fs.writeFileSync(url, buffer);
33
44
  }
package/js/base.js CHANGED
@@ -6,11 +6,12 @@ var _Printer = _interopRequireDefault(require("./Printer"));
6
6
  var _virtualFs = _interopRequireDefault(require("./virtual-fs"));
7
7
  var _tools = require("./helpers/tools");
8
8
  var _variableType = require("./helpers/variableType");
9
+ var _URLResolver = _interopRequireDefault(require("./URLResolver"));
9
10
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
10
11
  class pdfmake {
11
12
  constructor() {
12
13
  this.virtualfs = _virtualFs.default;
13
- this.urlResolver = null;
14
+ this.urlAccessPolicy = undefined;
14
15
  }
15
16
 
16
17
  /**
@@ -27,10 +28,26 @@ class pdfmake {
27
28
  }
28
29
  options.progressCallback = this.progressCallback;
29
30
  options.tableLayouts = this.tableLayouts;
30
- let printer = new _Printer.default(this.fonts, this.virtualfs, this.urlResolver());
31
+ const isServer = typeof process !== 'undefined' && process?.versions?.node;
32
+ if (typeof this.urlAccessPolicy === 'undefined' && isServer) {
33
+ console.warn('No URL access policy defined. Consider using setUrlAccessPolicy() to restrict external resource downloads.');
34
+ }
35
+ let urlResolver = new _URLResolver.default(this.virtualfs);
36
+ urlResolver.setUrlAccessPolicy(this.urlAccessPolicy);
37
+ let printer = new _Printer.default(this.fonts, this.virtualfs, urlResolver);
31
38
  const pdfDocumentPromise = printer.createPdfKitDocument(docDefinition, options);
32
39
  return this._transformToDocument(pdfDocumentPromise);
33
40
  }
41
+
42
+ /**
43
+ * @param {(url: string) => boolean} callback
44
+ */
45
+ setUrlAccessPolicy(callback) {
46
+ if (callback !== undefined && typeof callback !== 'function') {
47
+ throw new Error("Parameter 'callback' has an invalid type. Function or undefined expected.");
48
+ }
49
+ this.urlAccessPolicy = callback;
50
+ }
34
51
  setProgressCallback(callback) {
35
52
  this.progressCallback = callback;
36
53
  }
@@ -4,7 +4,6 @@ exports.__esModule = true;
4
4
  exports.default = void 0;
5
5
  var _base = _interopRequireDefault(require("../base"));
6
6
  var _OutputDocumentBrowser = _interopRequireDefault(require("./OutputDocumentBrowser"));
7
- var _URLResolver = _interopRequireDefault(require("../URLResolver"));
8
7
  var _fs = _interopRequireDefault(require("fs"));
9
8
  var _configurator = _interopRequireDefault(require("core-js/configurator"));
10
9
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
@@ -23,7 +22,6 @@ let defaultClientFonts = {
23
22
  class pdfmake extends _base.default {
24
23
  constructor() {
25
24
  super();
26
- this.urlResolver = () => new _URLResolver.default(this.virtualfs);
27
25
  this.fonts = defaultClientFonts;
28
26
  }
29
27
  addFontContainer(fontContainer) {
package/js/index.js CHANGED
@@ -2,11 +2,9 @@
2
2
 
3
3
  const pdfmakeBase = require('./base').default;
4
4
  const OutputDocumentServer = require('./OutputDocumentServer').default;
5
- const URLResolver = require('./URLResolver').default;
6
5
  class pdfmake extends pdfmakeBase {
7
6
  constructor() {
8
7
  super();
9
- this.urlResolver = () => new URLResolver(this.virtualfs);
10
8
  }
11
9
  _transformToDocument(doc) {
12
10
  return new OutputDocumentServer(doc);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pdfmake",
3
- "version": "0.3.5",
3
+ "version": "0.3.6",
4
4
  "description": "Client/server side PDF printing in pure JavaScript",
5
5
  "main": "js/index.js",
6
6
  "esnext": "src/index.js",
package/src/DocMeasure.js CHANGED
@@ -95,10 +95,12 @@ class DocMeasure {
95
95
  node._width = node._minWidth = node._maxWidth = node.cover.width;
96
96
  node._height = node._minHeight = node._maxHeight = node.cover.height;
97
97
  } else {
98
+ let nodeWidth = isNumber(node.width) ? node.width : undefined;
99
+ let nodeHeight = isNumber(node.height) ? node.height : undefined;
98
100
  let ratio = dimensions.width / dimensions.height;
99
101
 
100
- node._width = node._minWidth = node._maxWidth = node.width || (node.height ? (node.height * ratio) : dimensions.width);
101
- node._height = node.height || (node.width ? node.width / ratio : dimensions.height);
102
+ node._width = node._minWidth = node._maxWidth = nodeWidth || (nodeHeight ? (nodeHeight * ratio) : dimensions.width);
103
+ node._height = nodeHeight || (nodeWidth ? nodeWidth / ratio : dimensions.height);
102
104
 
103
105
  if (isNumber(node.maxWidth) && node.maxWidth < node._width) {
104
106
  node._width = node._minWidth = node._maxWidth = node.maxWidth;
package/src/Printer.js CHANGED
@@ -7,21 +7,6 @@ import Renderer from './Renderer';
7
7
  import { isNumber, isValue } from './helpers/variableType';
8
8
  import { convertToDynamicContent } from './helpers/tools';
9
9
 
10
- /**
11
- * Printer which turns document definition into a pdf
12
- *
13
- * @example
14
- * var fontDescriptors = {
15
- * Roboto: {
16
- * normal: 'fonts/Roboto-Regular.ttf',
17
- * bold: 'fonts/Roboto-Medium.ttf',
18
- * italics: 'fonts/Roboto-Italic.ttf',
19
- * bolditalics: 'fonts/Roboto-MediumItalic.ttf'
20
- * }
21
- * };
22
- *
23
- * var printer = new PdfPrinter(fontDescriptors);
24
- */
25
10
  class PdfPrinter {
26
11
 
27
12
  /**
@@ -29,7 +14,7 @@ class PdfPrinter {
29
14
  * @param {object} virtualfs
30
15
  * @param {object} urlResolver
31
16
  */
32
- constructor(fontDescriptors, virtualfs = null, urlResolver = null) {
17
+ constructor(fontDescriptors, virtualfs, urlResolver) {
33
18
  this.fontDescriptors = fontDescriptors;
34
19
  this.virtualfs = virtualfs;
35
20
  this.urlResolver = urlResolver;
@@ -127,10 +112,6 @@ class PdfPrinter {
127
112
  return { url: url, headers: {} };
128
113
  };
129
114
 
130
- if (this.urlResolver === null) {
131
- return;
132
- }
133
-
134
115
  for (let font in this.fontDescriptors) {
135
116
  if (this.fontDescriptors.hasOwnProperty(font)) {
136
117
  if (this.fontDescriptors[font].normal) {
@@ -14,6 +14,14 @@ class URLResolver {
14
14
  constructor(fs) {
15
15
  this.fs = fs;
16
16
  this.resolving = {};
17
+ this.urlAccessPolicy = undefined;
18
+ }
19
+
20
+ /**
21
+ * @param {(url: string) => boolean} callback
22
+ */
23
+ setUrlAccessPolicy(callback) {
24
+ this.urlAccessPolicy = callback;
17
25
  }
18
26
 
19
27
  resolve(url, headers = {}) {
@@ -22,6 +30,11 @@ class URLResolver {
22
30
  if (this.fs.existsSync(url)) {
23
31
  return; // url was downloaded earlier
24
32
  }
33
+
34
+ if ((typeof this.urlAccessPolicy !== 'undefined') && (this.urlAccessPolicy(url) !== true)) {
35
+ throw new Error(`Access to URL denied by resource access policy: ${url}`);
36
+ }
37
+
25
38
  const buffer = await fetchUrl(url, headers);
26
39
  this.fs.writeFileSync(url, buffer);
27
40
  }
package/src/base.js CHANGED
@@ -2,12 +2,13 @@ import Printer from './Printer';
2
2
  import virtualfs from './virtual-fs';
3
3
  import { pack } from './helpers/tools';
4
4
  import { isObject } from './helpers/variableType';
5
+ import URLResolver from './URLResolver';
5
6
 
6
7
  class pdfmake {
7
8
 
8
9
  constructor() {
9
10
  this.virtualfs = virtualfs;
10
- this.urlResolver = null;
11
+ this.urlAccessPolicy = undefined;
11
12
  }
12
13
 
13
14
  /**
@@ -27,12 +28,33 @@ class pdfmake {
27
28
  options.progressCallback = this.progressCallback;
28
29
  options.tableLayouts = this.tableLayouts;
29
30
 
30
- let printer = new Printer(this.fonts, this.virtualfs, this.urlResolver());
31
+ const isServer = typeof process !== 'undefined' && process?.versions?.node;
32
+ if (typeof this.urlAccessPolicy === 'undefined' && isServer) {
33
+ console.warn(
34
+ 'No URL access policy defined. Consider using setUrlAccessPolicy() to restrict external resource downloads.'
35
+ );
36
+ }
37
+
38
+ let urlResolver = new URLResolver(this.virtualfs);
39
+ urlResolver.setUrlAccessPolicy(this.urlAccessPolicy);
40
+
41
+ let printer = new Printer(this.fonts, this.virtualfs, urlResolver);
31
42
  const pdfDocumentPromise = printer.createPdfKitDocument(docDefinition, options);
32
43
 
33
44
  return this._transformToDocument(pdfDocumentPromise);
34
45
  }
35
46
 
47
+ /**
48
+ * @param {(url: string) => boolean} callback
49
+ */
50
+ setUrlAccessPolicy(callback) {
51
+ if (callback !== undefined && typeof callback !== 'function') {
52
+ throw new Error("Parameter 'callback' has an invalid type. Function or undefined expected.");
53
+ }
54
+
55
+ this.urlAccessPolicy = callback;
56
+ }
57
+
36
58
  setProgressCallback(callback) {
37
59
  this.progressCallback = callback;
38
60
  }
@@ -1,6 +1,5 @@
1
1
  import pdfmakeBase from '../base';
2
2
  import OutputDocumentBrowser from './OutputDocumentBrowser';
3
- import URLResolver from '../URLResolver';
4
3
  import fs from 'fs';
5
4
  import configurator from 'core-js/configurator';
6
5
 
@@ -21,7 +20,6 @@ let defaultClientFonts = {
21
20
  class pdfmake extends pdfmakeBase {
22
21
  constructor() {
23
22
  super();
24
- this.urlResolver = () => new URLResolver(this.virtualfs);
25
23
  this.fonts = defaultClientFonts;
26
24
  }
27
25
 
package/src/index.js CHANGED
@@ -1,11 +1,9 @@
1
1
  const pdfmakeBase = require('./base').default;
2
2
  const OutputDocumentServer = require('./OutputDocumentServer').default;
3
- const URLResolver = require('./URLResolver').default;
4
3
 
5
4
  class pdfmake extends pdfmakeBase {
6
5
  constructor() {
7
6
  super();
8
- this.urlResolver = () => new URLResolver(this.virtualfs);
9
7
  }
10
8
 
11
9
  _transformToDocument(doc) {