Skip to content

Commit 2c41416

Browse files
author
Maksym Bykovskyy
committed
Fixed issue 71. Refactored code a little.
1 parent 267a031 commit 2c41416

File tree

4 files changed

+154
-104
lines changed

4 files changed

+154
-104
lines changed

lib/scripts/pdf_a4_portrait.js

Lines changed: 65 additions & 60 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/scripts/pdf_a4_portrait.coffee

Lines changed: 55 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -44,64 +44,75 @@ setTimeout ->
4444
, timeout || 12000
4545

4646

47-
# Set up content
48-
# --------------
49-
content = page.evaluate ->
50-
styles = document.querySelectorAll('link,style')
51-
styles = Array::reduce.call(styles, ((string, node) -> string+node.outerHTML),'')
52-
if $header = document.getElementById('pageHeader')
53-
header = $header.outerHTML
54-
$header.parentNode.removeChild($header)
55-
56-
if $footer = document.getElementById('pageFooter')
57-
footer = $footer.outerHTML
58-
$footer.parentNode.removeChild($footer)
59-
60-
if $body = document.getElementById('pageContent')
61-
body = $body.outerHTML
47+
# Returns a hash of HTML content
48+
# ------------------------------
49+
getContent = () ->
50+
page.evaluate ->
51+
styles = document.querySelectorAll('link,style')
52+
styles = Array::reduce.call(styles, ((string, node) -> string+node.outerHTML),'')
53+
if $header = document.getElementById('pageHeader')
54+
header = $header.outerHTML
55+
$header.parentNode.removeChild($header)
56+
57+
if $footer = document.getElementById('pageFooter')
58+
footer = $footer.outerHTML
59+
$footer.parentNode.removeChild($footer)
60+
61+
if $body = document.getElementById('pageContent')
62+
body = $body.outerHTML
63+
else
64+
body = document.body.outerHTML
65+
66+
{styles, header, body, footer}
67+
68+
69+
# Creates paper with specified options
70+
# ------------------------------------
71+
createPaper = (options) ->
72+
paper = border: options.border || '0'
73+
74+
if options.height && options.width
75+
paper.width = options.width
76+
paper.height = options.height
6277
else
63-
body = document.body.outerHTML
78+
paper.format = options.format || 'A4'
79+
paper.orientation = options.orientation || 'portrait'
6480

65-
{styles, header, body, footer}
81+
paper
6682

6783

68-
# Set up paperSize options
69-
# -------------------------
70-
paper = border: options.border || '0'
84+
# Creates page section
85+
# --------------------
86+
createSection = (content, styles, options) ->
87+
height: options?.height
88+
contents: phantom.callback (pageNum, numPages) ->
89+
(options?.contents || content || '')
90+
.replace('{{page}}', pageNum)
91+
.replace('{{pages}}', numPages)+styles
7192

72-
if options.height && options.width
73-
paper.width = options.width
74-
paper.height = options.height
75-
else
76-
paper.format = options.format || 'A4'
77-
paper.orientation = options.orientation || 'portrait'
7893

94+
# Creates paper with generated footer & header
95+
# --------------------------------------------
96+
generatePaper = (content, options) ->
97+
paper = createPaper(options)
7998

80-
# Generate footer & header
81-
# ------------------------
82-
setContent = (type) ->
83-
paper[type] =
84-
height: options[type]?.height
85-
contents: phantom.callback (pageNum, numPages) ->
86-
(options[type]?.contents || content[type] || '')
87-
.replace('{{page}}', pageNum)
88-
.replace('{{pages}}', numPages)+content.styles
99+
for section in ['header', 'footer']
100+
if options[section] || content[section]
101+
paper[section] =
102+
createSection(content[section], content.styles, options[section])
89103

90-
for type in ['header', 'footer']
91-
setContent(type) if options[type] || content[type]
104+
paper.header?.height ?= '46mm'
105+
paper.footer?.height ?= '28mm'
92106

93-
paper.header?.height ?= '46mm'
94-
paper.footer?.height ?= '28mm'
95-
96-
97-
# The paperSize object must be set at once
98-
# -----------------------------------------
99-
page.paperSize = paper
107+
paper
100108

101109

102110
# Completely load page & end process
103111
# ----------------------------------
104112
page.onLoadFinished = (status) ->
113+
# The paperSize object must be set at once
114+
page.paperSize = generatePaper(getContent(), options)
115+
105116
# Output to parent process
106117
fileOptions =
107118
type: options.type || 'pdf'

test/external-js.html

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<html>
2+
<head>
3+
<meta charset="utf-8">
4+
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
5+
<style>
6+
body {
7+
font-family: "Helvetica Neue", sans-serif;
8+
background: #F0F0F0;
9+
color: #333;
10+
}
11+
12+
* {
13+
border: 0;
14+
margin: 0;
15+
padding: 0;
16+
}
17+
</style>
18+
</head>
19+
<body>
20+
<div id="pageHeader">Header</div>
21+
<div id="pageContent">Content with Unicode Char 😃</div>
22+
<div id="pageFooter">Footer</div>
23+
</body>
24+
</html>

test/index.coffee

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,13 @@ test 'load external css', (st) ->
127127
st.error(error)
128128
st.assert(pdf.filename == filename, 'Returns the filename from the phantom script')
129129
st.assert(fs.existsSync(pdf.filename), 'Saves the pdf with a custom page size and footer')
130+
131+
test 'load external js', (st) ->
132+
st.plan(3)
133+
134+
enrichedHtml = fs.readFileSync(path.join(__dirname, 'external-js.html'), 'utf8')
135+
filename = path.join(__dirname, 'external-js.pdf')
136+
pdf.create(enrichedHtml).toFile filename, (error, pdf) ->
137+
st.error(error)
138+
st.assert(pdf.filename == filename, 'Returns the filename from the phantom script')
139+
st.assert(fs.existsSync(pdf.filename), 'Saves the pdf with a custom page size and footer')

0 commit comments

Comments
 (0)