-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathexport4.js
More file actions
194 lines (152 loc) · 6.66 KB
/
export4.js
File metadata and controls
194 lines (152 loc) · 6.66 KB
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
const puppeteer = require('puppeteer');
const util = require('./util.js');
const fs = require('fs');
const dir = './appdata';
const merge = require('easy-pdf-merge');
const puppeteerParams = {
headless: true,
args: ['--no-sandbox', '--disable-setuid-sandbox']
};
function export4(path, bodies, callback){
if (!fs.existsSync(dir)){
fs.mkdirSync(dir);
}
util.delFiles(dir);
(async () => {
const browser = await puppeteer.launch(puppeteerParams);
const page = await browser.newPage();
var pagesForMerge = [];
var currentPageIndex = 0;
var totalPagesCount = 0;
for(var body of bodies){
totalPagesCount += body.pages.length;
}
for(var body of bodies){
for(var i = 0; i < body.pages.length; i++){
currentPageIndex++;
var currentPage = body.pages[i];
var vb = currentPage.vb;
var header = body.options.header;
var footer = body.options.footer;
if (currentPage.header){
header = currentPage.header;
}
if (header){
header = header.replace('{current-page}', currentPageIndex).replace('{total-pages}', totalPagesCount);
}
if (currentPage.footer){
footer = currentPage.footer;
}
if (footer){
footer = footer.replace('{current-page}', currentPageIndex).replace('{total-pages}', totalPagesCount);
}
var marginTop = body.options.margin[0];
var marginBottom = body.options.margin[2];
var backgroundColor = currentPage.backgroundColor;
if (backgroundColor == undefined){
backgroundColor = '';
}
var content = currentPage.html ? currentPage.html : body.content;
var styles = body.styles ? body.styles : '';
var html = exportHtml(content + styles, body.options, currentPage.innerSize.w, currentPage.innerSize.h, header, footer);
var htmlPath = util.pageHtmlPath(__dirname, dir, path.href);
fs.writeFileSync(htmlPath.path, html);
await page.goto(htmlPath.url, { waitUntil: 'networkidle2' });
await page.evaluate((data) => {
var svg = document.querySelector('svg');
if (svg && data.vb){
svg.setAttribute("viewBox", data.vb);
}
if (svg && svg.style.backgroundColor){
document.documentElement.style.backgroundColor = svg.style.backgroundColor;
}
if (data.backgroundColor){
document.documentElement.style.backgroundColor = data.backgroundColor;
}
var bgheader = document.getElementById('bg-header');
var bgfooter = document.getElementById('bg-footer');
if (bgheader){
var top = data.marginTop - bgheader.offsetHeight - 7;
bgheader.style.top = top + 'px';
}
if (bgfooter){
var bottom = data.marginBottom - bgfooter.offsetHeight - 7;
bgfooter.style.bottom = bottom + 'px';
}
}, {vb, header, footer, marginTop, marginBottom, backgroundColor});
if (body.options.ext == "pdf"){
var pagepath = util.pagePdfPath(__dirname, dir);
pagesForMerge.push(pagepath);
await page.pdf({
printBackground: true,
path: pagepath,
pageRanges: '1',
timeout: 180000, //3 minutes timeout
margin: {
top: 0,
bottom: 0,
left: 0,
right: 0
},
width : currentPage.size.w + 'px',
height : currentPage.size.h + 'px'
});
}
else if (body.options.ext == "png") {
await page.screenshot({
path: path.targetpath,
printBackground: true,
timeout: 180000,
//fullPage: true
clip : {
x : 0,
y : 0,
width : parseFloat(currentPage.size.w),
height : parseFloat(currentPage.size.h)
}
});
}
};
}
if (pagesForMerge.length > 1){
merge(pagesForMerge, path.targetpath, function(err){
if(err)
return console.log(err);
callback();
});
}
else if (pagesForMerge.length == 1){
fs.copyFile(pagesForMerge[0], path.targetpath, (err) => {
if (err) throw err;
callback();
});
}
else{
callback();
}
browser.close();
})();
}
function exportHtml(html, options, w, h, header, footer){
var smargin = '';
for(var j = 0; j < options.margin.length; j++){
smargin += (options.margin[j] + 'px ');
}
var mode = '';
if (options.mode){
mode = options.mode;
}
var result = '<!DOCTYPE html><html style="margin:0;padding:0;"><head></head><body class="export-service ' + mode + '" style="margin:0; padding:0;">'
+ '<div style="margin: ' + smargin + ';overflow:hidden;width:' + w + 'px;height:' + (h) + 'px">';
if (header){
result += '<div id="bg-header" style="width:' + w + 'px;color:#757575;position:absolute;left:' + options.margin[3] + 'px;top:0;">' + header + '</div>';
}
result += html;
if (footer){
result += '<div id="bg-footer" style="width:' + w + 'px;color:#757575;position:absolute;left:' + options.margin[3] + 'px;bottom:0;">' + footer + '</div>';
}
result += '</div>';
result += '</body></html>';
return result;
}
module.exports = export4;