Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/unlooped/NodePDF into unloo…
Browse files Browse the repository at this point in the history
…ped-master
  • Loading branch information
TJkrusinski committed Jan 23, 2017
2 parents 401d91a + a892892 commit d206a7d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 44 deletions.
20 changes: 17 additions & 3 deletions child.js
Expand Up @@ -3,6 +3,7 @@
var child = require('child_process');
var shq = require('shell-quote').quote;
var which = process.platform == 'win32' ? 'where' : 'which';
var fs = require('fs');

/**
* Execute the command
Expand All @@ -15,17 +16,30 @@ var which = process.platform == 'win32' ? 'where' : 'which';
exports.exec = function(url, filename, options, cb){
var key;
var stdin = ['phantomjs'];
var optsFile = __dirname + '/' + Date.now() + '.js';

stdin.push(options.args);
stdin.push(shq([
__dirname+'/render.js',
url,
filename,
new Buffer(JSON.stringify(options)).toString('base64')
optsFile,
]));

return child.exec(stdin.join(' '), function(err, stdo, stde){
cb ? cb(err) : null;
fs.writeFile(optsFile, 'module.exports='+JSON.stringify(options), 'UTF-8', function(err) {
if(err) {
return console.log(err);
}

var c = child.exec(stdin.join(' '));
c.on('exit', function () {
fs.unlinkSync(optsFile);
});

c.on('error', function () {
fs.unlinkSync(optsFile);
});
cb(c);
});
};

Expand Down
48 changes: 24 additions & 24 deletions index.js
@@ -1,6 +1,6 @@
'use strict';

var child = require('./child.js');
var child = require('./child.js');
var Emitter = require('events').EventEmitter;

var defaults = {
Expand Down Expand Up @@ -85,26 +85,26 @@ Pdf.prototype = Object.create(Emitter.prototype);

Pdf.prototype.run = function() {
var self = this;
var ps = child.exec(this.url, this.fileName, this.options);

ps.on('exit', function(c, d){
if (c != 0) return self.emit('error', 'PDF conversion failed with exit of '+c);
child.exec(this.url, this.fileName, this.options, function (ps) {
ps.on('exit', function(c, d){
if (c != 0) return self.emit('error', 'PDF conversion failed with exit of '+c);

var targetFilePath = self.fileName;
var targetFilePath = self.fileName;

if (targetFilePath[0] != '/') {
targetFilePath = self.filePath + '/' + targetFilePath;
};
if (targetFilePath[0] != '/') {
targetFilePath = self.filePath + '/' + targetFilePath;
};

self.emit('done', targetFilePath);
});
self.emit('done', targetFilePath);
});

ps.stdout.on('data', function(std){
self.emit('stdout', std);
});
ps.stdout.on('data', function(std){
self.emit('stdout', std);
});

ps.stderr.on('data', function(std){
self.emit('stderr', std);
ps.stderr.on('data', function(std){
self.emit('stderr', std);
});
});
};

Expand All @@ -130,17 +130,17 @@ exports.render = function(address, file, options, callback) {
child.supports(function(support){
if (!support) callback(new Error('PhantomJS not installed'));

var ps = child.exec(address, file, options);

ps.on('exit', function(c, d){
if (c) return callback(new Error('Conversion failed with exit of '+c));
child.exec(address, file, options, function (ps) {
ps.on('exit', function(c, d){
if (c) return callback(new Error('Conversion failed with exit of '+c));

var targetFilePath = file;
var targetFilePath = file;

if (targetFilePath[0] != '/')
targetFilePath = filePath + '/' + targetFilePath;
if (targetFilePath[0] != '/')
targetFilePath = filePath + '/' + targetFilePath;

return callback(null, targetFilePath);
return callback(null, targetFilePath);
});
});
});
};
18 changes: 2 additions & 16 deletions render.js
Expand Up @@ -31,22 +31,8 @@ if (args.length < 2) {
console.log('incorrect args');
phantom.exit();
} else {

try {
var encodedOpts = atob(args[2]);
}
catch (e) {
console.log("Options are not base64 encoded correctly");
phantom.exit();
}

try {
var options = JSON.parse(encodedOpts);
}
catch (e) {
console.log("Options are not valid JSON");
phantom.exit();
}
var optionsFile = args[3];
var options = require(optionsFile);

contentsCb(options.paperSize.header);
contentsCb(options.paperSize.footer);
Expand Down
3 changes: 2 additions & 1 deletion tests/pdf.js
Expand Up @@ -44,6 +44,7 @@ describe('pdf#done() 1', function(){

describe('pdf#content', function() {
it('fires done when content is loaded', function(d) {
this.timeout(20000);
var pdf1 = new Pdf(null, 'html.pdf', {
'content': '<html><body>Test</body></html>'
});
Expand Down Expand Up @@ -113,7 +114,7 @@ describe('pdf#done() 2', function(){

describe('pdf#render()', function(){
it('renders a pdf with a callback style', function(d){
this.timeout(5000);
this.timeout(20000);
Pdf.render('http://www.google.com', 'google2.pdf', function(err, file){
assert.equal(err, null);
assert.equal(FP + '/google2.pdf', file);
Expand Down

0 comments on commit d206a7d

Please sign in to comment.