Skip to content

Commit 6830076

Browse files
committed
Add manual renderDelay support using window.callPhantom
1 parent 42e2dfb commit 6830076

File tree

3 files changed

+73
-21
lines changed

3 files changed

+73
-21
lines changed

lib/scripts/pdf_a4_portrait.js

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,34 +33,21 @@ if (!json.html || !json.html.trim()) exit('Did not receive any html')
3333
var options = json.options
3434
var page = webpage.create()
3535

36-
if (options.httpHeaders) page.customHeaders = options.httpHeaders
37-
if (options.viewportSize) page.viewportSize = options.viewportSize
38-
if (options.zoomFactor) page.zoomFactor = options.zoomFactor
39-
if (options.base) page.setContent(json.html, options.base)
40-
else page.setContent(json.html, null)
41-
42-
page.onError = function (msg, trace) {
43-
exit(buildStack('Evaluation - ' + msg, trace))
44-
}
45-
46-
// Force cleanup after 2 minutes
47-
// Add 2 seconds to make sure master process triggers kill
48-
// before to the phantom process
49-
var timeout = (options.timeout || 120000) + 2000
50-
setTimeout(function () {
51-
exit('Force timeout')
52-
}, timeout)
53-
5436
// Completely load page & end process
5537
// ----------------------------------
5638
var rendered = false
5739
var renderTimeout
5840

5941
// If renderDelay is manual, then listen for an event and don't automatically render
60-
if (options.renderDelay === 'manual') page.onCallback = renderNow
42+
if (options.renderDelay === 'manual') {
43+
page.onCallback = function (message) {
44+
setTimeout(renderNow, 0)
45+
return message
46+
}
47+
}
6148

6249
page.onLoadFinished = function () {
63-
if (typeof options.renderDelay === 'manual') return
50+
if (options.renderDelay === 'manual') return
6451
renderTimeout = setTimeout(renderNow, Math.floor(options.renderDelay) || 0)
6552
}
6653

@@ -76,14 +63,33 @@ function renderNow () {
7663
}
7764

7865
var filename = options.filename || (options.directory || '/tmp') + '/html-pdf-' + system.pid + '.' + fileOptions.type
79-
8066
page.render(filename, fileOptions)
8167

8268
// Output to parent process
8369
system.stdout.write(JSON.stringify({filename: filename}))
8470
exit(null)
8571
}
8672

73+
// Set Content and begin loading
74+
// -----------------------------
75+
if (options.httpHeaders) page.customHeaders = options.httpHeaders
76+
if (options.viewportSize) page.viewportSize = options.viewportSize
77+
if (options.zoomFactor) page.zoomFactor = options.zoomFactor
78+
if (options.base) page.setContent(json.html, options.base)
79+
else page.setContent(json.html, null)
80+
81+
page.onError = function (msg, trace) {
82+
exit(buildStack('Evaluation - ' + msg, trace))
83+
}
84+
85+
// Force cleanup after 2 minutes
86+
// Add 2 seconds to make sure master process triggers kill
87+
// before to the phantom process
88+
var timeout = (options.timeout || 120000) + 2000
89+
setTimeout(function () {
90+
exit('Force timeout')
91+
}, timeout)
92+
8793
// Returns a hash of HTML content
8894
// ------------------------------
8995
function getContent (page) {

test/callback.html

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<html>
2+
<head>
3+
<meta charset="utf-8">
4+
<style>
5+
body {
6+
font-family: "Helvetica Neue", sans-serif;
7+
background: #F0F0F0;
8+
color: #333;
9+
}
10+
11+
* {
12+
border: 0;
13+
margin: 0;
14+
padding: 0;
15+
}
16+
</style>
17+
</head>
18+
<body>
19+
<div id="pageHeader">Header</div>
20+
<div id="pageContent"></div>
21+
<div id="pageFooter">Footer</div>
22+
23+
<script>
24+
setTimeout(function () {
25+
var res = window.callPhantom({success: 'true'})
26+
document.getElementById('pageContent').innerHTML = JSON.stringify(res)
27+
}, 1000)
28+
</script>
29+
</body>
30+
</html>

test/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,22 @@ test('pdf.create(html, {renderDelay: 1000}).toBuffer(callback)', function (t) {
8383
})
8484
})
8585

86+
test('window.callPhantom renders page', function (t) {
87+
t.plan(3)
88+
89+
var callbackHtml = fs.readFileSync(path.join(__dirname, 'callback.html'), 'utf8')
90+
var file = path.join(__dirname, 'callback.pdf')
91+
var startTime = new Date().getTime()
92+
93+
pdf.create(callbackHtml, {renderDelay: 'manual'}).toFile(file, function (err, pdf) {
94+
var endTime = new Date().getTime()
95+
t.error(err)
96+
97+
var time = endTime - startTime
98+
t.assert(time > 1000 && time < 2000, 'rendered in response to callPhantom')
99+
t.assert(fs.existsSync(file), 'writes the file to the given destination')
100+
})
101+
})
86102

87103
test('pdf.create(html[, options]).toStream(callback)', function (t) {
88104
t.plan(3)

0 commit comments

Comments
 (0)