phantom-html-to-pdf
Version:
Convert html to pdf using phantomjs
86 lines (68 loc) • 2.33 kB
JavaScript
var Phantom = require("phantom-workers"),
fs = require("fs"),
_ = require("lodash");
var phantoms = {};
function ensurePhantom(phantom, cb) {
if (phantom.started)
return cb();
phantom.startCb = phantom.startCb || [];
phantom.startCb.push(cb);
if (phantom.starting)
return;
phantom.starting = true;
phantom.start(function(startErr) {
phantom.started = true;
phantom.starting = false;
phantom.startCb.forEach(function(cb) { cb(startErr); })
});
}
module.exports = function(options, requestOptions, id, cb) {
var phantomInstanceId = requestOptions.phantomPath || options.phantomPath || "default";
if (!phantoms[phantomInstanceId]) {
var opts = _.extend({}, options);
opts.workerEnv = {
'PHANTOM_MAX_LOG_ENTRY_SIZE': options.maxLogEntrySize || 1000,
'PHANTOM_HTTPS_RESOURCE_PROXY_URL': options.httpsResourceProxyUrl
}
opts.phantomPath = requestOptions.phantomPath || options.phantomPath;
phantoms[phantomInstanceId] = Phantom(opts);
}
var phantom = phantoms[phantomInstanceId];
ensurePhantom(phantom, function(err) {
if (err)
return cb(err);
phantom.execute(requestOptions, function (err, res) {
if (err) {
// if the error is a timeout from phantom-workers
if (err.message === "Timeout") {
err.phantomTimeout = true;
}
return cb(err);
}
if (res.isError) {
var error = new Error(res.message);
error.stack = res.stack;
return cb(error);
}
res.logs.forEach(function(m) {
m.timestamp = new Date(m.timestamp)
})
cb(null, {
stream: fs.createReadStream(requestOptions.output),
numberOfPages: res.numberOfPages,
logs: res.logs
});
});
})
};
module.exports.kill = function() {
Object.keys(phantoms).forEach(function(key) {
var phantom = phantoms[key]
if (!phantom.started)
return;
phantom.started = false;
phantom.startCb = [];
return phantom.kill();
});
phantoms = {}
}