forked from jurgelenas/pdfify-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
265 lines (238 loc) · 7.75 KB
/
server.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
'use strict';
const ghostTown = require('ghost-town');
const phantomjs = require('phantomjs-prebuilt');
let config = {
port: process.env.PORT || 3000,
htmlSizeLimit: process.env.HTML_SIZE_LIMIT || '2mb',
workerCount: process.env.WORKER_COUNT || 2,
workerDeath: process.env.WORKER_DEATH || 20,
pageDeath: process.env.PAGE_DEATH || 8000
};
let town = ghostTown({
workerCount: config.workerCount,
workerDeath: config.workerDeath,
pageDeath: config.pageDeath,
phantomBinary: phantomjs.path
});
if (town.isMaster) {
const express = require('express');
const logger = require('morgan');
const bodyParser = require('body-parser');
const http = require('http');
let app = express();
app.use(logger('dev'));
app.set('port', config.port);
app.use(bodyParser.json({ limit: config.htmlSizeLimit }));
app.use(bodyParser.urlencoded({
extended: true,
limit: config.htmlSizeLimit
}));
app.get('/', (req, res) => {
res.json({
message: 'HTML to PDF service.'
});
});
let fillMissingFields = (data) => {
data.format = data.format || 'binary'; // or base64
if (! (data.paperWidth && data.paperHeight) && ! data.paperSize) {
data.paperSize = 'A4'; // 'A3', 'A4', 'A5', 'Legal', 'Letter', 'Tabloid'
}
data.orientation = data.orientation || 'portrait'; // or horizontal
data.zoomFactor = data.zoomFactor || 1;
let defaultMargin = '0';
let defaultPaperMargin = {
left: defaultMargin,
right: defaultMargin,
top: defaultMargin,
bottom: defaultMargin
};
data.paperMargin = data.paperMargin || defaultPaperMargin;
data.paperMargin.left = data.paperMargin.left || defaultMargin;
data.paperMargin.right = data.paperMargin.right || defaultMargin;
data.paperMargin.top = data.paperMargin.top || defaultMargin;
data.paperMargin.bottom = data.paperMargin.bottom || defaultMargin;
data.javascriptEnabled = data.javascriptEnabled || true;
data.loadImages = data.loadImages || true;
data.resourceTimeout = data.resourceTimeout || 10000; // in ms
let defaultUA = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36';
data.userAgent = data.userAgent || defaultUA;
return data;
};
let queuePdf = (req, res) => {
let data = {};
if (req.is('application/json')) {
data = req.body || {};
} else {
data = {
format: req.query.format,
orientation: req.query.orientation,
zoomFactor: req.query.zoomFactor,
html: req.query.html,
url: req.query.url,
paperSize: req.query.paperSize,
paperWidth: req.query.paperWidth,
paperHeight: req.query.paperHeight,
userAgent: req.query.userAgent,
paperMargin: {
left: req.query.paperMarginLeft,
right: req.query.paperMarginRight,
top: req.query.paperMarginTop,
bottom: req.query.paperMarginBottom
},
javascriptEnabled: req.query.javascriptEnabled,
loadImages: req.query.loadImages,
resourceTimeout: req.query.resourceTimeout,
};
}
data = fillMissingFields(data);
town.queue(data, (err, pdf) => {
if (err) {
return res.status(400).json({
message: err,
error: true
});
}
if (data.format === 'base64') {
res.status(200).json({
success: true,
pdf: new Buffer(pdf, 'base64').toString('base64')
});
} else if (data.format === 'binary') {
pdf = new Buffer(pdf);
res.writeHead(200, {
'Content-Type': 'application/pdf',
'Content-Length': pdf.length
});
res.end(pdf);
}
});
};
app.get('/convert', queuePdf);
app.post('/convert', queuePdf);
// catch 404 and forward to error handler
app.use((req, res, next) => {
let err = new Error('Not Found');
err.status = 404;
next(err, null);
});
// error handler
app.use((err, req, res) => {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
let status = err.status || 500;
res.status(status).json({
error: status,
message: err.message
});
});
// Create server
var server = http.createServer(app);
server.listen(config.port);
server.on('error', onError);
server.on('listening', onListening);
} else {
const uuid = require('node-uuid');
const fs = require('fs');
town.on('queue', (page, data, next) => {
let tmpFile = '/tmp/' + uuid.v4();
page.setting('javascriptEnabled', data.javascriptEnabled).then(() => {
return page.setting('loadImages', data.loadImages);
}).then(() => {
return page.setting('userAgent', data.userAgent);
}).then(() => {
return page.setting('resourceTimeout', data.resourceTimeout);
}).then(() => {
let paperProperties = {
orientation: data.orientation,
margin: data.paperMargin
};
if (data.paperSize) {
paperProperties.format = data.paperSize;
} else if (data.paperWidth && data.paperHeight) {
paperProperties.width = data.paperWidth;
paperProperties.height = data.paperHeight;
}
return page.property('paperSize', paperProperties);
}).then(() => {
return page.property('onResourceError', function (resourceError){
console.log('Unable to load resource (#' + resourceError.id + 'URL:' + resourceError.url + ')');
console.log('Error code: ' + resourceError.errorCode + '. Description: ' + resourceError.errorString);
})
})
.then(() => {
if (data.url && data.url.length) {
return page.open(data.url);
} else {
return page.open('data:text/html,' + data.html, function (status){
window.setTimeout(function (){
console.log('waiting 10 seconds for js download...');
}, 10000);
});
}
}).then((status) => {
if (status === 'fail') {
return Promise.reject('Failed to load the given URL.');
}
// We can use here only ES5 since.
// page.evaluate passes function to PhantomJs.
return page.evaluate(function(zoomFactor) {
var rule = 'body { -webkit-print-color-adjust: exact !important; zoom: ' + zoomFactor + '; }'
var sheet = window.document.styleSheets[0];
if (sheet) {
console.log('Style sheet found!');
var index = 0;
if (sheet.cssRules && sheet.cssRules.length){
index = sheet.cssRules.length;
}
sheet.insertRule(rule, index);
}
}, data.zoomFactor);
}).then(() => {
return page.render(tmpFile, {
format: 'pdf'
});
}).then(() => {
fs.readFile(tmpFile, (err, pdf) => {
fs.unlink(tmpFile, () => {});
if (err) {
return next(err, null);
}
next(null, pdf);
});
}).catch((err) => {
next(err, null);
});
});
}
// Event listener for HTTP server "error" event.
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
let bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
// Event listener for HTTP server "listening" event.
function onListening() {
let addr = server.address();
let bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
console.log('Listening on ' + bind);
}