Skip to content

Commit

Permalink
Merge pull request #105 from marcbachmann/dynamic-header-and-footers
Browse files Browse the repository at this point in the history
Support dynamic headers and footers
  • Loading branch information
marcbachmann committed Jan 27, 2016
2 parents d25f2d1 + 6bb48cf commit 5485abd
Show file tree
Hide file tree
Showing 8 changed files with 337 additions and 42 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.pdf
!example/businesscard.pdf
node_modules
8 changes: 4 additions & 4 deletions lib/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 10 additions & 10 deletions lib/pdf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

74 changes: 59 additions & 15 deletions lib/scripts/pdf_a4_portrait.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"tape": "^3.4.0"
},
"optionalDependencies": {
"phantomjs": "^1.9.16"
"phantomjs": "^1.9.19"
},
"repository": {
"type": "git",
Expand Down
54 changes: 42 additions & 12 deletions src/scripts/pdf_a4_portrait.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,40 @@ setTimeout ->
# ------------------------------
getContent = ->
page.evaluate ->
getElements = (doc, wildcard) ->
wildcardMatcher = new RegExp("#{wildcard}(.*)")
hasElements = false
elements = {}
$elements = document.querySelectorAll("[id*='#{wildcard}']")
for $elem in $elements
if match = $elem.attributes.id.value.match(wildcardMatcher)
hasElements = true
i = match[1]
elements[i] = $elem.outerHTML

$elem.parentNode.removeChild($elem)

if hasElements
return elements

getElement = (doc, id) ->
if $elem = doc.getElementById(id)
html = $elem.outerHTML
$elem.parentNode.removeChild($elem)
return html

styles = document.querySelectorAll('link,style')
styles = Array::reduce.call(styles, ((string, node) -> string+node.outerHTML),'')
if $header = document.getElementById('pageHeader')
header = $header.outerHTML
$header.parentNode.removeChild($header)

if $footer = document.getElementById('pageFooter')
footer = $footer.outerHTML
$footer.parentNode.removeChild($footer)
# Wildcard headers e.g. <div id="pageHeader-first"> or <div id="pageHeader-0">
header = getElements(document, 'pageHeader-')
footer = getElements(document, 'pageFooter-')

# Default header and footer e.g. <div id="pageHeader">
h = getElement(document, 'pageHeader')
f = getElement(document, 'pageFooter')
(header ?= {}).default = h if h
(footer ?= {}).default = f if f

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

# Creates page section
# --------------------
createSection = (content, styles, options) ->
height: options?.height
createSection = (section, content, options) ->
c = content[section] || {}
o = options[section] || {}

height: o.height
contents: phantom.callback (pageNum, numPages) ->
(options?.contents || content || '')
html = c[pageNum]
html ?= c['first'] if pageNum == 1
html ?= c['last'] if pageNum == numPages
(html || c.default || o.contents || '')
.replace('{{page}}', pageNum)
.replace('{{pages}}', numPages)+styles
.replace('{{pages}}', numPages) + content.styles


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

for section in ['header', 'footer']
if options[section] || content[section]
paper[section] =
createSection(content[section], content.styles, options[section])
paper[section] = createSection(section, content, options)

paper.header?.height ?= '46mm'
paper.footer?.height ?= '28mm'
Expand Down
11 changes: 11 additions & 0 deletions test/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,17 @@ test 'allows custom page and footer options', (st) ->
st.assert(fs.existsSync(pdf.filename), 'Saves the pdf with a custom page size and footer')


test 'allows different header and footer for first page', (st) ->
st.plan(3)

enrichedHtml = fs.readFileSync(path.join(__dirname, 'multiple-pages.html'), 'utf8')
filename = path.join(__dirname, 'multiple-pages.pdf')
pdf.create(enrichedHtml, quality: 100).toFile filename, (error, pdf) ->
st.error(error)
st.assert(pdf.filename == filename, 'Returns the filename from the phantom script')
st.assert(fs.existsSync(pdf.filename), 'Saves the pdf with a custom page size and footer')


test 'load external css', (st) ->
st.plan(3)

Expand Down
Loading

0 comments on commit 5485abd

Please sign in to comment.