pdfmake 0.3.0-beta.8 → 0.3.0-beta.9
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 +8 -0
- package/build/pdfmake.js +74624 -74535
- package/build/pdfmake.js.map +1 -1
- package/build/pdfmake.min.js +2 -2
- package/build/pdfmake.min.js.map +1 -1
- package/js/3rd-party/svg-to-pdfkit/source.js +3626 -0
- package/js/3rd-party/svg-to-pdfkit.js +7 -0
- package/js/DocMeasure.js +626 -0
- package/js/DocPreprocessor.js +238 -0
- package/js/DocumentContext.js +258 -0
- package/js/ElementWriter.js +331 -0
- package/js/LayoutBuilder.js +744 -0
- package/js/Line.js +105 -0
- package/js/OutputDocument.js +76 -0
- package/js/OutputDocumentServer.js +27 -0
- package/js/PDFDocument.js +144 -0
- package/js/PageElementWriter.js +140 -0
- package/js/PageSize.js +74 -0
- package/js/Printer.js +291 -0
- package/js/Renderer.js +375 -0
- package/js/SVGMeasure.js +69 -0
- package/js/StyleContextStack.js +164 -0
- package/js/TableProcessor.js +522 -0
- package/js/TextBreaker.js +139 -0
- package/js/TextDecorator.js +143 -0
- package/js/TextInlines.js +206 -0
- package/js/URLResolver.js +73 -0
- package/js/base.js +52 -0
- package/js/browser-extensions/OutputDocumentBrowser.js +118 -0
- package/js/browser-extensions/URLBrowserResolver.js +76 -0
- package/js/browser-extensions/fonts/Roboto.js +38 -0
- package/js/browser-extensions/index.js +53 -0
- package/js/browser-extensions/pdfMake.js +15 -0
- package/js/browser-extensions/standard-fonts/Courier.js +38 -0
- package/js/browser-extensions/standard-fonts/Helvetica.js +38 -0
- package/js/browser-extensions/standard-fonts/Symbol.js +23 -0
- package/js/browser-extensions/standard-fonts/Times.js +38 -0
- package/js/browser-extensions/standard-fonts/ZapfDingbats.js +23 -0
- package/js/browser-extensions/virtual-fs-cjs.js +3 -0
- package/js/columnCalculator.js +148 -0
- package/js/helpers/node.js +98 -0
- package/js/helpers/tools.js +40 -0
- package/js/helpers/variableType.js +59 -0
- package/js/index.js +15 -0
- package/js/qrEnc.js +721 -0
- package/js/standardPageSizes.js +56 -0
- package/js/tableLayouts.js +98 -0
- package/js/virtual-fs.js +60 -0
- package/package.json +1 -1
- package/src/DocMeasure.js +7 -6
- package/src/DocumentContext.js +6 -13
- package/src/LayoutBuilder.js +52 -2
- package/src/StyleContextStack.js +3 -44
- package/src/TableProcessor.js +22 -6
- package/src/columnCalculator.js +24 -3
- package/src/helpers/variableType.js +11 -0
package/js/Line.js
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.default = void 0;
|
|
5
|
+
class Line {
|
|
6
|
+
/**
|
|
7
|
+
* @param {number} maxWidth Maximum width this line can have
|
|
8
|
+
*/
|
|
9
|
+
constructor(maxWidth) {
|
|
10
|
+
this.maxWidth = maxWidth;
|
|
11
|
+
this.leadingCut = 0;
|
|
12
|
+
this.trailingCut = 0;
|
|
13
|
+
this.inlineWidths = 0;
|
|
14
|
+
this.inlines = [];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @param {object} inline
|
|
19
|
+
*/
|
|
20
|
+
addInline(inline) {
|
|
21
|
+
if (this.inlines.length === 0) {
|
|
22
|
+
this.leadingCut = inline.leadingCut || 0;
|
|
23
|
+
}
|
|
24
|
+
this.trailingCut = inline.trailingCut || 0;
|
|
25
|
+
inline.x = this.inlineWidths - this.leadingCut;
|
|
26
|
+
this.inlines.push(inline);
|
|
27
|
+
this.inlineWidths += inline.width;
|
|
28
|
+
if (inline.lineEnd) {
|
|
29
|
+
this.newLineForced = true;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* @returns {number}
|
|
35
|
+
*/
|
|
36
|
+
getHeight() {
|
|
37
|
+
let max = 0;
|
|
38
|
+
this.inlines.forEach(item => {
|
|
39
|
+
max = Math.max(max, item.height || 0);
|
|
40
|
+
});
|
|
41
|
+
return max;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* @returns {number}
|
|
46
|
+
*/
|
|
47
|
+
getAscenderHeight() {
|
|
48
|
+
let y = 0;
|
|
49
|
+
this.inlines.forEach(inline => {
|
|
50
|
+
y = Math.max(y, inline.font.ascender / 1000 * inline.fontSize);
|
|
51
|
+
});
|
|
52
|
+
return y;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* @returns {number}
|
|
57
|
+
*/
|
|
58
|
+
getWidth() {
|
|
59
|
+
return this.inlineWidths - this.leadingCut - this.trailingCut;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* @returns {number}
|
|
64
|
+
*/
|
|
65
|
+
getAvailableWidth() {
|
|
66
|
+
return this.maxWidth - this.getWidth();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* @param {object} inline
|
|
71
|
+
* @param {Array} nextInlines
|
|
72
|
+
* @returns {boolean}
|
|
73
|
+
*/
|
|
74
|
+
hasEnoughSpaceForInline(inline, nextInlines = []) {
|
|
75
|
+
if (this.inlines.length === 0) {
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
if (this.newLineForced) {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
let inlineWidth = inline.width;
|
|
82
|
+
let inlineTrailingCut = inline.trailingCut || 0;
|
|
83
|
+
if (inline.noNewLine) {
|
|
84
|
+
for (let i = 0, l = nextInlines.length; i < l; i++) {
|
|
85
|
+
let nextInline = nextInlines[i];
|
|
86
|
+
inlineWidth += nextInline.width;
|
|
87
|
+
inlineTrailingCut += nextInline.trailingCut || 0;
|
|
88
|
+
if (!nextInline.noNewLine) {
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return this.inlineWidths + inlineWidth - this.leadingCut - inlineTrailingCut <= this.maxWidth;
|
|
94
|
+
}
|
|
95
|
+
clone() {
|
|
96
|
+
let result = new Line(this.maxWidth);
|
|
97
|
+
for (let key in this) {
|
|
98
|
+
if (this.hasOwnProperty(key)) {
|
|
99
|
+
result[key] = this[key];
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return result;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
var _default = exports.default = Line;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.default = void 0;
|
|
5
|
+
class OutputDocument {
|
|
6
|
+
/**
|
|
7
|
+
* @param {Promise<object>} pdfDocumentPromise
|
|
8
|
+
*/
|
|
9
|
+
constructor(pdfDocumentPromise) {
|
|
10
|
+
this.bufferSize = 1073741824;
|
|
11
|
+
this.pdfDocumentPromise = pdfDocumentPromise;
|
|
12
|
+
this.bufferPromise = null;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @returns {Promise<object>}
|
|
17
|
+
*/
|
|
18
|
+
getStream() {
|
|
19
|
+
return this.pdfDocumentPromise;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @returns {Promise<Buffer>}
|
|
24
|
+
*/
|
|
25
|
+
getBuffer() {
|
|
26
|
+
if (this.bufferPromise === null) {
|
|
27
|
+
this.bufferPromise = new Promise((resolve, reject) => {
|
|
28
|
+
this.getStream().then(stream => {
|
|
29
|
+
let chunks = [];
|
|
30
|
+
let result;
|
|
31
|
+
stream.on('readable', () => {
|
|
32
|
+
let chunk;
|
|
33
|
+
while ((chunk = stream.read(this.bufferSize)) !== null) {
|
|
34
|
+
chunks.push(chunk);
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
stream.on('end', () => {
|
|
38
|
+
result = Buffer.concat(chunks);
|
|
39
|
+
resolve(result);
|
|
40
|
+
});
|
|
41
|
+
stream.end();
|
|
42
|
+
}, result => {
|
|
43
|
+
reject(result);
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
return this.bufferPromise;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* @returns {Promise<string>}
|
|
52
|
+
*/
|
|
53
|
+
getBase64() {
|
|
54
|
+
return new Promise((resolve, reject) => {
|
|
55
|
+
this.getBuffer().then(buffer => {
|
|
56
|
+
resolve(buffer.toString('base64'));
|
|
57
|
+
}, result => {
|
|
58
|
+
reject(result);
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* @returns {Promise<string>}
|
|
65
|
+
*/
|
|
66
|
+
getDataUrl() {
|
|
67
|
+
return new Promise((resolve, reject) => {
|
|
68
|
+
this.getBase64().then(data => {
|
|
69
|
+
resolve('data:application/pdf;base64,' + data);
|
|
70
|
+
}, result => {
|
|
71
|
+
reject(result);
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
var _default = exports.default = OutputDocument;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.default = void 0;
|
|
5
|
+
var _OutputDocument = _interopRequireDefault(require("./OutputDocument"));
|
|
6
|
+
var _fs = _interopRequireDefault(require("fs"));
|
|
7
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
8
|
+
class OutputDocumentServer extends _OutputDocument.default {
|
|
9
|
+
/**
|
|
10
|
+
* @param {string} filename
|
|
11
|
+
* @returns {Promise}
|
|
12
|
+
*/
|
|
13
|
+
write(filename) {
|
|
14
|
+
return new Promise((resolve, reject) => {
|
|
15
|
+
this.getStream().then(stream => {
|
|
16
|
+
stream.pipe(_fs.default.createWriteStream(filename));
|
|
17
|
+
stream.on('end', () => {
|
|
18
|
+
resolve();
|
|
19
|
+
});
|
|
20
|
+
stream.end();
|
|
21
|
+
}, result => {
|
|
22
|
+
reject(result);
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
var _default = exports.default = OutputDocumentServer;
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.default = void 0;
|
|
5
|
+
var _pdfkit = _interopRequireDefault(require("@foliojs-fork/pdfkit"));
|
|
6
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
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
|
+
class PDFDocument extends _pdfkit.default {
|
|
19
|
+
constructor(fonts = {}, images = {}, patterns = {}, attachments = {}, options = {}, virtualfs = null) {
|
|
20
|
+
super(options);
|
|
21
|
+
this.fonts = {};
|
|
22
|
+
this.fontCache = {};
|
|
23
|
+
for (let font in fonts) {
|
|
24
|
+
if (fonts.hasOwnProperty(font)) {
|
|
25
|
+
let fontDef = fonts[font];
|
|
26
|
+
this.fonts[font] = {
|
|
27
|
+
normal: fontDef.normal,
|
|
28
|
+
bold: fontDef.bold,
|
|
29
|
+
italics: fontDef.italics,
|
|
30
|
+
bolditalics: fontDef.bolditalics
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
this.patterns = {};
|
|
35
|
+
for (let pattern in patterns) {
|
|
36
|
+
if (patterns.hasOwnProperty(pattern)) {
|
|
37
|
+
let patternDef = patterns[pattern];
|
|
38
|
+
this.patterns[pattern] = this.pattern(patternDef.boundingBox, patternDef.xStep, patternDef.yStep, patternDef.pattern, patternDef.colored);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
this.images = images;
|
|
42
|
+
this.attachments = attachments;
|
|
43
|
+
this.virtualfs = virtualfs;
|
|
44
|
+
}
|
|
45
|
+
getFontType(bold, italics) {
|
|
46
|
+
return typeName(bold, italics);
|
|
47
|
+
}
|
|
48
|
+
getFontFile(familyName, bold, italics) {
|
|
49
|
+
let type = this.getFontType(bold, italics);
|
|
50
|
+
if (!this.fonts[familyName] || !this.fonts[familyName][type]) {
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
return this.fonts[familyName][type];
|
|
54
|
+
}
|
|
55
|
+
provideFont(familyName, bold, italics) {
|
|
56
|
+
let type = this.getFontType(bold, italics);
|
|
57
|
+
if (this.getFontFile(familyName, bold, italics) === null) {
|
|
58
|
+
throw new Error(`Font '${familyName}' in style '${type}' is not defined in the font section of the document definition.`);
|
|
59
|
+
}
|
|
60
|
+
this.fontCache[familyName] = this.fontCache[familyName] || {};
|
|
61
|
+
if (!this.fontCache[familyName][type]) {
|
|
62
|
+
let def = this.fonts[familyName][type];
|
|
63
|
+
if (!Array.isArray(def)) {
|
|
64
|
+
def = [def];
|
|
65
|
+
}
|
|
66
|
+
if (this.virtualfs && this.virtualfs.existsSync(def[0])) {
|
|
67
|
+
def[0] = this.virtualfs.readFileSync(def[0]);
|
|
68
|
+
}
|
|
69
|
+
this.fontCache[familyName][type] = this.font(...def)._font;
|
|
70
|
+
}
|
|
71
|
+
return this.fontCache[familyName][type];
|
|
72
|
+
}
|
|
73
|
+
provideImage(src) {
|
|
74
|
+
const realImageSrc = src => {
|
|
75
|
+
let image = this.images[src];
|
|
76
|
+
if (!image) {
|
|
77
|
+
return src;
|
|
78
|
+
}
|
|
79
|
+
if (this.virtualfs && this.virtualfs.existsSync(image)) {
|
|
80
|
+
return this.virtualfs.readFileSync(image);
|
|
81
|
+
}
|
|
82
|
+
let index = image.indexOf('base64,');
|
|
83
|
+
if (index < 0) {
|
|
84
|
+
return this.images[src];
|
|
85
|
+
}
|
|
86
|
+
return Buffer.from(image.substring(index + 7), 'base64');
|
|
87
|
+
};
|
|
88
|
+
if (this._imageRegistry[src]) {
|
|
89
|
+
return this._imageRegistry[src];
|
|
90
|
+
}
|
|
91
|
+
let image;
|
|
92
|
+
try {
|
|
93
|
+
image = this.openImage(realImageSrc(src));
|
|
94
|
+
if (!image) {
|
|
95
|
+
throw new Error('No image');
|
|
96
|
+
}
|
|
97
|
+
} catch (error) {
|
|
98
|
+
throw new Error(`Invalid image: ${error.toString()}\nImages dictionary should contain dataURL entries (or local file paths in node.js)`);
|
|
99
|
+
}
|
|
100
|
+
image.embed(this);
|
|
101
|
+
this._imageRegistry[src] = image;
|
|
102
|
+
return image;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* @param {Array} color pdfmake format: [<pattern name>, <color>]
|
|
107
|
+
* @returns {Array} pdfkit format: [<pattern object>, <color>]
|
|
108
|
+
*/
|
|
109
|
+
providePattern(color) {
|
|
110
|
+
if (Array.isArray(color) && color.length === 2) {
|
|
111
|
+
return [this.patterns[color[0]], color[1]];
|
|
112
|
+
}
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
provideAttachment(src) {
|
|
116
|
+
const checkRequired = obj => {
|
|
117
|
+
if (!obj) {
|
|
118
|
+
throw new Error('No attachment');
|
|
119
|
+
}
|
|
120
|
+
if (!obj.src) {
|
|
121
|
+
throw new Error('The "src" key is required for attachments');
|
|
122
|
+
}
|
|
123
|
+
return obj;
|
|
124
|
+
};
|
|
125
|
+
if (typeof src === 'object') {
|
|
126
|
+
return checkRequired(src);
|
|
127
|
+
}
|
|
128
|
+
let attachment = checkRequired(this.attachments[src]);
|
|
129
|
+
if (this.virtualfs && this.virtualfs.existsSync(attachment.src)) {
|
|
130
|
+
return this.virtualfs.readFileSync(attachment.src);
|
|
131
|
+
}
|
|
132
|
+
return attachment;
|
|
133
|
+
}
|
|
134
|
+
setOpenActionAsPrint() {
|
|
135
|
+
let printActionRef = this.ref({
|
|
136
|
+
Type: 'Action',
|
|
137
|
+
S: 'Named',
|
|
138
|
+
N: 'Print'
|
|
139
|
+
});
|
|
140
|
+
this._root.data.OpenAction = printActionRef;
|
|
141
|
+
printActionRef.end();
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
var _default = exports.default = PDFDocument;
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.default = void 0;
|
|
5
|
+
var _ElementWriter = _interopRequireDefault(require("./ElementWriter"));
|
|
6
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
7
|
+
/**
|
|
8
|
+
* An extended ElementWriter which can handle:
|
|
9
|
+
* - page-breaks (it adds new pages when there's not enough space left),
|
|
10
|
+
* - repeatable fragments (like table-headers, which are repeated everytime
|
|
11
|
+
* a page-break occurs)
|
|
12
|
+
* - transactions (used for unbreakable-blocks when we want to make sure
|
|
13
|
+
* whole block will be rendered on the same page)
|
|
14
|
+
*/
|
|
15
|
+
class PageElementWriter extends _ElementWriter.default {
|
|
16
|
+
constructor(context) {
|
|
17
|
+
super(context);
|
|
18
|
+
this.transactionLevel = 0;
|
|
19
|
+
this.repeatables = [];
|
|
20
|
+
}
|
|
21
|
+
addLine(line, dontUpdateContextPosition, index) {
|
|
22
|
+
return this._fitOnPage(() => super.addLine(line, dontUpdateContextPosition, index));
|
|
23
|
+
}
|
|
24
|
+
addImage(image, index) {
|
|
25
|
+
return this._fitOnPage(() => super.addImage(image, index));
|
|
26
|
+
}
|
|
27
|
+
addCanvas(image, index) {
|
|
28
|
+
return this._fitOnPage(() => super.addCanvas(image, index));
|
|
29
|
+
}
|
|
30
|
+
addSVG(image, index) {
|
|
31
|
+
return this._fitOnPage(() => super.addSVG(image, index));
|
|
32
|
+
}
|
|
33
|
+
addQr(qr, index) {
|
|
34
|
+
return this._fitOnPage(() => super.addQr(qr, index));
|
|
35
|
+
}
|
|
36
|
+
addAttachment(attachment, index) {
|
|
37
|
+
return this._fitOnPage(() => super.addAttachment(attachment, index));
|
|
38
|
+
}
|
|
39
|
+
addVector(vector, ignoreContextX, ignoreContextY, index) {
|
|
40
|
+
return super.addVector(vector, ignoreContextX, ignoreContextY, index);
|
|
41
|
+
}
|
|
42
|
+
beginClip(width, height) {
|
|
43
|
+
return super.beginClip(width, height);
|
|
44
|
+
}
|
|
45
|
+
endClip() {
|
|
46
|
+
return super.endClip();
|
|
47
|
+
}
|
|
48
|
+
addFragment(fragment, useBlockXOffset, useBlockYOffset, dontUpdateContextPosition) {
|
|
49
|
+
return this._fitOnPage(() => super.addFragment(fragment, useBlockXOffset, useBlockYOffset, dontUpdateContextPosition));
|
|
50
|
+
}
|
|
51
|
+
moveToNextPage(pageOrientation) {
|
|
52
|
+
let nextPage = this.context().moveToNextPage(pageOrientation);
|
|
53
|
+
|
|
54
|
+
// moveToNextPage is called multiple times for table, because is called for each column
|
|
55
|
+
// and repeatables are inserted only in the first time. If columns are used, is needed
|
|
56
|
+
// call for table in first column and then for table in the second column (is other repeatables).
|
|
57
|
+
this.repeatables.forEach(function (rep) {
|
|
58
|
+
if (rep.insertedOnPages[this.context().page] === undefined) {
|
|
59
|
+
rep.insertedOnPages[this.context().page] = true;
|
|
60
|
+
this.addFragment(rep, true);
|
|
61
|
+
} else {
|
|
62
|
+
this.context().moveDown(rep.height);
|
|
63
|
+
}
|
|
64
|
+
}, this);
|
|
65
|
+
this.emit('pageChanged', {
|
|
66
|
+
prevPage: nextPage.prevPage,
|
|
67
|
+
prevY: nextPage.prevY,
|
|
68
|
+
y: this.context().y
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
beginUnbreakableBlock(width, height) {
|
|
72
|
+
if (this.transactionLevel++ === 0) {
|
|
73
|
+
this.originalX = this.context().x;
|
|
74
|
+
this.pushContext(width, height);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
commitUnbreakableBlock(forcedX, forcedY) {
|
|
78
|
+
if (--this.transactionLevel === 0) {
|
|
79
|
+
let unbreakableContext = this.context();
|
|
80
|
+
this.popContext();
|
|
81
|
+
let nbPages = unbreakableContext.pages.length;
|
|
82
|
+
if (nbPages > 0) {
|
|
83
|
+
// no support for multi-page unbreakableBlocks
|
|
84
|
+
let fragment = unbreakableContext.pages[0];
|
|
85
|
+
fragment.xOffset = forcedX;
|
|
86
|
+
fragment.yOffset = forcedY;
|
|
87
|
+
|
|
88
|
+
//TODO: vectors can influence height in some situations
|
|
89
|
+
if (nbPages > 1) {
|
|
90
|
+
// on out-of-context blocs (headers, footers, background) height should be the whole DocumentContext height
|
|
91
|
+
if (forcedX !== undefined || forcedY !== undefined) {
|
|
92
|
+
fragment.height = unbreakableContext.getCurrentPage().pageSize.height - unbreakableContext.pageMargins.top - unbreakableContext.pageMargins.bottom;
|
|
93
|
+
} else {
|
|
94
|
+
fragment.height = this.context().getCurrentPage().pageSize.height - this.context().pageMargins.top - this.context().pageMargins.bottom;
|
|
95
|
+
for (let i = 0, l = this.repeatables.length; i < l; i++) {
|
|
96
|
+
fragment.height -= this.repeatables[i].height;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
} else {
|
|
100
|
+
fragment.height = unbreakableContext.y;
|
|
101
|
+
}
|
|
102
|
+
if (forcedX !== undefined || forcedY !== undefined) {
|
|
103
|
+
super.addFragment(fragment, true, true, true);
|
|
104
|
+
} else {
|
|
105
|
+
this.addFragment(fragment);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
currentBlockToRepeatable() {
|
|
111
|
+
let unbreakableContext = this.context();
|
|
112
|
+
let rep = {
|
|
113
|
+
items: []
|
|
114
|
+
};
|
|
115
|
+
unbreakableContext.pages[0].items.forEach(item => {
|
|
116
|
+
rep.items.push(item);
|
|
117
|
+
});
|
|
118
|
+
rep.xOffset = this.originalX;
|
|
119
|
+
|
|
120
|
+
//TODO: vectors can influence height in some situations
|
|
121
|
+
rep.height = unbreakableContext.y;
|
|
122
|
+
rep.insertedOnPages = [];
|
|
123
|
+
return rep;
|
|
124
|
+
}
|
|
125
|
+
pushToRepeatables(rep) {
|
|
126
|
+
this.repeatables.push(rep);
|
|
127
|
+
}
|
|
128
|
+
popFromRepeatables() {
|
|
129
|
+
this.repeatables.pop();
|
|
130
|
+
}
|
|
131
|
+
_fitOnPage(addFct) {
|
|
132
|
+
let position = addFct();
|
|
133
|
+
if (!position) {
|
|
134
|
+
this.moveToNextPage();
|
|
135
|
+
position = addFct();
|
|
136
|
+
}
|
|
137
|
+
return position;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
var _default = exports.default = PageElementWriter;
|
package/js/PageSize.js
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.normalizePageMargin = normalizePageMargin;
|
|
5
|
+
exports.normalizePageSize = normalizePageSize;
|
|
6
|
+
var _standardPageSizes = _interopRequireDefault(require("./standardPageSizes"));
|
|
7
|
+
var _variableType = require("./helpers/variableType");
|
|
8
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
9
|
+
function normalizePageSize(pageSize, pageOrientation) {
|
|
10
|
+
function isNeedSwapPageSizes(pageOrientation) {
|
|
11
|
+
if ((0, _variableType.isString)(pageOrientation)) {
|
|
12
|
+
pageOrientation = pageOrientation.toLowerCase();
|
|
13
|
+
return pageOrientation === 'portrait' && size.width > size.height || pageOrientation === 'landscape' && size.width < size.height;
|
|
14
|
+
}
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
function pageSizeToWidthAndHeight(pageSize) {
|
|
18
|
+
if ((0, _variableType.isString)(pageSize)) {
|
|
19
|
+
let size = _standardPageSizes.default[pageSize.toUpperCase()];
|
|
20
|
+
if (!size) {
|
|
21
|
+
throw new Error(`Page size ${pageSize} not recognized`);
|
|
22
|
+
}
|
|
23
|
+
return {
|
|
24
|
+
width: size[0],
|
|
25
|
+
height: size[1]
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
return pageSize;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// if pageSize.height is set to auto, set the height to infinity so there are no page breaks.
|
|
32
|
+
if (pageSize && pageSize.height === 'auto') {
|
|
33
|
+
pageSize.height = Infinity;
|
|
34
|
+
}
|
|
35
|
+
let size = pageSizeToWidthAndHeight(pageSize || 'A4');
|
|
36
|
+
if (isNeedSwapPageSizes(pageOrientation)) {
|
|
37
|
+
// swap page sizes
|
|
38
|
+
size = {
|
|
39
|
+
width: size.height,
|
|
40
|
+
height: size.width
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
size.orientation = size.width > size.height ? 'landscape' : 'portrait';
|
|
44
|
+
return size;
|
|
45
|
+
}
|
|
46
|
+
function normalizePageMargin(margin) {
|
|
47
|
+
if ((0, _variableType.isNumber)(margin)) {
|
|
48
|
+
margin = {
|
|
49
|
+
left: margin,
|
|
50
|
+
right: margin,
|
|
51
|
+
top: margin,
|
|
52
|
+
bottom: margin
|
|
53
|
+
};
|
|
54
|
+
} else if (Array.isArray(margin)) {
|
|
55
|
+
if (margin.length === 2) {
|
|
56
|
+
margin = {
|
|
57
|
+
left: margin[0],
|
|
58
|
+
top: margin[1],
|
|
59
|
+
right: margin[0],
|
|
60
|
+
bottom: margin[1]
|
|
61
|
+
};
|
|
62
|
+
} else if (margin.length === 4) {
|
|
63
|
+
margin = {
|
|
64
|
+
left: margin[0],
|
|
65
|
+
top: margin[1],
|
|
66
|
+
right: margin[2],
|
|
67
|
+
bottom: margin[3]
|
|
68
|
+
};
|
|
69
|
+
} else {
|
|
70
|
+
throw new Error('Invalid pageMargins definition');
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return margin;
|
|
74
|
+
}
|