Skip to content

Commit 3ea3cc3

Browse files
committed
Add toBuffer, toStream & toFile methods
1 parent c1dc25f commit 3ea3cc3

9 files changed

Lines changed: 243 additions & 91 deletions

File tree

README.md

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,18 @@
99

1010
```javascript
1111
var fs = require('fs');
12-
var pdf = require('./lib');
12+
var pdf = require('html-pdf');
1313
var html = fs.readFileSync('./test/businesscard.html', 'utf8')
14-
pdf.create(html, { width: '50mm', height: '90mm'}, function(err, buffer) {
14+
var options = { filename: './businesscard.pdf', format: 'Letter' };
15+
pdf(html, options).exec(function(err, res) {
1516
if (err) return console.log(err);
16-
fs.writeFile('businesscard.pdf', buffer);
17+
console.log(res);
18+
/*
19+
{
20+
filename: './businesscard.pdf',
21+
pages: 1
22+
}
23+
*/
1724
});
1825
```
1926

@@ -32,16 +39,19 @@ pdf.create(html [, options], callback)
3239

3340
```javascript
3441
var pdf = require('html-pdf');
35-
var callback = function(err, buffer){}
36-
pdf.create(htmlString, options, callback)
42+
pdf.create(htmlString, options, function(err, res){
43+
console.log(res); // { "filename": "/tmp/path" }
44+
})
3745
```
3846

47+
3948
## Options
4049
```javascript
4150
config = {
42-
// Script options
43-
script: '/url' // Absolute path to a custom phantomjs script, use the file in lib/scripts as example
44-
timeout: 10000 // Timeout that will cancel phantomjs, in milliseconds
51+
52+
// Export options
53+
"filename": "/tmp/html-pdf-123-123.pdf" // The file path of the file that will be written. If you want to save the file permanently, you have to pass this option.
54+
"directory": "/tmp" // The directory the file gets written into if no filename is defined. default: '/tmp'
4555

4656
// Papersize Options: http://phantomjs.org/api/webpage/property/paper-size.html
4757
"height": "", // allowed units: mm, cm, in, px
@@ -50,7 +60,6 @@ config = {
5060
"format": "A4", // allowed units: A3, A4, A5, Legal, Letter, Tabloid
5161
"orientation": "portrait", // portrait or landscape
5262

53-
5463
// Page options
5564
"border": "0" // default is 0, units: mm, cm, in, px
5665
"header": {
@@ -61,18 +70,15 @@ config = {
6170
"height": "28mm",
6271
"contents": '<span style="color: #444;">{{page}}</span>/<span>{{pages}}</span>'
6372
},
64-
6573

6674
// File options
6775
"type": "pdf", // allowed file types: png, jpeg, pdf
6876
"quality": "75", // only used for types png & jpeg
6977

78+
// Script options
79+
script: '/url' // Absolute path to a custom phantomjs script, use the file in lib/scripts as example
80+
timeout: 10000 // Timeout that will cancel phantomjs, in milliseconds
7081

71-
// Export options
72-
"buffer": true, // only supported on certain systems
73-
- or -
74-
"filename": "/tmp/html-pdf-123-123.pdf" // The file path of the file that will be written. If you want to save the file permanently, you have to pass this option.
75-
"directory": "/tmp" // The directory the file gets written into if no filename is defined. default: '/tmp'
7682
}
7783
```
7884

lib/index.coffee

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
PDF = require('./pdf')
22

3-
module.exports = (html, options, callback) ->
3+
exports.create = (html, options, callback) ->
44
if arguments.length == 1
55
return new PDF(html)
66

@@ -17,7 +17,3 @@ module.exports = (html, options, callback) ->
1717
return callback(err)
1818

1919
pdf.exec(callback)
20-
21-
22-
module.exports.create = ->
23-
module.exports.apply(undefined, arguments)

lib/index.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var PDF;
22

33
PDF = require('./pdf');
44

