forked from gr2m/wkhtmltopdf-node-heroku
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
60 lines (51 loc) · 1.77 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
var util = require('util'),
exec = require('child_process').exec,
fs = require('fs'),
path = require('path'),
port = process.env.PORT || 5315,
http = require('http'),
child;
var wkhtmltopdf_path = process.env.PORT ? './bin/wkhtmltopdf-linux-amd64' : 'wkhtmltopdf';
http.createServer(function (req, res) {
if (req.url == '/') {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('try something like <a href="/news.google.ch/nwshp">/news.google.ch/nwshp</a> ...\n');
return;
};
if (req.url === '/favicon.ico') {
res.writeHead(200, {'Content-Type': 'image/x-icon'} );
res.end();
return;
}
var protocol = req.headers['x-forwarded-proto'] === 'https' ? 'https' : 'http'
pdf_url = req.url.substr(1),
pdf_path = './' +pdf_url.replace(/\//g, '_') + '.pdf';
path.exists(pdf_path, function(exists) {
if (exists) {
fs.readFile(pdf_path, function(error, content) {
if (error) {
res.writeHead(500);
res.end();
}
else {
res.writeHead(200, { 'Content-Type': 'application/pdf' });
res.end(content, 'utf-8');
}
});
}
else {
console.log('generating ' + pdf_path + ' out of ' + pdf_url)
child = exec([wkhtmltopdf_path, '--print-media-type', '--no-background', protocol + '://' + pdf_url, pdf_path].join(' '),
function (error, stdout, stderr) {
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
}
});
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('generating pdf ...\n');
}
});
}).listen(port);
console.log('Server running at ' + port);