-
Notifications
You must be signed in to change notification settings - Fork 528
Expand file tree
/
Copy pathpdf_a4_portrait.js
More file actions
executable file
·225 lines (186 loc) · 6.35 KB
/
pdf_a4_portrait.js
File metadata and controls
executable file
·225 lines (186 loc) · 6.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/* global phantom */
const system = require('system')
const webpage = require('webpage')
// Error handler
function exit (error) {
let message
if (typeof error === 'string') message = error
if (error) system.stderr.write('html-pdf: ' + (message || 'Unknown Error ' + error) + '\n')
phantom.exit(error ? 1 : 0)
}
// Build stack to print
function buildStack (msg, trace) {
const msgStack = [msg]
if (trace && trace.length) {
msgStack.push('Stack:')
trace.forEach(function (t) {
msgStack.push(' at ' + t.file || t.sourceURL + ': ' + t.line + ' (in function ' + t.function + ')')
})
}
return msgStack.join('\n')
}
phantom.onError = function (msg, trace) {
exit(buildStack('Script - ' + msg, trace))
}
// Load configurations from stdin
const json = JSON.parse(system.stdin.readLine())
if (!json.html || !json.html.trim()) exit('Did not receive any html')
const options = json.options
const page = webpage.create()
// Completely load page & end process
// ----------------------------------
let rendered = false
let renderTimeout
// If renderDelay is manual, then listen for an event and don't automatically render
if (options.renderDelay === 'manual') {
page.onCallback = function (message) {
setTimeout(renderNow, 0)
return message
}
}
page.onLoadFinished = function () {
if (options.renderDelay === 'manual') return
renderTimeout = setTimeout(renderNow, Math.floor(options.renderDelay) || 0)
}
function renderNow () {
if (rendered) return
rendered = true
clearTimeout(renderTimeout)
page.paperSize = definePaperSize(getContent(page), options)
const fileOptions = {
type: options.type || 'pdf',
quality: options.quality || 75
}
const filename = options.filename || (options.directory || '/tmp') + '/html-pdf-' + system.pid + '.' + fileOptions.type
page.render(filename, fileOptions)
// Output to parent process
system.stdout.write(JSON.stringify({ filename }))
exit(null)
}
// Set Content and begin loading
// -----------------------------
if (options.httpCookies) page.cookies = options.httpCookies
if (options.httpHeaders) page.customHeaders = options.httpHeaders
if (options.viewportSize) page.viewportSize = options.viewportSize
if (options.zoomFactor) page.zoomFactor = options.zoomFactor
if (options.base) page.setContent(json.html, options.base)
else page.setContent(json.html, null)
page.onError = function (msg, trace) {
exit(buildStack('Evaluation - ' + msg, trace))
}
// Force cleanup after 2 minutes
// Add 2 seconds to make sure master process triggers kill
// before to the phantom process
const timeout = (options.timeout || 120000) + 2000
setTimeout(function () {
exit('Force timeout')
}, timeout)
// Returns a hash of HTML content
// ------------------------------
function getContent (page) {
return page.evaluate(function () {
function getElements (doc, wildcard) {
const wildcardMatcher = new RegExp(wildcard + '(.*)')
let hasElements = false
const elements = {}
const $elements = document.querySelectorAll("[id*='" + wildcard + "']")
let $elem, match, i
const len = $elements.length
for (i = 0; i < len; i++) {
$elem = $elements[i]
match = $elem.attributes.id.value.match(wildcardMatcher)
if (match) {
hasElements = true
elements[match[1]] = $elem.outerHTML
$elem.parentNode.removeChild($elem)
}
}
if (hasElements) return elements
}
function getElement (doc, id) {
const $elem = doc.getElementById(id)
if ($elem) {
const html = $elem.outerHTML
$elem.parentNode.removeChild($elem)
return html
}
}
let styles = document.querySelectorAll('link,style')
styles = Array.prototype.reduce.call(styles, function (string, node) {
return string + (node.outerHTML || '')
}, '')
// Wildcard headers e.g. <div id="pageHeader-first"> or <div id="pageHeader-0">
let header = getElements(document, 'pageHeader-')
let footer = getElements(document, 'pageFooter-')
// Default header and footer e.g. <div id="pageHeader">
const h = getElement(document, 'pageHeader')
const f = getElement(document, 'pageFooter')
if (h) {
header = header || {}
header.default = h
}
if (f) {
footer = footer || {}
footer.default = f
}
let body
const $body = document.getElementById('pageContent')
if ($body) body = $body.outerHTML
else body = document.body.outerHTML
return {
styles,
header,
body,
footer
}
})
}
// Creates page section
// --------------------
function createSection (section, content, options) {
options = options[section] || {}
const c = content[section] || {}
let o = options.contents
const paginationOffset = Math.floor(options.paginationOffset) || 0
if (typeof o !== 'object') o = { default: o }
return {
height: options.height,
contents: phantom.callback(function (pageNum, numPages) {
let html = o[pageNum] || c[pageNum]
const pageNumFinal = pageNum + paginationOffset
const numPagesFinal = numPages + paginationOffset
if (pageNumFinal === 1 && !html) html = o.first || c.first
if (pageNumFinal === numPages && !html) html = o.last || c.last
return (html || o.default || c.default || '')
.replace(/{{page}}/g, pageNumFinal)
.replace(/{{pages}}/g, numPagesFinal) + content.styles
})
}
}
// Creates paper with specified options
// ------------------------------------
function definePaperOrientation (options) {
const paper = { border: options.border || '0' }
if (options.height && options.width) {
paper.width = options.width
paper.height = options.height
} else {
paper.format = options.format || 'A4'
paper.orientation = options.orientation || 'portrait'
}
return paper
}
// Creates paper with generated footer & header
// --------------------------------------------
function definePaperSize (content, options) {
const paper = definePaperOrientation(options)
if (options.header || content.header) {
paper.header = createSection('header', content, options)
}
if (options.footer || content.footer) {
paper.footer = createSection('footer', content, options)
}
if (paper.header && paper.header.height === undefined) paper.header.height = '46mm'
if (paper.footer && paper.footer.height === undefined) paper.footer.height = '28mm'
return paper
}