5-
module.exports = function(html, options, callback) {
5+
exports.create = function(html, options, callback) {
66
var err, pdf;
77
if (arguments.length === 1) {
88
return new PDF(html);
@@ -22,7 +22,3 @@ module.exports = function(html, options, callback) {
2222
}
2323
return pdf.exec(callback);
2424
};
25-
26-
module.exports.create = function() {
27-
return module.exports.apply(void 0, arguments);
28-
};

lib/pdf.coffee

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,17 @@ module.exports = class PDF
4242
stream
4343

4444

45+
toFile: (filename, callback) ->
46+
assert(arguments.length == 2, 'html-pdf: The method pdf.toFile([filename, ]callback) requires two arguments.')
47+
if arguments.length == 1
48+
callback = filename
49+
filename = undefined
50+
else
51+
@options.filename = filename
52+
@exec(callback)
53+
stream
54+
55+
4556
exec: (callback) ->
4657
child = childprocess.spawn(phantomjs.path, [@script])
4758
stdout = []

lib/pdf.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ module.exports = PDF = (function() {
4747
return stream;
4848
};
4949

50+
PDF.prototype.toFile = function(filename, callback) {
51+
assert(arguments.length === 2, 'html-pdf: The method pdf.toFile([filename, ]callback) requires two arguments.');
52+
if (arguments.length === 1) {
53+
callback = filename;
54+
filename = void 0;
55+
} else {
56+
this.options.filename = filename;
57+
}
58+
this.exec(callback);
59+
return stream;
60+
};
61+
5062
PDF.prototype.exec = function(callback) {
5163
var child, stderr, stdout, timeout;
5264
child = childprocess.spawn(phantomjs.path, [this.script]);

lib/scripts/pdf_a4_portrait.coffee

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ exit('Did not receive any html') if !json.html?.trim()
2121
options = json.options
2222
page = webpage.create()
2323
page.content = json.html
24+
page.viewportSize = vp if vp = options.viewportSize
25+
totalPages = 0
2426

2527

2628
# Set up content

lib/scripts/pdf_a4_portrait.js

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
var content, exit, json, options, page, paper, setContent, system, totalPages, type, vp, webpage, _i, _len, _ref, _ref1, _ref2, _ref3;
2+
3+
system = require('system');
4+
5+
webpage = require('webpage');
6+
7+
exit = function(error) {
8+
var message;
9+
if (typeof error === 'string') {
10+
message = error;
11+
}
12+
if (error) {
13+
system.stderr.write("html-pdf: " + (message || ("Unknown Error " + error)) + "\n");
14+
}
15+
return phantom.exit(error ? 1 : 0);
16+
};
17+
18+
setTimeout(function() {
19+
return exit('Force timeout');
20+
}, 120000);
21+
22+
json = JSON.parse(system.stdin.readLine());
23+
24+
if (!((_ref = json.html) != null ? _ref.trim() : void 0)) {
25+
exit('Did not receive any html');
26+
}
27+
28+
options = json.options;
29+
30+
page = webpage.create();
31+
32+
page.content = json.html;
33+
34+
if (vp = options.viewportSize) {
35+
page.viewportSize = vp;
36+
}
37+
38+
totalPages = 0;
39+
40+
content = page.evaluate(function() {
41+
var $body, $footer, $header, body, footer, header, styles, _ref1;
42+
styles = ((_ref1 = document.querySelector('head style')) != null ? _ref1.outerHTML : void 0) || '';
43+
if ($header = document.getElementById('pageHeader')) {
44+
header = $header.outerHTML;
45+
$header.parentNode.removeChild($header);
46+
}
47+
if ($footer = document.getElementById('pageFooter')) {
48+
footer = $footer.outerHTML;
49+
$footer.parentNode.removeChild($footer);
50+
}
51+
if ($body = document.getElementById('pageContent')) {
52+
body = $body.outerHTML;
53+
} else {
54+
body = document.body.outerHTML;
55+
}
56+
return {
57+
styles: styles,
58+
header: header,
59+
body: body,
60+
footer: footer
61+
};
62+
});
63+
64+
paper = {
65+
border: options.border || '0'
66+
};
67+
68+
if (options.height && options.width) {
69+
paper.width = options.width;
70+
paper.height = options.height;
71+
} else {
72+
paper.format = options.format || 'A4';
73+
paper.orientation = options.orientation || 'portrait';
74+
}
75+
76+
setContent = function(type) {
77+
var _ref1;
78+
return paper[type] = {
79+
height: (_ref1 = options[type]) != null ? _ref1.height : void 0,
80+
contents: phantom.callback(function(pageNum, numPages) {
81+
var _ref2;
82+
return (((_ref2 = options[type]) != null ? _ref2.contents : void 0) || content[type] || '').replace('{{page}}', pageNum).replace('{{pages}}', numPages) + content.styles;
83+
})
84+
};
85+
};
86+
87+
_ref1 = ['header', 'footer'];
88+
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
89+
type = _ref1[_i];
90+
if (options[type] || content[type]) {
91+
setContent(type);
92+
}
93+
}
94+
95+
if ((_ref2 = paper.header) != null) {
96+
if (_ref2.height == null) {
97+
_ref2.height = '45mm';
98+
}
99+
}
100+
101+
if ((_ref3 = paper.footer) != null) {
102+
if (_ref3.height == null) {
103+
_ref3.height = '28mm';
104+
}
105+
}
106+
107+
page.paperSize = paper;
108+
109+
page.onLoadFinished = function(status) {
110+
var fileOptions, filename;
111+
fileOptions = {
112+
type: options.type || 'pdf',
113+
quality: options.quality || 75
114+
};
115+
if (!options.buffer) {
116+
filename = options.filename || ("" + (options.directory || '/tmp') + "/html-pdf-" + system.pid + "." + fileOptions.type);
117+
page.render(filename, fileOptions);
118+
system.stdout.write(filename);
119+
} else {
120+
system.stderr.write('html-pdf: options.buffer is deprecated. Because of compatibility issues this method is longer supported.\n');
121+
page.render('/dev/stdout', fileOptions);
122+
}
123+
return exit(null);
124+
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "html-pdf",
3-
"version": "0.3.0",
3+
"version": "0.4.0",
44
"description": "HTML to PDF converter that uses phantomjs",
55
"main": "lib/index.js",
66
"directories": {

0 commit comments

Comments
 (0)