Skip to content

Commit a7bcbe5

Browse files
committed
Update readme & unlink file after buffer & stream read
1 parent 4d6782a commit a7bcbe5

5 files changed

Lines changed: 46 additions & 46 deletions

File tree

README.md

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,32 @@
77
[Example Receipt](http://public.admintools.ch/gh/html-pdf/order.pdf)
88

99

10+
## Example
1011
```javascript
1112
var fs = require('fs');
1213
var pdf = require('html-pdf');
1314
var html = fs.readFileSync('./test/businesscard.html', 'utf8')
1415
var options = { filename: './businesscard.pdf', format: 'Letter' };
15-
pdf(html, options).exec(function(err, res) {
16+
17+
pdf.create(html, options).toFile(function(err, res) {
1618
if (err) return console.log(err);
17-
console.log(res);
18-
/*
19-
{
20-
filename: './businesscard.pdf',
21-
pages: 1
22-
}
23-
*/
19+
console.log(res); // { filename: '/tmp/html-pdf-8ymPV.pdf' }
2420
});
2521
```
2622

2723
## API
24+
2825
```js
29-
pdf(html [, options]).toFile(callback)
30-
pdf(html [, options]).toBuffer(callback)
31-
pdf(html [, options]).toStream(callback)
26+
pdf.create(html [, options]).toFile(callback)
27+
pdf.create(html [, options]).toBuffer(callback)
28+
pdf.create(html [, options]).toStream(callback)
3229

3330
// for backward compatibility
3431
pdf.create(html [, options], callback)
3532

3633
```
3734

3835

39-
```javascript
40-
var pdf = require('html-pdf');
41-
pdf.create(htmlString, options, function(err, res){
42-
console.log(res); // { "filename": "/tmp/path" }
43-
})
44-
```
45-
46-
4736
## Options
4837
```javascript
4938
config = {
@@ -53,10 +42,10 @@ config = {
5342
"directory": "/tmp" // The directory the file gets written into if no filename is defined. default: '/tmp'
5443

5544
// Papersize Options: http://phantomjs.org/api/webpage/property/paper-size.html
56-
"height": "", // allowed units: mm, cm, in, px
57-
"width": "", // allowed units: mm, cm, in, px
45+
"height": "10.5in", // allowed units: mm, cm, in, px
46+
"width": "8in", // allowed units: mm, cm, in, px
5847
- or -
59-
"format": "A4", // allowed units: A3, A4, A5, Legal, Letter, Tabloid
48+
"format": "Letter", // allowed units: A3, A4, A5, Legal, Letter, Tabloid
6049
"orientation": "portrait", // portrait or landscape
6150

6251
// Page options
@@ -75,8 +64,8 @@ config = {
7564
"quality": "75", // only used for types png & jpeg
7665

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

8170
}
8271
```

lib/pdf.js

Lines changed: 18 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/scripts/pdf_a4_portrait.js

Lines changed: 5 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/pdf.coffee

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ module.exports = class PDF
3232
toBuffer: (callback) ->
3333
@exec (err, res) ->
3434
return callback(err) if err
35-
fs.readFile(res.filename, callback)
35+
fs.readFile res.filename, (err, buffer) ->
36+
return callback(err) if err
37+
fs.unlink res.filename, (err) ->
38+
return callback(err) if err
39+
callback(null, buffer)
3640

3741

3842
toStream: (callback) ->
@@ -42,6 +46,8 @@ module.exports = class PDF
4246
stream = fs.createReadStream(res.filename)
4347
catch err
4448
return callback(err)
49+
50+
stream.on 'end', -> fs.unlink res.filename, (err) -> console.log('html-pdf:', err) if err
4551
callback(null, stream)
4652

4753

src/scripts/pdf_a4_portrait.coffee

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,8 @@ page.onLoadFinished = (status) ->
8888
type: options.type || 'pdf'
8989
quality: options.quality || 75
9090

91-
if !options.buffer
92-
filename = options.filename || ("#{options.directory || '/tmp'}/html-pdf-#{system.pid}.#{fileOptions.type}")
93-
page.render(filename, fileOptions)
94-
system.stdout.write(JSON.stringify({filename}))
95-
96-
# Deprecated options.buffer method
97-
else
98-
system.stderr.write('html-pdf: options.buffer is deprecated. Because of compatibility issues this method is longer supported.\n')
99-
page.render('/dev/stdout', fileOptions)
100-
91+
filename = options.filename || ("#{options.directory || '/tmp'}/html-pdf-#{system.pid}.#{fileOptions.type}")
92+
page.render(filename, fileOptions)
93+
system.stdout.write(JSON.stringify({filename}))
10194

10295
exit(null)

0 commit comments

Comments
 (0)