diff --git a/README.md b/README.md index 3c1c73f..338336d 100644 --- a/README.md +++ b/README.md @@ -45,11 +45,11 @@ pdf.create(html).toFile([filepath, ]function(err, res){ console.log(res.filename); }); -pdf.create(html).toStream(function(err, stream){ +pdf.create(html).toStream(function(err, stream, res){ stream.pipe(fs.createWriteStream('./foo.pdf')); }); -pdf.create(html).toBuffer(function(err, buffer){ +pdf.create(html).toBuffer(function(err, buffer, res){ console.log('This is a buffer:', Buffer.isBuffer(buffer)); }); @@ -132,6 +132,7 @@ config = { "phantomArgs": [], // array of strings used as phantomjs args e.g. ["--ignore-ssl-errors=yes"] "script": '/url', // Absolute path to a custom phantomjs script, use the file in lib/scripts as example "timeout": 30000, // Timeout that will cancel phantomjs, in milliseconds + "evaluateScript": "function () { console.log(document.title) }", // Evaluate a script in the context of the web page (console.log are collected in consoleMessages field in the JSON returned by the callback: res) // HTTP Headers that are used for requests "httpHeaders": { diff --git a/bin/index.js b/bin/index.js index d8b7661..1c0d62c 100755 --- a/bin/index.js +++ b/bin/index.js @@ -28,5 +28,6 @@ function htmlpdf (source, destination) { } pdf.create(html, options).toFile(destination, function (err, res) { if (err) throw err + if (res) console.log(JSON.stringify(res)) }) } diff --git a/lib/pdf.js b/lib/pdf.js index 9195e7f..abd7fe5 100644 --- a/lib/pdf.js +++ b/lib/pdf.js @@ -47,7 +47,8 @@ PDF.prototype.toBuffer = function PdfToBuffer (callback) { if (err) return callback(err) fs.unlink(res.filename, function unlinkPdfFile (err) { if (err) return callback(err) - callback(null, buffer) + delete res.filename + callback(null, buffer, res) }) }) }) @@ -62,13 +63,15 @@ PDF.prototype.toStream = function PdfToStream (callback) { return callback(err) } + var filename = res.filename stream.on('end', function () { - fs.unlink(res.filename, function (err) { + fs.unlink(filename, function (err) { if (err) console.log('html-pdf:', err) }) }) - callback(null, stream) + delete res.filename + callback(null, stream, res) }) } diff --git a/lib/scripts/pdf_a4_portrait.js b/lib/scripts/pdf_a4_portrait.js index a9197c4..78f7ee6 100755 --- a/lib/scripts/pdf_a4_portrait.js +++ b/lib/scripts/pdf_a4_portrait.js @@ -57,6 +57,15 @@ page.onLoadFinished = function (status) { // The paperSize object must be set at once page.paperSize = definePaperSize(getContent(page), options) + // Collect console.log messages in rendered web page and custom script + var consoleMessages = [] + page.onConsoleMessage = function (msg) { + consoleMessages.push(msg) + } + + // Evaluate custom JavaScript + if (options.evaluateScript) page.evaluateJavaScript(options.evaluateScript) + // Output to parent process var fileOptions = { type: options.type || 'pdf', @@ -65,7 +74,7 @@ page.onLoadFinished = function (status) { var filename = options.filename || (options.directory || '/tmp') + '/html-pdf-' + system.pid + '.' + fileOptions.type page.render(filename, fileOptions) - system.stdout.write(JSON.stringify({filename: filename})) + system.stdout.write(JSON.stringify({filename: filename, consoleMessages: consoleMessages})) exit(null) }