Skip to content

Commit 4b29bd4

Browse files
committed
Expose config for custom header & footer per page.
This brings the config object up to date with the dom api. I guess this closes #154 ``` "header": { "height": "28mm", "contents": { "first": "first page", "2": "second page", "last": "last page" } }, "footer": { "height": "28mm", "contents": { "first": "first page", "2": "second page", "last": "last page", "default": "Page {{page}} of {{pages}}" } } ```
1 parent cc5aa84 commit 4b29bd4

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ pdf.create(html [, options], function(err, buffer){});
6161

6262
### Footers and Headers
6363

64-
`html-pdf` can read the header or footer of a page out of the html source. You can either set a default header or footer or overwrite that by appending a page number (1 based index) to the `id="pageHeader"` attribute of a html tag.
64+
`html-pdf` can read the header or footer either out of the `footer` and `header` config object or out of the html source. You can either set a default header & footer or overwrite that by appending a page number (1 based index) to the `id="pageHeader"` attribute of a html tag.
6565

6666
You can use any combination of those tags. The library tries to find any element, that contains the `pageHeader` or `pageFooter` id prefix.
6767
```html
@@ -108,14 +108,19 @@ config = {
108108
},
109109
"footer": {
110110
"height": "28mm",
111-
"contents": '<span style="color: #444;">{{page}}</span>/<span>{{pages}}</span>'
111+
"contents": {
112+
first: 'Cover page',
113+
2: 'Second page' // Any page number is working. 1-based index
114+
default: '<span style="color: #444;">{{page}}</span>/<span>{{pages}}</span>', // fallback value
115+
last: 'Last Page'
116+
}
112117
},
113118

114119

115120
// Rendering options
116121
"base": "file:///home/www/your-asset-path", // Base path that's used to load files (images, css, js) when they aren't referenced using a host
117-
118-
// Zooming options
122+
123+
// Zooming option, can be used to scale images if `options.type` is not pdf
119124
"zoomFactor": "1", // default is 1
120125

121126
// File options

lib/scripts/pdf_a4_portrait.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,16 +144,18 @@ function getContent (page) {
144144
// Creates page section
145145
// --------------------
146146
function createSection (section, content, options) {
147+
options = options[section] || {}
147148
var c = content[section] || {}
148-
var o = options[section] || {}
149+
var o = options.contents
150+
if (typeof o !== 'object') o = {default: o}
149151

150152
return {
151-
height: o.height,
153+
height: options.height,
152154
contents: phantom.callback(function (pageNum, numPages) {
153-
var html = c[pageNum]
154-
if (pageNum === 1 && !html) html = c.first
155-
if (pageNum === numPages && !html) html = c.last
156-
return (html || c.default || o.contents || '')
155+
var html = o[pageNum] || c[pageNum]
156+
if (pageNum === 1 && !html) html = o.first || c.first
157+
if (pageNum === numPages && !html) html = o.last || c.last
158+
return (html || o.default || c.default || '')
157159
.replace('{{page}}', pageNum)
158160
.replace('{{pages}}', numPages) + content.styles
159161
})

0 commit comments

Comments
 (0)