-
Notifications
You must be signed in to change notification settings - Fork 538
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
02748ee
commit d454deb
Showing
10 changed files
with
480 additions
and
775 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
# Development Files | ||
src/ | ||
test/ | ||
|
||
# Example Files | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,26 @@ | ||
// Generated by CoffeeScript 1.10.0 | ||
(function() { | ||
var PDF; | ||
var PDF = require('./pdf') | ||
|
||
PDF = require('./pdf'); | ||
|
||
exports.create = function(html, options, callback) { | ||
var err, error, pdf; | ||
module.exports = { | ||
create: function createPdf (html, options, callback) { | ||
if (arguments.length === 1) { | ||
return new PDF(html); | ||
return new PDF(html) | ||
} | ||
|
||
if (arguments.length === 2 && typeof options !== 'function') { | ||
return new PDF(html, options); | ||
return new PDF(html, options) | ||
} | ||
|
||
if (arguments.length === 2) { | ||
callback = options; | ||
options = {}; | ||
callback = options | ||
options = {} | ||
} | ||
|
||
try { | ||
pdf = new PDF(html, options); | ||
} catch (error) { | ||
err = error; | ||
return callback(err); | ||
var pdf = new PDF(html, options) | ||
} catch (err) { | ||
return callback(err) | ||
} | ||
return pdf.exec(callback); | ||
}; | ||
|
||
}).call(this); | ||
pdf.exec(callback) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,146 +1,127 @@ | ||
// Generated by CoffeeScript 1.10.0 | ||
(function() { | ||
var PDF, Stream, assert, childprocess, err, error, fs, path, phantomjs; | ||
var fs = require('fs') | ||
var childprocess = require('child_process') | ||
var path = require('path') | ||
var assert = require('assert') | ||
|
||
fs = require('fs'); | ||
try { | ||
var phantomjs = require('phantomjs') | ||
} catch (err) { | ||
console.log('html-pdf: Failed to load PhantomJS module.', err) | ||
} | ||
|
||
Stream = require('stream').Readable; | ||
/* | ||
* 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 || [] | ||
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) || 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) | ||
}) | ||
}) | ||
}) | ||
} | ||
|
||
childprocess = require('child_process'); | ||
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) | ||
} | ||
|
||
path = require('path'); | ||
stream.on('end', function () { | ||
fs.unlink(res.filename, function (err) { | ||
if (err) console.log('html-pdf:', err) | ||
}) | ||
}) | ||
|
||
assert = require('assert'); | ||
callback(null, stream) | ||
}) | ||
} | ||
|
||
try { | ||
phantomjs = require('phantomjs'); | ||
} catch (error) { | ||
err = error; | ||
console.log('html-pdf: Failed to load PhantomJS module.', err); | ||
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) | ||
} | ||
|
||
module.exports = PDF = (function() { | ||
function PDF(html, options) { | ||
var base, base1; | ||
this.html = html; | ||
this.options = options != null ? 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 ((base = this.options).phantomPath == null) { | ||
base.phantomPath = phantomjs != null ? phantomjs.path : void 0; | ||
} | ||
if ((base1 = this.options).phantomArgs == null) { | ||
base1.phantomArgs = []; | ||
} | ||
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) || 30000; | ||
PDF.prototype.exec = function PdfExec (callback) { | ||
var child = childprocess.spawn(this.options.phantomPath, [].concat(this.options.phantomArgs, [this.script])) | ||
var stdout = [] | ||
var stderr = [] | ||
var timeout = setTimeout(function execTimeout () { | ||
child.stdin.end() | ||
child.kill() | ||
if (!stderr.length) { | ||
stderr = [new Buffer('html-pdf: PDF generation timeout. Phantom.js script did not exit.')] | ||
} | ||
}, this.options.timeout) | ||
|
||
PDF.prototype.toBuffer = function(callback) { | ||
return this.exec(function(err, res) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
return fs.readFile(res.filename, function(err, buffer) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
return fs.unlink(res.filename, function(err) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
return callback(null, buffer); | ||
}); | ||
}); | ||
}); | ||
}; | ||
child.stdout.on('data', function (buffer) { | ||
return stdout.push(buffer) | ||
}) | ||
|
||
PDF.prototype.toStream = function(callback) { | ||
return this.exec(function(err, res) { | ||
var error1, stream; | ||
if (err) { | ||
return callback(err); | ||
} | ||
try { | ||
stream = fs.createReadStream(res.filename); | ||
} catch (error1) { | ||
err = error1; | ||
return callback(err); | ||
} | ||
stream.on('end', function() { | ||
return fs.unlink(res.filename, function(err) { | ||
if (err) { | ||
return console.log('html-pdf:', err); | ||
} | ||
}); | ||
}); | ||
return callback(null, stream); | ||
}); | ||
}; | ||
child.stderr.on('data', function (buffer) { | ||
stderr.push(buffer) | ||
child.stdin.end() | ||
return child.kill() | ||
}) | ||
|
||
PDF.prototype.toFile = function(filename, callback) { | ||
assert(arguments.length > 0, 'html-pdf: The method .toFile([filename, ]callback) requires a callback.'); | ||
if (filename instanceof Function) { | ||
callback = filename; | ||
filename = void 0; | ||
} else { | ||
this.options.filename = path.resolve(filename); | ||
child.on('exit', function (code) { | ||
clearTimeout(timeout) | ||
if (code || stderr.length) { | ||
var err = new Error(Buffer.concat(stderr).toString() || 'html-pdf: Unknown Error') | ||
return callback(err) | ||
} else { | ||
try { | ||
var buff = Buffer.concat(stdout).toString() | ||
var data = (buff) != null ? buff.trim() : undefined | ||
data = JSON.parse(data) | ||
} catch (err) { | ||
return callback(err) | ||
} | ||
return this.exec(callback); | ||
}; | ||
|
||
PDF.prototype.exec = function(callback) { | ||
var child, stderr, stdout, timeout; | ||
child = childprocess.spawn(this.options.phantomPath, [].concat(this.options.phantomArgs, [this.script])); | ||
stdout = []; | ||
stderr = []; | ||
timeout = setTimeout(function() { | ||
child.stdin.end(); | ||
child.kill(); | ||
if (!stderr.length) { | ||
return stderr = [new Buffer('html-pdf: PDF generation timeout. Phantom.js script did not exit.')]; | ||
} | ||
}, this.options.timeout); | ||
child.stdout.on('data', function(buffer) { | ||
return stdout.push(buffer); | ||
}); | ||
child.stderr.on('data', function(buffer) { | ||
stderr.push(buffer); | ||
child.stdin.end(); | ||
return child.kill(); | ||
}); | ||
child.on('exit', function(code) { | ||
var data, error1, ref; | ||
clearTimeout(timeout); | ||
if (code || stderr.length) { | ||
err = new Error(Buffer.concat(stderr).toString() || 'html-pdf: Unknown Error'); | ||
return callback(err); | ||
} else { | ||
try { | ||
data = (ref = Buffer.concat(stdout).toString()) != null ? ref.trim() : void 0; | ||
data = JSON.parse(data); | ||
} catch (error1) { | ||
err = error1; | ||
return callback(err); | ||
} | ||
return callback(null, data); | ||
} | ||
}); | ||
return child.stdin.write(JSON.stringify({ | ||
html: this.html, | ||
options: this.options | ||
}) + '\n', 'utf8'); | ||
}; | ||
|
||
return PDF; | ||
|
||
})(); | ||
return callback(null, data) | ||
} | ||
}) | ||
|
||
}).call(this); | ||
var res = JSON.stringify({html: this.html, options: this.options}) | ||
return child.stdin.write(res + '\n', 'utf8') | ||
} |
Oops, something went wrong.