forked from bpampuch/pdfmake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrinter.js
216 lines (189 loc) · 6.68 KB
/
Printer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import PDFDocument from './PDFDocument';
import LayoutBuilder from './LayoutBuilder';
import SVGMeasure from './SVGMeasure';
import { normalizePageSize, normalizePageMargin } from './PageSize';
import { tableLayouts } from './tableLayouts';
import Renderer from './Renderer';
import { isNumber, isValue } from './helpers/variableType';
/**
* Printer which turns document definition into a pdf
*
* @example
* var fontDescriptors = {
* Roboto: {
* normal: 'fonts/Roboto-Regular.ttf',
* bold: 'fonts/Roboto-Medium.ttf',
* italics: 'fonts/Roboto-Italic.ttf',
* bolditalics: 'fonts/Roboto-MediumItalic.ttf'
* }
* };
*
* var printer = new PdfPrinter(fontDescriptors);
*/
class PdfPrinter {
/**
* @param {object} fontDescriptors font definition dictionary
* @param {object} virtualfs
* @param {object} urlResolver
*/
constructor(fontDescriptors, virtualfs = null, urlResolver = null) {
this.fontDescriptors = fontDescriptors;
this.virtualfs = virtualfs;
this.urlResolver = urlResolver;
}
/**
* Executes layout engine for the specified document and renders it into a pdfkit document
* ready to be saved.
*
* @param {object} docDefinition
* @param {object} options
* @returns {Promise<PDFDocument>} resolved promise return a pdfkit document
*/
createPdfKitDocument(docDefinition, options = {}) {
return new Promise((resolve, reject) => {
this.resolveUrls(docDefinition).then(() => {
try {
docDefinition.version = docDefinition.version || '1.3';
docDefinition.compress = typeof docDefinition.compress === 'boolean' ? docDefinition.compress : true;
docDefinition.images = docDefinition.images || {};
docDefinition.pageMargins = isValue(docDefinition.pageMargins) ? docDefinition.pageMargins : 40;
let pageSize = normalizePageSize(docDefinition.pageSize, docDefinition.pageOrientation);
let pdfOptions = {
size: [pageSize.width, pageSize.height],
pdfVersion: docDefinition.version,
compress: docDefinition.compress,
userPassword: docDefinition.userPassword,
ownerPassword: docDefinition.ownerPassword,
permissions: docDefinition.permissions,
fontLayoutCache: typeof options.fontLayoutCache === 'boolean' ? options.fontLayoutCache : true,
bufferPages: options.bufferPages || false,
autoFirstPage: false,
font: null
};
this.pdfKitDoc = new PDFDocument(this.fontDescriptors, docDefinition.images, pdfOptions, this.virtualfs);
setMetadata(docDefinition, this.pdfKitDoc);
const builder = new LayoutBuilder(pageSize, normalizePageMargin(docDefinition.pageMargins), new SVGMeasure());
builder.registerTableLayouts(tableLayouts);
if (options.tableLayouts) {
builder.registerTableLayouts(options.tableLayouts);
}
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);
let maxNumberPages = docDefinition.maxPagesNumber || -1;
if (isNumber(maxNumberPages) && maxNumberPages > -1) {
pages = pages.slice(0, maxNumberPages);
}
// if pageSize.height is set to Infinity, calculate the actual height of the page that
// was laid out using the height of each of the items in the page.
if (pageSize.height === Infinity) {
let pageHeight = calculatePageHeight(pages, docDefinition.pageMargins);
this.pdfKitDoc.options.size = [pageSize.width, pageHeight];
}
const renderer = new Renderer(this.pdfKitDoc, options.progressCallback);
renderer.renderPages(pages);
resolve(this.pdfKitDoc);
} catch (e) {
reject(e);
}
}, result => {
reject(result);
});
});
}
/**
* @param {object} docDefinition
* @returns {Promise}
*/
resolveUrls(docDefinition) {
return new Promise((resolve, reject) => {
if (this.urlResolver === null) {
resolve();
}
for (let font in this.fontDescriptors) {
if (this.fontDescriptors.hasOwnProperty(font)) {
if (this.fontDescriptors[font].normal) {
this.urlResolver.resolve(this.fontDescriptors[font].normal);
}
if (this.fontDescriptors[font].bold) {
this.urlResolver.resolve(this.fontDescriptors[font].bold);
}
if (this.fontDescriptors[font].italics) {
this.urlResolver.resolve(this.fontDescriptors[font].italics);
}
if (this.fontDescriptors[font].bolditalics) {
this.urlResolver.resolve(this.fontDescriptors[font].bolditalics);
}
}
}
if (docDefinition.images) {
for (let image in docDefinition.images) {
if (docDefinition.images.hasOwnProperty(image)) {
this.urlResolver.resolve(docDefinition.images[image]);
}
}
}
this.urlResolver.resolved().then(() => {
resolve();
}, result => {
reject(result);
});
});
}
}
function setMetadata(docDefinition, pdfKitDoc) {
// PDF standard has these properties reserved: Title, Author, Subject, Keywords,
// Creator, Producer, CreationDate, ModDate, Trapped.
// To keep the pdfmake api consistent, the info field are defined lowercase.
// Custom properties don't contain a space.
function standardizePropertyKey(key) {
let standardProperties = ['Title', 'Author', 'Subject', 'Keywords',
'Creator', 'Producer', 'CreationDate', 'ModDate', 'Trapped'];
let standardizedKey = key.charAt(0).toUpperCase() + key.slice(1);
if (standardProperties.includes(standardizedKey)) {
return standardizedKey;
}
return key.replace(/\s+/g, '');
}
pdfKitDoc.info.Producer = 'pdfmake';
pdfKitDoc.info.Creator = 'pdfmake';
if (docDefinition.info) {
for (let key in docDefinition.info) {
let value = docDefinition.info[key];
if (value) {
key = standardizePropertyKey(key);
pdfKitDoc.info[key] = value;
}
}
}
}
function calculatePageHeight(pages, margins) {
function getItemHeight(item) {
if (typeof item.item.getHeight === 'function') {
return item.item.getHeight();
} else if (item.item._height) {
return item.item._height;
} else if (item.type === 'vector') {
return item.item.y1 > item.item.y2 ? item.item.y1 : item.item.y2;
} else {
// TODO: add support for next item types
return 0;
}
}
function getBottomPosition(item) {
let top = item.item.y || 0;
let height = getItemHeight(item);
return top + height;
}
let fixedMargins = normalizePageMargin(margins || 40);
let height = fixedMargins.top;
pages.forEach(page => {
page.items.forEach(item => {
let bottomPosition = getBottomPosition(item);
if (bottomPosition > height) {
height = bottomPosition;
}
});
});
height += fixedMargins.bottom;
return height;
}
export default PdfPrinter;