node-html-pdf
HTML to PDF converter that uses phantomjs
![image](https://github.com/marcbachmann/node-html-pdf/raw/HEAD/examples/businesscard/businesscard.png)
Example Business Card
-> and its Source file
Example Receipt
Changelog
Have a look at the releases page: https://github.com/marcbachmann/node-html-pdf/releases
Installation
Install the html-pdf utility via npm:
$ npm install -g html-pdf
Command-line example
$ html-pdf test/businesscard.html businesscard.pdf
Code example
var fs = require('fs');
var pdf = require('html-pdf');
var html = fs.readFileSync('./test/businesscard.html', 'utf8');
var options = { format: 'Letter' };
pdf.create(html, options).toFile('./businesscard.pdf', function(err, res) {
if (err) return console.log(err);
console.log(res);
});
API
var pdf = require('html-pdf');
pdf.create(html).toFile([filepath, ]function(err, res){
console.log(res.filename);
});
pdf.create(html).toStream(function(err, stream){
stream.pipe(fs.createWriteStream('./foo.pdf'));
});
pdf.create(html).toBuffer(function(err, buffer){
console.log('This is a buffer:', Buffer.isBuffer(buffer));
});
pdf.create(html [, options], function(err, buffer){});
Footers and Headers
html-pdf
can read the header or footer either out of the footer
and header
config object or out of the html source. You can either set a default header & footer or overwrite that by appending a page number (1 based index) to the id="pageHeader"
attribute of a html tag.
You can use any combination of those tags. The library tries to find any element, that contains the pageHeader
or pageFooter
id prefix.
<div id="pageHeader">Default header</div>
<div id="pageHeader-first">Header on first page</div>
<div id="pageHeader-2">Header on second page</div>
<div id="pageHeader-3">Header on third page</div>
<div id="pageHeader-last">Header on last page</div>
...
<div id="pageFooter">Default footer</div>
<div id="pageFooter-first">Footer on first page</div>
<div id="pageFooter-2">Footer on second page</div>
<div id="pageFooter-last">Footer on last page</div>
Options
config = {
"directory": "/tmp",
"height": "10.5in",
"width": "8in",
- or -
"format": "Letter",
"orientation": "portrait",
"border": "0",
- or -
"border": {
"top": "2in",
"right": "1in",
"bottom": "2in",
"left": "1.5in"
},
paginationOffset: 1,
"header": {
"height": "45mm",
"contents": '<div style="text-align: center;">Author: Marc Bachmann</div>'
},
"footer": {
"height": "28mm",
"contents": {
first: 'Cover page',
2: 'Second page',
default: '<span style="color: #444;">{{page}}</span>/<span>{{pages}}</span>',
last: 'Last Page'
}
},
"base": "file:///home/www/your-asset-path/",
"zoomFactor": "1",
"type": "pdf",
"quality": "75",
"phantomPath": "./node_modules/phantomjs/bin/phantomjs",
"phantomArgs": [],
"localUrlAccess": false,
"script": '/url',
"timeout": 30000,
"renderDelay": 1000,
"httpHeaders": {
"Authorization": "Bearer ACEFAD8C-4B4D-4042-AB30-6C735F5BAC8B"
},
"childProcessOptions": {
"detached": true
}
"httpCookies": [
{
"name": "Valid-Cookie-Name",
"value": "Valid-Cookie-Value",
"domain": "localhost",
"path": "/foo",
"httponly": true,
"secure": false,
"expires": (new Date()).getTime() + (1000 * 60 * 60)
}
]
}
The full options object gets converted to JSON and will get passed to the phantomjs script as third argument.
There are more options concerning the paperSize, header & footer options inside the phantomjs script.