Skip to content

Evaluate script option #184

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});

Expand Down Expand Up @@ -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": {
Expand Down
1 change: 1 addition & 0 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
}
9 changes: 6 additions & 3 deletions lib/pdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
})
Expand All @@ -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)
})
}

Expand Down
11 changes: 10 additions & 1 deletion lib/scripts/pdf_a4_portrait.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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)
}
Expand Down