Skip to content

Commit d4c2707

Browse files
committed
Merge pull request #2 from marcbachmann/prettify
Support global css styles
2 parents a91a3c9 + 7fcec97 commit d4c2707

4 files changed

Lines changed: 79 additions & 38 deletions

File tree

README.md

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,44 @@ pdf.create(htmlString, options, callback)
2626
```
2727

2828
## Options
29-
`script`: Absolute path to a custom phantomjs script, use the file in lib/scripts as example
30-
`timeout`: Timeout that will cancel phantomjs, milliseconds as Integer, default: 10000
31-
`filename`: The file path of the file that will be written. If you want to save the file permanently, you have to pass this option.
32-
`directory`: The directory path of the file that will be written. default: '/tmp'
29+
```javascript
30+
config = {
31+
// Script options
32+
script: '/url' // Absolute path to a custom phantomjs script, use the file in lib/scripts as example
33+
timeout: 10000 // Timeout that will cancel phantomjs, in milliseconds
3334

34-
The full options object gets converted to JSON and will get passed to the phantomjs script as third argument.
35-
There are more options concerning the paperSize, header & footer options inside the phantomjs script.
35+
// Papersize Options: http://phantomjs.org/api/webpage/property/paper-size.html
36+
"height": "", // allowed units: mm, cm, in, px
37+
"width": "", // allowed units: mm, cm, in, px
38+
- or -
39+
"format": "A4", // allowed units: A3, A4, A5, Legal, Letter, Tabloid
40+
"orientation": "portrait", // portrait or landscape
41+
42+
43+
// Page options
44+
"border": "0" // default is 0, units: mm, cm, in, px
45+
"header": {
46+
"height": "45mm",
47+
"contents": '<div style="text-align: center;">Author: Marc Bachmann</div>'
48+
},
49+
"footer": {
50+
"height": "28mm",
51+
"contents": '<span style="color: #444;">{{page}}</span>/<span>{{pages}}</span>'
52+
},
53+
54+
55+
// File options
56+
"type": "pdf", // allowed file types: png, jpeg, pdf
57+
"quality": "75", // only used for types png & jpeg
3658

37-
## documentations for more available options will follow soon :)
59+
60+
// Export options
61+
"buffer": true, // only supported on certain systems
62+
- or -
63+
"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.
64+
"directory": "/tmp" // The directory the file gets written into if no filename is defined. default: '/tmp'
65+
}
66+
```
67+
68+
The full options object gets converted to JSON and will get passed to the phantomjs script as third argument.
69+
There are more options concerning the paperSize, header & footer options inside the phantomjs script.

lib/index.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ exports.create = (string, options, callback) ->
3131
child.stdin.end()
3232
child.kill()
3333
stderr = [new Buffer('PDF creation timeout. PDF generation script did not end.')] unless stderr.length
34-
, options.timeout || 10000
34+
, parseInt(options.timeout) || 10000
3535

3636
child.stdout.on 'data', (buffer) ->
3737
stdout.push(buffer)

lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ exports.create = function(string, options, callback) {
2828
if (!stderr.length) {
2929
return stderr = [new Buffer('PDF creation timeout. PDF generation script did not end.')];
3030
}
31-
}, options.timeout || 10000);
31+
}, parseInt(options.timeout) || 10000);
3232
child.stdout.on('data', function(buffer) {
3333
return stdout.push(buffer);
3434
});

lib/scripts/pdf_a4_portrait.coffee

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ sys = require('system')
22
webpage = require('webpage')
33

44
page = webpage.create()
5-
size = sys.args[1]
5+
bufferSize = sys.args[1]
66
options = {}
77
options = JSON.parse(sys.args[2]) if typeof sys.args[2] is 'string'
8-
page.content = sys.stdin.read(size)
8+
page.content = sys.stdin.read(bufferSize)
99

1010

1111
# Set up content
12+
# --------------
1213
content = page.evaluate ->
14+
styles = document.querySelector('head style')?.outerHTML || ''
1315
if $header = document.getElementById('pageHeader')
1416
header = $header.outerHTML
1517
$header.parentNode.removeChild($header)
@@ -21,41 +23,48 @@ content = page.evaluate ->
2123
if $body = document.getElementById('pageContent')
2224
body = $body.outerHTML
2325
else
24-
document.body.outerHTML
26+
body = document.body.outerHTML
2527

26-
return {
27-
header: header || ""
28-
body: body || ""
29-
footer: footer || ""
30-
}
28+
{styles, header, body, footer}
3129

3230

33-
# Set up options
34-
paperSize =
35-
border: options.border || '0'
36-
header: if options.header || content.header
37-
height: options.header?.height || "45mm"
38-
contents: phantom.callback (pageNum, numPages) ->
39-
(options.header?.contents || content.header)
40-
.replace('{{page}}', pageNum)
41-
.replace('{{pages}}', numPages)
31+
# Set up paperSize options
32+
# -------------------------
33+
paper = border: options.border || '0'
34+
35+
if options.height && options.width
36+
paper.width = options.width
37+
paper.height = options.height
38+
else
39+
paper.format = options.format || 'A4'
40+
paper.orientation = options.orientation || 'portrait'
41+
4242

43-
footer: if options.footer || content.footer
44-
height: options.footer?.height || "28mm"
43+
# Generate footer & header
44+
# ------------------------
45+
setContent = (type) ->
46+
paper[type] =
47+
height: options[type]?.height
4548
contents: phantom.callback (pageNum, numPages) ->
46-
(options.footer?.contents || content.footer)
49+
(options[type]?.contents || content[type] || '')
4750
.replace('{{page}}', pageNum)
48-
.replace('{{pages}}', numPages)
51+
.replace('{{pages}}', numPages)+content.styles
52+
53+
for type in ['header', 'footer']
54+
setContent(type) if options[type] || content[type]
55+
56+
paper.header?.height ?= '45mm'
57+
paper.footer?.height ?= '28mm'
58+
59+
60+
# The paperSize object must be set at once
61+
# -----------------------------------------
62+
page.paperSize = paper
4963

50-
if options.height && options.width
51-
paperSize.width = options.width
52-
paperSize.height = options.height
53-
else
54-
paperSize.format = options.format || 'A4'
55-
paperSize.orientation = options.orientation || 'portrait'
5664

57-
page.paperSize = paperSize
5865

66+
# Completely load page & end process
67+
# ----------------------------------
5968
page.onLoadFinished = (status) ->
6069
# Output to parent process
6170
fileOptions =
@@ -70,7 +79,7 @@ page.onLoadFinished = (status) ->
7079

7180
# Option 2: Output filename to stdout
7281
else
73-
filename = options.filename || ("#{options.directory || '/tmp'}/html-pdf-#{sys.pid}-#{size}.#{fileOptions.type}")
82+
filename = options.filename || ("#{options.directory || '/tmp'}/html-pdf-#{sys.pid}-#{bufferSize}.#{fileOptions.type}")
7483
page.render(filename, fileOptions)
7584
sys.stdout.write(filename)
7685

0 commit comments

Comments
 (0)