@@ -2,14 +2,16 @@ sys = require('system')
22webpage = require (' webpage' )
33
44page = webpage .create ()
5- size = sys .args [1 ]
5+ bufferSize = sys .args [1 ]
66options = {}
77options = 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+ # --------------
1213content = 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+ # ----------------------------------
5968page .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