Skip to content

Commit

Permalink
Merge pull request #47 from witty-pigeon/master
Browse files Browse the repository at this point in the history
Fix bug when using PhantomJS version 2.x+
  • Loading branch information
TJkrusinski committed Apr 15, 2016
2 parents 48d5d40 + f241209 commit 6bb6ea0
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions render.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
var page = require('webpage').create();
var system = require('system');

// ADDRESS CHANGE IN PARAMETERS HANDLING FOR PHANTOMJS 2.x
var phantomargs;
if(phantom.version.major >= 2) {
// remove the script name from the arguments
phantomargs = system.args.slice(1,system.args.length);
} else {
phantomargs = phantom.args;
}

var contentsCb = function(pobj) {
if (!pobj || !pobj.contents) return;
var contentStr = pobj.contents;
pobj.contents = phantom.callback(function(currentPage, pages) {
return contentStr.replace(/\{currentPage\}/g, currentPage).replace(/\{pages\}/g, pages);
});
}
};

if (phantom.args.length < 2) {
if (phantomargs.length < 2) {
console.log('11');
console.log('incorrect args');
phantom.exit();
} else {

try {
var encodedOpts = atob(phantom.args[2]);
var encodedOpts = atob(phantomargs[2]);
}
catch (e) {
console.log("Options are not base64 encoded correctly");
Expand Down Expand Up @@ -44,7 +54,7 @@ if (phantom.args.length < 2) {
}
}

if (!options.content) page.open(phantom.args[0]);
if (!options.content) page.open(phantomargs[0]);

page.onLoadFinished = function(status) {
if(status !== 'success'){
Expand All @@ -53,7 +63,7 @@ if (phantom.args.length < 2) {
phantom.exit();
} else {
window.setTimeout(function(){
page.render(phantom.args[1], { format: 'pdf', quality: options.outputQuality || '80' });
page.render(phantomargs[1], { format: 'pdf', quality: options.outputQuality || '80' });
console.log('success');
phantom.exit();
}, options.captureDelay || 400);
Expand Down

0 comments on commit 6bb6ea0

Please sign in to comment.