UNPKG

5.53 kBMarkdownView Raw
1# node-html-pdf
2## HTML to PDF converter that uses phantomjs
3![image](examples/businesscard.png)
4[Example Business Card](examples/businesscard.pdf)
5 -> [and its Source file](examples/businesscard.html)
6
7[Example Receipt](http://imgr-static.s3-eu-west-1.amazonaws.com/order.pdf)
8
9## Changelog
10
11Have a look at the releases page: https://github.com/marcbachmann/node-html-pdf/releases
12
13## Installation
14
15Install the html-pdf utility via [npm](http://npmjs.org/):
16
17```
18$ npm install -g html-pdf
19```
20
21## Command-line example
22
23```
24$ html-pdf test/businesscard.html businesscard.pdf
25```
26
27## Code example
28```javascript
29var fs = require('fs');
30var pdf = require('html-pdf');
31var html = fs.readFileSync('./test/businesscard.html', 'utf8');
32var options = { format: 'Letter' };
33
34pdf.create(html, options).toFile('./businesscard.pdf', function(err, res) {
35 if (err) return console.log(err);
36 console.log(res); // { filename: '/app/businesscard.pdf' }
37});
38```
39
40## API
41
42```js
43var pdf = require('html-pdf');
44pdf.create(html).toFile([filepath, ]function(err, res){
45 console.log(res.filename);
46});
47
48pdf.create(html).toStream(function(err, stream){
49 stream.pipe(fs.createWriteStream('./foo.pdf'));
50});
51
52pdf.create(html).toBuffer(function(err, buffer){
53 console.log('This is a buffer:', Buffer.isBuffer(buffer));
54});
55
56
57// for backwards compatibility
58// alias to pdf.create(html[, options]).toBuffer(callback)
59pdf.create(html [, options], function(err, buffer){});
60```
61
62### Footers and Headers
63
64`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.
65
66You can use any combination of those tags. The library tries to find any element, that contains the `pageHeader` or `pageFooter` id prefix.
67```html
68<div id="pageHeader">Default header</div>
69<div id="pageHeader-first">Header on first page</div>
70<div id="pageHeader-2">Header on second page</div>
71<div id="pageHeader-3">Header on third page</div>
72<div id="pageHeader-last">Header on last page</div>
73...
74<div id="pageFooter">Default footer</div>
75<div id="pageFooter-first">Footer on first page</div>
76<div id="pageFooter-2">Footer on second page</div>
77<div id="pageFooter-last">Footer on last page</div>
78```
79
80
81## Options
82```javascript
83config = {
84
85 // Export options
86 "directory": "/tmp", // The directory the file gets written into if not using .toFile(filename, callback). default: '/tmp'
87
88 // Papersize Options: http://phantomjs.org/api/webpage/property/paper-size.html
89 "height": "10.5in", // allowed units: mm, cm, in, px
90 "width": "8in", // allowed units: mm, cm, in, px
91 - or -
92 "format": "Letter", // allowed units: A3, A4, A5, Legal, Letter, Tabloid
93 "orientation": "portrait", // portrait or landscape
94
95 // Page options
96 "border": "0", // default is 0, units: mm, cm, in, px
97 - or -
98 "border": {
99 "top": "2in", // default is 0, units: mm, cm, in, px
100 "right": "1in",
101 "bottom": "2in",
102 "left": "1.5in"
103 },
104
105 paginationOffset: 1, // Override the initial pagination number
106 "header": {
107 "height": "45mm",
108 "contents": '<div style="text-align: center;">Author: Marc Bachmann</div>'
109 },
110 "footer": {
111 "height": "28mm",
112 "contents": {
113 first: 'Cover page',
114 2: 'Second page', // Any page number is working. 1-based index
115 default: '<span style="color: #444;">{{page}}</span>/<span>{{pages}}</span>', // fallback value
116 last: 'Last Page'
117 }
118 },
119
120
121 // Rendering options
122 "base": "file:///home/www/your-asset-path", // Base path that's used to load files (images, css, js) when they aren't referenced using a host
123
124 // Zooming option, can be used to scale images if `options.type` is not pdf
125 "zoomFactor": "1", // default is 1
126
127 // File options
128 "type": "pdf", // allowed file types: png, jpeg, pdf
129 "quality": "75", // only used for types png & jpeg
130
131 // Script options
132 "phantomPath": "./node_modules/phantomjs/bin/phantomjs", // PhantomJS binary which should get downloaded automatically
133 "phantomArgs": [], // array of strings used as phantomjs args e.g. ["--ignore-ssl-errors=yes"]
134 "script": '/url', // Absolute path to a custom phantomjs script, use the file in lib/scripts as example
135 "timeout": 30000, // Timeout that will cancel phantomjs, in milliseconds
136
137 // Time we should wait after window load
138 // accepted values are 'manual', some delay in milliseconds or undefined to wait for a render event
139 "renderDelay": 1000,
140
141 // HTTP Headers that are used for requests
142 "httpHeaders": {
143 // e.g.
144 "Authorization": "Bearer ACEFAD8C-4B4D-4042-AB30-6C735F5BAC8B"
145 },
146
147 // To run Node application as Windows service
148 "childProcessOptions": {
149 "detached": true
150 }
151
152 // HTTP Cookies that are used for requests
153 "httpCookies": [
154 // e.g.
155 {
156 "name": "Valid-Cookie-Name", // required
157 "value": "Valid-Cookie-Value", // required
158 "domain": "localhost",
159 "path": "/foo", // required
160 "httponly": true,
161 "secure": false,
162 "expires": (new Date()).getTime() + (1000 * 60 * 60) // e.g. expires in 1 hour
163 }
164 ]
165
166}
167```
168
169The full options object gets converted to JSON and will get passed to the phantomjs script as third argument.
170There are more options concerning the paperSize, header & footer options inside the phantomjs script.