@@ -44,64 +44,75 @@ setTimeout ->
4444, timeout || 12000
4545
4646
47- # Set up content
48- # --------------
49- content = page .evaluate ->
50- styles = document .querySelectorAll (' link,style' )
51- styles = Array :: reduce .call (styles, ((string , node ) -> string+ node .outerHTML ),' ' )
52- if $header = document .getElementById (' pageHeader' )
53- header = $header .outerHTML
54- $header .parentNode .removeChild ($header)
55-
56- if $footer = document .getElementById (' pageFooter' )
57- footer = $footer .outerHTML
58- $footer .parentNode .removeChild ($footer)
59-
60- if $body = document .getElementById (' pageContent' )
61- body = $body .outerHTML
47+ # Returns a hash of HTML content
48+ # ------------------------------
49+ getContent = () ->
50+ page .evaluate ->
51+ styles = document .querySelectorAll (' link,style' )
52+ styles = Array :: reduce .call (styles, ((string , node ) -> string+ node .outerHTML ),' ' )
53+ if $header = document .getElementById (' pageHeader' )
54+ header = $header .outerHTML
55+ $header .parentNode .removeChild ($header)
56+
57+ if $footer = document .getElementById (' pageFooter' )
58+ footer = $footer .outerHTML
59+ $footer .parentNode .removeChild ($footer)
60+
61+ if $body = document .getElementById (' pageContent' )
62+ body = $body .outerHTML
63+ else
64+ body = document .body .outerHTML
65+
66+ {styles, header, body, footer}
67+
68+
69+ # Creates paper with specified options
70+ # ------------------------------------
71+ createPaper = (options ) ->
72+ paper = border : options .border || ' 0'
73+
74+ if options .height && options .width
75+ paper .width = options .width
76+ paper .height = options .height
6277 else
63- body = document .body .outerHTML
78+ paper .format = options .format || ' A4'
79+ paper .orientation = options .orientation || ' portrait'
6480
65- {styles, header, body, footer}
81+ paper
6682
6783
68- # Set up paperSize options
69- # -------------------------
70- paper = border : options .border || ' 0'
84+ # Creates page section
85+ # --------------------
86+ createSection = (content , styles , options ) ->
87+ height : options ? .height
88+ contents : phantom .callback (pageNum, numPages) ->
89+ (options ? .contents || content || ' ' )
90+ .replace (' {{page}}' , pageNum)
91+ .replace (' {{pages}}' , numPages)+ styles
7192
72- if options .height && options .width
73- paper .width = options .width
74- paper .height = options .height
75- else
76- paper .format = options .format || ' A4'
77- paper .orientation = options .orientation || ' portrait'
7893
94+ # Creates paper with generated footer & header
95+ # --------------------------------------------
96+ generatePaper = (content , options ) ->
97+ paper = createPaper (options)
7998
80- # Generate footer & header
81- # ------------------------
82- setContent = (type ) ->
83- paper[type] =
84- height : options[type]? .height
85- contents : phantom .callback (pageNum, numPages) ->
86- (options[type]? .contents || content[type] || ' ' )
87- .replace (' {{page}}' , pageNum)
88- .replace (' {{pages}}' , numPages)+ content .styles
99+ for section in [' header' , ' footer' ]
100+ if options[section] || content[section]
101+ paper[section] =
102+ createSection (content[section], content .styles , options[section])
89103
90- for type in [ ' header' , ' footer ' ]
91- setContent (type) if options[type] || content[type]
104+ paper . header ? . height ?= ' 46mm '
105+ paper . footer ? . height ?= ' 28mm '
92106
93- paper .header ? .height ?= ' 46mm'
94- paper .footer ? .height ?= ' 28mm'
95-
96-
97- # The paperSize object must be set at once
98- # -----------------------------------------
99- page .paperSize = paper
107+ paper
100108
101109
102110# Completely load page & end process
103111# ----------------------------------
104112page .onLoadFinished = (status ) ->
113+ # The paperSize object must be set at once
114+ page .paperSize = generatePaper (getContent (), options)
115+
105116 # Output to parent process
106117 fileOptions =
107118 type : options .type || ' pdf'
0 commit comments