forked from ovvn/dom-to-pdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
298 lines (282 loc) · 7.75 KB
/
index.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
let _cloneNode;
let _createElement;
let _isCanvasBlank;
let downloadPdf;
const domToImage = require('dom-to-image-more');
const { jsPDF } = require('jspdf');
_cloneNode = (node, javascriptEnabled) => {
let child;
let clone;
clone = node.nodeType === 3 ? document.createTextNode(node.nodeValue) : node.cloneNode(false);
child = node.firstChild;
while (child) {
if (javascriptEnabled === true || child.nodeType !== 1 || child.nodeName !== 'SCRIPT') {
clone.appendChild(_cloneNode(child, javascriptEnabled));
}
child = child.nextSibling;
}
if (node.nodeType === 1) {
if (node.nodeName === 'CANVAS') {
clone.width = node.width;
clone.height = node.height;
clone.getContext('2d').drawImage(node, 0, 0);
} else if (node.nodeName === 'TEXTAREA' || node.nodeName === 'SELECT') {
clone.value = node.value;
}
clone.addEventListener('load', (() => {
clone.scrollTop = node.scrollTop;
clone.scrollLeft = node.scrollLeft;
}), true);
}
return clone;
};
_createElement = (tagName, {className, innerHTML, style}) => {
let el;
let i;
let key;
let scripts;
el = document.createElement(tagName);
if (className) {
el.className = className;
}
if (innerHTML) {
el.innerHTML = innerHTML;
scripts = el.getElementsByTagName('script');
i = scripts.length;
while (i-- > 0) {
scripts[i].parentNode.removeChild(scripts[i]);
}
}
for (key in style) {
el.style[key] = style[key];
}
return el;
};
_isCanvasBlank = canvas => {
let blank;
let ctx;
blank = document.createElement('canvas');
blank.width = canvas.width;
blank.height = canvas.height;
ctx = blank.getContext('2d');
ctx.fillStyle = '#FFFFFF';
ctx.fillRect(0, 0, blank.width, blank.height);
return canvas.toDataURL() === blank.toDataURL();
};
downloadPdf = (dom, options, cb) => {
const a4Height = 841.89;
const a4Width = 595.28;
let overrideWidth;
let container;
let containerCSS;
let containerWidth;
let elements;
let excludeClassNames;
let excludeTagNames;
let filename;
let filterFn;
let innerRatio;
let overlay;
let overlayCSS;
let pageHeightPx;
let proxyUrl;
let compression = 'NONE';
let scale;
let opts;
let offsetHeight;
let offsetWidth;
let scaleObj;
let style;
const transformOrigin = 'top left';
const pdfOptions = {
orientation: 'p',
unit: 'pt',
format: 'a4'
};
({filename, excludeClassNames = [], excludeTagNames = ['button', 'input', 'select'], overrideWidth, proxyUrl, compression, scale, domToImageOptions = {}} = options);
overlayCSS = {
position: 'fixed',
zIndex: 1000,
opacity: 0,
left: 0,
right: 0,
bottom: 0,
top: 0,
backgroundColor: 'rgba(0,0,0,0.8)'
};
if (overrideWidth) {
overlayCSS.width = `${overrideWidth}px`;
}
containerCSS = {
position: 'absolute',
left: 0,
right: 0,
top: 0,
height: 'auto',
margin: 'auto',
overflow: 'auto',
backgroundColor: 'white'
};
overlay = _createElement('div', {
style: overlayCSS
});
container = _createElement('div', {
style: containerCSS
});
container.appendChild(_cloneNode(dom));
overlay.appendChild(container);
document.body.appendChild(overlay);
innerRatio = a4Height / a4Width;
containerWidth = overrideWidth || container.getBoundingClientRect().width;
pageHeightPx = Math.floor(containerWidth * innerRatio);
elements = container.querySelectorAll('*');
for (let i = 0, len = excludeClassNames.length; i < len; i++) {
const clName = excludeClassNames[i];
container.querySelectorAll(`.${clName}`).forEach(function(a) {
return a.remove();
});
}
for (let j = 0, len1 = excludeTagNames.length; j < len1; j++) {
const tName = excludeTagNames[j];
let els = container.getElementsByTagName(tName);
for (let k = els.length - 1; k >= 0; k--) {
if (!els[k]) {
continue;
}
els[k].parentNode.removeChild(els[k]);
}
}
Array.prototype.forEach.call(elements, el => {
let clientRect;
let endPage;
let nPages;
let pad;
let rules;
let startPage;
rules = {
before: false,
after: false,
avoid: true
};
clientRect = el.getBoundingClientRect();
if (rules.avoid && !rules.before) {
startPage = Math.floor(clientRect.top / pageHeightPx);
endPage = Math.floor(clientRect.bottom / pageHeightPx);
nPages = Math.abs(clientRect.bottom - clientRect.top) / pageHeightPx;
// Turn on rules.before if the el is broken and is at most one page long.
if (endPage !== startPage && nPages <= 1) {
rules.before = true;
}
// Before: Create a padding div to push the element to the next page.
if (rules.before) {
pad = _createElement('div', {
style: {
display: 'block',
height: `${pageHeightPx - clientRect.top % pageHeightPx}px`
}
});
return el.parentNode.insertBefore(pad, el);
}
}
});
// Remove unnecessary elements from result pdf
filterFn = ({classList, tagName}) => {
let cName;
let j;
let len;
let ref;
if (classList) {
for (j = 0, len = excludeClassNames.length; j < len; j++) {
cName = excludeClassNames[j];
if (Array.prototype.indexOf.call(classList, cName) >= 0) {
return false;
}
}
}
ref = tagName != null ? tagName.toLowerCase() : undefined;
return excludeTagNames.indexOf(ref) < 0;
};
opts = {
filter: filterFn,
proxy: proxyUrl,
...domToImageOptions
};
if (scale) {
offsetWidth = container.offsetWidth;
offsetHeight = container.offsetHeight;
style = {
transform: 'scale(' + scale + ')',
transformOrigin: transformOrigin,
width: offsetWidth + 'px',
height: offsetHeight + 'px'
};
scaleObj = {
width: offsetWidth * scale,
height: offsetHeight * scale,
quality: 1,
style: style
};
opts = Object.assign(opts, scaleObj);
}
return domToImage.toCanvas(container, opts).then(canvas => {
let h;
let imgData;
let nPages;
let page;
let pageCanvas;
let pageCtx;
let pageHeight;
let pdf;
let pxFullHeight;
let w;
// Remove overlay
document.body.removeChild(overlay);
// Initialize the PDF.
pdf = new jsPDF(pdfOptions);
// Calculate the number of pages.
pxFullHeight = canvas.height;
nPages = Math.ceil(pxFullHeight / pageHeightPx);
// Define pageHeight separately so it can be trimmed on the final page.
pageHeight = a4Height;
pageCanvas = document.createElement('canvas');
pageCtx = pageCanvas.getContext('2d');
pageCanvas.width = canvas.width;
pageCanvas.height = pageHeightPx;
page = 0;
while (page < nPages) {
if (page === nPages - 1 && pxFullHeight % pageHeightPx !== 0) {
pageCanvas.height = pxFullHeight % pageHeightPx;
pageHeight = pageCanvas.height * a4Width / pageCanvas.width;
}
w = pageCanvas.width;
h = pageCanvas.height;
pageCtx.fillStyle = 'white';
pageCtx.fillRect(0, 0, w, h);
pageCtx.drawImage(canvas, 0, page * pageHeightPx, w, h, 0, 0, w, h);
// Don't create blank pages
if (_isCanvasBlank(pageCanvas)) {
++page;
continue;
}
// Add the page to the PDF.
if (page) {
pdf.addPage();
}
imgData = pageCanvas.toDataURL('image/PNG');
pdf.addImage(imgData, 'PNG', 0, 0, a4Width, pageHeight, undefined, compression);
++page;
}
if (typeof cb === "function") {
cb(pdf);
}
return pdf.save(filename);
}).catch(error => {
// Remove overlay
document.body.removeChild(overlay);
if (typeof cb === "function") {
cb(null);
}
return console.error(error);
});
};
module.exports = downloadPdf;