function convert(options, callback) { var options = options || {}, html = options.html || '<p>No HTML source specified!</p>', css = options.css || '', js = options.js || '', runnings = options.runnings || '', deleteOnAction = options.deleteOnAction || false; /* Create temporary files for PDF, HTML, CSS and JS storage * We need to wait for all of them to finish creating the files before proceeding. */ async.series([ function(callback) { createTempFile(".html", html, callback); }, /* PDF file is necessary for further access within Node */ function(callback) { createTempFile(".pdf", "", callback); }, /* Create optional CSS injection file */ function(callback) { (css) ? createTempFile(".css", css, callback) : callback(null, null); }, /* Create optional JS injection file */ function(callback) { (js) ? createTempFile(".js", js, callback) : callback(null, null); }, /* Create runnings (JSON header, footer) file */ function(callback) { (runnings) ? createTempFile(".runnings", runnings, callback) : callback(null, "nofile"); }, ], /* err/results-Structure * [0] = HTML temp file * [1] = PDF temp file * [2] = CSS temp file * [3] = JS temp file * [4] = Runnings temp file */ function(err, results) { var paperFormat = "A4"; var paperOrientation = "portrait"; var paperBorder = "1cm"; var renderDelay = 500; /* All necessary files have been created. * Construct arguments and create a new phantom process. */ var childArgs = [ path.join(__dirname, "phantom-script.js"), results[0], results[1], results[2], results[3], results[4], paperFormat, paperOrientation, paperBorder, renderDelay ]; childProcess.execFile(phantom.path, childArgs, function(err, stdout, stderr) { var opPointer = new PDFResult(err, results[1]); if (typeof callback === "function") { callback(opPointer); } }); }); }
...
## Conversion API
The API exposes a single function 'convert'. Using this function, you can input a multitude of settings, which are further
specified below:
```` javascript
var pdf = require('phantomjs-pdf');
pdf.convert(options, function(result) {
/* Using a buffer and callback */
result.toBuffer(function(returnedBuffer) {});
/* Using a readable stream */
var stream = result.toStream();
...
function PDFResult(err, tmpPath, deleteOnAction) { this._err = err; this._tmpPath = tmpPath; this._deleteOnAction = deleteOnAction; this._invalid = false; }
n/a
function PDFResult(err, tmpPath, deleteOnAction) { this._err = err; this._tmpPath = tmpPath; this._deleteOnAction = deleteOnAction; this._invalid = false; }
n/a
deleteTmpFile = function () { var resultObject = this; fs.unlink(this._tmpPath, function (err) { if (err) throw err; debug('Successfully deleted "%s"', resultObject._tmpPath); resultObject._tmpPath = ''; resultObject._invalid = true; }); }
...
stream.on('data', function(data) {
buffers.push(data);
});
stream.on('end', function() {
buffer = Buffer.concat(buffers);
if (resultObject._deleteOnAction) { resultObject.deleteTmpFile(); }
if (typeof callback === "function") { callback(buffer); }
});
};
method.getTmpPath = function() {
return this._tmpPath;
};
...
getTmpPath = function () { return this._tmpPath; }
...
/* Using a buffer and callback */
result.toBuffer(function(returnedBuffer) {});
/* Using a readable stream */
var stream = result.toStream();
/* Using the temp file path */
var tmpPath = result.getTmpPath();
/* Using the file writer and callback */
result.toFile("/path/to/file.pdf", function() {});
});
````
## Options
...
toBuffer = function (callback) { if (this._invalid) throw new Error('PDFResult became invalid after deletion!'); var buffers = []; var buffer; var stream = fs.createReadStream(this._tmpPath); var resultObject = this; stream.on('data', function(data) { buffers.push(data); }); stream.on('end', function() { buffer = Buffer.concat(buffers); if (resultObject._deleteOnAction) { resultObject.deleteTmpFile(); } if (typeof callback === "function") { callback(buffer); } }); }
...
```` javascript
var pdf = require('phantomjs-pdf');
pdf.convert(options, function(result) {
/* Using a buffer and callback */
result.toBuffer(function(returnedBuffer) {});
/* Using a readable stream */
var stream = result.toStream();
/* Using the temp file path */
var tmpPath = result.getTmpPath();
...
toFile = function (path, callback) { if (this._invalid) throw new Error('PDFResult became invalid after deletion!'); var outputStream = fs.createWriteStream(path); var operation = fs.createReadStream(this._tmpPath).pipe(outputStream); var resultObject = this; operation.on('close', function() { if (resultObject._deleteOnAction) { resultObject.deleteTmpFile(); } if (typeof callback === "function") { callback(); } }); }
...
/* Using a readable stream */
var stream = result.toStream();
/* Using the temp file path */
var tmpPath = result.getTmpPath();
/* Using the file writer and callback */
result.toFile("/path/to/file.pdf", function() {});
});
````
## Options
Calling convert() requires an options object, which includes the following definitions:
...
toStream = function () { if (this._invalid) throw new Error('PDFResult became invalid after deletion!'); return fs.createReadStream(this._tmpPath); }
...
pdf.convert(options, function(result) {
/* Using a buffer and callback */
result.toBuffer(function(returnedBuffer) {});
/* Using a readable stream */
var stream = result.toStream();
/* Using the temp file path */
var tmpPath = result.getTmpPath();
/* Using the file writer and callback */
result.toFile("/path/to/file.pdf", function() {});
});
...