This repository has been archived by the owner on Oct 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pdfsnag.js
executable file
·140 lines (126 loc) · 4.1 KB
/
pdfsnag.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
#!/usr/bin/env node
var spawn = require('child_process').spawn
var http = require('http')
var url = require('url')
var qs = require('querystring')
var port = process.argv[2] || 3000
var util = require('util')
function Undefined() {}
function StringStripped(value) { return value.replace('$','\\$').replace('&','\\&').replace(';','\\;') }
var acceptableOpts = {
'background': Undefined,
'collate': Undefined,
'copies': Number,
'default-header': Undefined,
'disable-external-links': Undefined,
'disable-forms': Undefined,
'disable-internal-links': Undefined,
'disable-javascript': Undefined,
'disable-smart-shrinking': Undefined,
'disable-toc-back-links': Undefined,
'enable-external-links': Undefined,
'enable-forms': Undefined,
'enable-internal-links': Undefined,
'enable-javascript': Undefined,
'enable-smart-shrinking': Undefined,
'enable-toc-back-links': Undefined,
'encoding': StringStripped,
'exclude-from-outline': Undefined,
'grayscale': Undefined,
'image-dpi': Number,
'image-quality': Number,
'images': Undefined,
'include-in-outline': Undefined,
'javascript-delay': Number,
'load-error-handling': StringStripped,
'lowquality': Undefined,
'margin-bottom': StringStripped,
'margin-left': StringStripped,
'margin-right': StringStripped,
'margin-top': StringStripped,
'minimum-font-size': Number,
'no-background': Undefined,
'no-collate': Undefined,
'no-images': Undefined,
'no-outline': Undefined,
'no-pdf-compression': Undefined,
'no-print-media-type': Undefined,
'no-stop-slow-scripts': Undefined,
'orientation': StringStripped,
'outline': Undefined,
'outline-depth': Number,
'output-format': StringStripped,
'page-height': StringStripped,
'page-offset': Number,
'page-size': StringStripped,
'page-width': StringStripped,
'password': StringStripped,
'print-media-type': Undefined,
'proxy': StringStripped,
'stop-slow-scripts': Undefined,
'title': StringStripped,
'user-style-sheet': StringStripped,
'username': StringStripped,
'zoom': Number,
}
var index = require('fs').readFileSync(__dirname + '/index.html').toString().replace('%OPTIONS%', Object.keys(acceptableOpts))
var forkme = require('fs').readFileSync(__dirname + '/forkme.png')
function afterwards(res, wkhtmltopdf) {
if (!res.finished) {
util.log('error - wkhtmltopdf did not complete in a timely manner, killing')
wkhtmltopdf.stdout.destroy()
wkhtmltopdf.stderr.destroy()
wkhtmltopdf.kill('SIGKILL')
}
}
function getopts(query) {
var opts = []
Object.keys(query).forEach(function(opt) {
var Type = acceptableOpts[opt]
if (Type) {
opts.push('--' + opt)
if (Type != Undefined) opts.push(Type(query[opt]))
}
})
return opts
}
function snagit(query, res) {
query.html || (query.html = '')
query.url || (query.url = null)
query.name || (query.name = 'output')
var opts = getopts(query)
res.writeHead(200, {
'Content-Type': 'application/pdf',
'Content-Disposition': 'attachment; filename=' + query.name + '.pdf',
})
var wkhtmltopdf = spawn('/bin/sh', ['-c', 'wkhtmltopdf ' + opts.join(' ') + ' ' + (query.html ? '-' : '"' + StringStripped(encodeURI(decodeURI(query.url))) + '"') + ' - | cat'])
if (query.html) wkhtmltopdf.stdin.end(query.html)
wkhtmltopdf.stdout.pipe(res)
util.log('info - ' + query.name + ' ' + (query.url || '"' + query.html.substr(25, 50).replace(/\n|\r/g,'') + '"') + ' ' + JSON.stringify(opts))
setTimeout(afterwards, 30000, res, wkhtmltopdf)
}
http.createServer(function(req, res) {
var query = url.parse(req.url, true).query
if (req.method === 'POST') {
req.setEncoding('utf-8')
var body = ''
req.on('data', function(chunk) {
body += chunk
})
req.on('end', function() {
body = qs.parse(body)
for (var i in body) query[i] = body[i]
snagit(query, res)
})
} else if (!query.html && !query.url) {
if (req.url == '/forkme.png') {
res.writeHead(200, {'Content-Type': 'image/png'})
res.end(forkme)
}
else {
res.writeHead(200, {'Content-Type': 'text/html'})
res.end(index)
}
}
else snagit(query, res)
}).listen(port)