-
Notifications
You must be signed in to change notification settings - Fork 528
Expand file tree
/
Copy pathpdf.js
More file actions
166 lines (141 loc) · 5.48 KB
/
pdf.js
File metadata and controls
166 lines (141 loc) · 5.48 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
var fs = require('fs')
var childprocess = require('child_process')
var path = require('path')
var assert = require('assert')
try {
var phantomjs = require('phantomjs-prebuilt')
} catch (err) {
console.log('html-pdf: Failed to load PhantomJS module.', err)
}
/*
* phantomjs version 1.8.1 and later should work.
*
* Create a PDF file out of an html string.
*
* Regions for the PDF page are:
*
* - Page Header -> document.getElementById('pageHeader')
* - Page Content -> document.getElementById('pageContent')
* - Page Footer -> document.getElementById('pageFooter')
*
* When no #pageContent is available, phantomjs will use document.body as pdf content
*/
module.exports = PDF
function PDF (html, options) {
this.html = html
this.options = options || {}
if (this.options.script) {
this.script = path.normalize(this.options.script)
} else {
this.script = path.join(__dirname, 'scripts', 'pdf_a4_portrait.js')
}
if (this.options.filename) this.options.filename = path.resolve(this.options.filename)
if (!this.options.phantomPath) this.options.phantomPath = phantomjs && phantomjs.path
this.options.phantomArgs = this.options.phantomArgs || []
if (!this.options.localUrlAccess) this.options.phantomArgs.push('--local-url-access=false')
assert(this.options.phantomPath, "html-pdf: Failed to load PhantomJS module. You have to set the path to the PhantomJS binary using 'options.phantomPath'")
assert(typeof this.html === 'string' && this.html.length, "html-pdf: Can't create a pdf without an html string")
this.options.timeout = parseInt(this.options.timeout, 10) || 30000
}
PDF.prototype.toBuffer = function PdfToBuffer (callback) {
this.exec(function execPdfToBuffer (err, res) {
if (err) return callback(err)
fs.readFile(res.filename, function readCallback (err, buffer) {
if (err) return callback(err)
fs.unlink(res.filename, function unlinkPdfFile (err) {
if (err) return callback(err)
callback(null, buffer)
})
})
})
}
PDF.prototype.toStream = function PdfToStream (callback) {
this.exec(function (err, res) {
if (err) return callback(err)
try {
var stream = fs.createReadStream(res.filename)
} catch (err) {
return callback(err)
}
stream.on('end', function () {
fs.unlink(res.filename, function unlinkPdfFile (err) {
if (err) console.log('html-pdf:', err)
})
})
callback(null, stream)
})
}
PDF.prototype.toFile = function PdfToFile (filename, callback) {
assert(arguments.length > 0, 'html-pdf: The method .toFile([filename, ]callback) requires a callback.')
if (filename instanceof Function) {
callback = filename
filename = undefined
} else {
this.options.filename = path.resolve(filename)
}
this.exec(callback)
}
PDF.prototype.exec = function PdfExec (callback) {
var child = childprocess.spawn(this.options.phantomPath, [].concat(this.options.phantomArgs, [this.script]), this.options.childProcessOptions)
var stderr = []
var timeout = setTimeout(function execTimeout () {
respond(null, new Error('html-pdf: PDF generation timeout. Phantom.js script did not exit.'))
}, this.options.timeout)
function onError (buffer) {
stderr.push(buffer)
}
function onData (buffer) {
var result
try {
var json = buffer.toString().trim()
if (json) result = JSON.parse(json)
} catch (err) {
// Proxy for debugging purposes
process.stdout.write(buffer)
}
if (result) respond(null, null, result)
}
var callbacked = false
function respond (code, err, data) {
if (callbacked) return
callbacked = true
clearTimeout(timeout)
// If we don't have an exit code, we kill the process, ignore stderr after this point
if (code === null) kill(child, onData, onError)
// Since code has a truthy/falsy value of either 0 or 1, check for existence first.
// Ignore if code has a value of 0 since that means PhantomJS has executed and exited successfully.
// Also, as per your script and standards, having a code value of 1 means one can always assume that
// an error occured.
if (((typeof code !== 'undefined' && code !== null) && code !== 0) || err || (code === 0 && !data)) {
var error = null
if (err) {
// Rudimentary checking if err is an instance of the Error class
error = err instanceof Error ? err : new Error(err)
} else if (code === 0 && !data) {
// This is to catch the edge case of having a exit code value of 0 but having no data (exit can be called before io pipes are closed)
error = new Error('html-pdf: Process exited successfully, but no data received')
} else {
// This is to catch the edge case of having a exit code value of 1 but having no error
error = new Error('html-pdf: Unknown Error')
}
// Append anything caught from the stderr
var postfix = stderr.length ? '\n' + Buffer.concat(stderr).toString() : ''
if (postfix) error.message += postfix
return callback(error)
}
callback(null, data)
}
child.stdout.on('data', onData)
child.stderr.on('data', onError)
child.on('error', function onError (err) { respond(null, err) })
// An exit event is most likely an error because we didn't get any data at this point
child.on('close', respond)
// child.on('exit', respond)
var config = JSON.stringify({html: this.html, options: this.options})
child.stdin.write(config + '\n', 'utf8')
child.stdin.end()
}
function kill (child, onData, onError) {
child.stdin.end()
child.kill()
}