Skip to content

Commit 2ce36eb

Browse files
committed
Add custom header and footer support
You can use <div id=“pageFooter-last”> <div id=“pageFooter-first”> <div id=“pageFooter-2”>
1 parent 6ca5600 commit 2ce36eb

1 file changed

Lines changed: 42 additions & 12 deletions

File tree

src/scripts/pdf_a4_portrait.coffee

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,40 @@ setTimeout ->
5454
# ------------------------------
5555
getContent = ->
5656
page.evaluate ->
57+
getElements = (doc, wildcard) ->
58+
wildcardMatcher = new RegExp("#{wildcard}(.*)")
59+
hasElements = false
60+
elements = {}
61+
$elements = document.querySelectorAll("[id*='#{wildcard}']")
62+
for $elem in $elements
63+
if match = $elem.attributes.id.value.match(wildcardMatcher)
64+
hasElements = true
65+
i = match[1]
66+
elements[i] = $elem.outerHTML
67+
68+
$elem.parentNode.removeChild($elem)
69+
70+
if hasElements
71+
return elements
72+
73+
getElement = (doc, id) ->
74+
if $elem = doc.getElementById(id)
75+
html = $elem.outerHTML
76+
$elem.parentNode.removeChild($elem)
77+
return html
78+
5779
styles = document.querySelectorAll('link,style')
5880
styles = Array::reduce.call(styles, ((string, node) -> string+node.outerHTML),'')
59-
if $header = document.getElementById('pageHeader')
60-
header = $header.outerHTML
61-
$header.parentNode.removeChild($header)
6281

63-
if $footer = document.getElementById('pageFooter')
64-
footer = $footer.outerHTML
65-
$footer.parentNode.removeChild($footer)
82+
# Wildcard headers e.g. <div id="pageHeader-first"> or <div id="pageHeader-0">
83+
header = getElements(document, 'pageHeader-')
84+
footer = getElements(document, 'pageFooter-')
85+
86+
# Default header and footer e.g. <div id="pageHeader">
87+
h = getElement(document, 'pageHeader')
88+
f = getElement(document, 'pageFooter')
89+
(header ?= {}).default = h if h
90+
(footer ?= {}).default = f if f
6691

6792
if $body = document.getElementById('pageContent')
6893
body = $body.outerHTML
@@ -89,12 +114,18 @@ createPaper = (options) ->
89114

90115
# Creates page section
91116
# --------------------
92-
createSection = (content, styles, options) ->
93-
height: options?.height
117+
createSection = (section, content, options) ->
118+
c = content[section] || {}
119+
o = options[section] || {}
120+
121+
height: o.height
94122
contents: phantom.callback (pageNum, numPages) ->
95-
(options?.contents || content || '')
123+
html = c[pageNum]
124+
html ?= c['first'] if pageNum == 1
125+
html ?= c['last'] if pageNum == numPages
126+
(html || c.default || o.contents || '')
96127
.replace('{{page}}', pageNum)
97-
.replace('{{pages}}', numPages)+styles
128+
.replace('{{pages}}', numPages) + content.styles
98129

99130

100131
# Creates paper with generated footer & header
@@ -104,8 +135,7 @@ generatePaper = (content, options) ->
104135

105136
for section in ['header', 'footer']
106137
if options[section] || content[section]
107-
paper[section] =
108-
createSection(content[section], content.styles, options[section])
138+
paper[section] = createSection(section, content, options)
109139

110140
paper.header?.height ?= '46mm'
111141
paper.footer?.height ?= '28mm'

0 commit comments

Comments
 (0)