Skip to content

Commit f24bc83

Browse files
committed
Move example html to separate file
1 parent 517d307 commit f24bc83

8 files changed

Lines changed: 77 additions & 35 deletions

File tree

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ pdf.create(html, options).toFile(function(err, res) {
2323
## API
2424

2525
```js
26-
pdf.create(html [, options]).toFile(callback)
26+
pdf.create(html [, options]).toFile([filepath, ]callback)
2727
pdf.create(html [, options]).toBuffer(callback)
2828
pdf.create(html [, options]).toStream(callback)
2929

30-
// for backward compatibility
30+
// for backwards compatibility
3131
pdf.create(html [, options], callback)
3232

3333
```
@@ -38,8 +38,7 @@ pdf.create(html [, options], callback)
3838
config = {
3939

4040
// Export options
41-
"filename": "/tmp/html-pdf-123-123.pdf" // The file path of the file that will be written. If you want to save the file permanently, you have to pass this option.
42-
"directory": "/tmp" // The directory the file gets written into if no filename is defined. default: '/tmp'
41+
"directory": "/tmp" // The directory the file gets written into if not using .toFile(filename, callback). default: '/tmp'
4342

4443
// Papersize Options: http://phantomjs.org/api/webpage/property/paper-size.html
4544
"height": "10.5in", // allowed units: mm, cm, in, px

lib/pdf.js

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

lib/scripts/pdf_a4_portrait.js

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

src/pdf.coffee

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ module.exports = class PDF
2626
@script = path.join(__dirname, 'scripts', 'pdf_a4_portrait.js')
2727

2828
@options.filename = path.resolve(@options.filename) if @options.filename
29-
assert(@html?.length, "html-pdf: Can't create a pdf without content")
29+
assert(typeof @html is 'string' && @html.length, "html-pdf: Can't create a pdf without an html string")
3030

3131

3232
toBuffer: (callback) ->
@@ -52,12 +52,12 @@ module.exports = class PDF
5252

5353

5454
toFile: (filename, callback) ->
55-
assert(arguments.length > 0, 'html-pdf: The method pdf.toFile([filename, ]callback) requires a callback.')
55+
assert(arguments.length > 0, 'html-pdf: The method .toFile([filename, ]callback) requires a callback.')
5656
if filename instanceof Function
5757
callback = filename
5858
filename = undefined
5959
else
60-
@options.filename = filename
60+
@options.filename = path.resolve(filename)
6161

6262
@exec(callback)
6363

src/scripts/pdf_a4_portrait.coffee

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ exit = (error) ->
1212
buildStack = (msg, trace) ->
1313
msgStack = [msg]
1414
if trace?.length
15-
msgStack.push('TRACE:')
15+
msgStack.push('Stack:')
1616
trace.forEach (t) ->
1717
msgStack.push(" at #{t.file || t.sourceURL}: #{t.line} (in function #{t.function})")
1818
msgStack.join('\n')
@@ -45,7 +45,7 @@ page.onError = (msg, trace) ->
4545

4646
# Set up content
4747
# --------------
48-
content = page.evaluate (options) ->
48+
content = page.evaluate ->
4949
styles = document.querySelector('head style')?.outerHTML || ''
5050
if $header = document.getElementById('pageHeader')
5151
header = $header.outerHTML
@@ -88,7 +88,7 @@ setContent = (type) ->
8888
for type in ['header', 'footer']
8989
setContent(type) if options[type] || content[type]
9090

91-
paper.header?.height ?= '45mm'
91+
paper.header?.height ?= '46mm'
9292
paper.footer?.height ?= '28mm'
9393

9494

test/example.html

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<html>
2+
<head>
3+
<style>
4+
* {
5+
border: 0;
6+
margin: 0;
7+
padding: 0;
8+
}
9+
body {
10+
background: #F0F0F0;
11+
color: #333;
12+
}
13+
</style>
14+
</head>
15+
<body>
16+
<div id="pageHeader">Header</div>
17+
<div id="pageContent">Content</div>
18+
<div id="pageFooter">Footer</div>
19+
</body>
20+
</html>

test/external-css.html

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<link rel="stylesheet" type="text/css" href="http://getbootstrap.com/dist/css/bootstrap.min.css">
2+
<style>
3+
body {
4+
padding: 1cm;
5+
}
6+
7+
.jumbotron {
8+
background: #F0F0F0!important;
9+
}
10+
11+
.btn {
12+
background: rgb(66,133,243)!important;
13+
}
14+
</style>
15+
<div class="container">
16+
<div class="jumbotron">
17+
<h1>Navbar example</h1>
18+
<p>This example is a quick exercise to illustrate how the default, static and fixed to top navbar work. It includes the responsive CSS and HTML, so it also adapts to your viewport and device.</p>
19+
<p>To see the difference between static and fixed top navbars, just scroll.</p>
20+
<p>
21+
<a class="btn btn-lg btn-primary" href="http://getbootstrap.com/components/#navbar" role="button">View navbar docs &raquo;</a>
22+
</p>
23+
</div>
24+
</div>

test/index.coffee

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,7 @@ test.createStream()
77
fs = require('fs')
88
path = require('path')
99
pdf = require('../')
10-
11-
12-
html = """
13-
<html>
14-
<head></head>
15-
<body>
16-
<div id="pageHeader">Header</div>
17-
<div id="pageContent">Content</div>
18-
<div id="pageFooter">Footer</div>
19-
</body>
20-
</html>
21-
"""
10+
html = fs.readFileSync(path.join(__dirname, 'example.html'), 'utf8')
2211

2312
#
2413
# API
@@ -60,7 +49,7 @@ test 'pdf.create(html[, options]).toFile([filename, ]callback)', (st) ->
6049
st.assert(typeof pdf.filename == 'string', "toFile(callback) returns {filename: '#{pdf.filename}'} as second cb argument")
6150
fs.unlink(pdf.filename)
6251

63-
file = path.join(__dirname,'simple.pdf')
52+
file = path.join(__dirname, 'simple.pdf')
6453
pdf.create(html).toFile file, (err, pdf) ->
6554
st.error(err)
6655
st.assert(pdf.filename == file, "toFile(filename, callback) returns {filename: '#{pdf.filename}'} as second cb argument")
@@ -82,7 +71,7 @@ test 'pdf.create(html[, options]).toStream(callback)', (st) ->
8271
stream = pdf.create(html).toStream (err, stream) ->
8372
st.error(err)
8473
st.assert(stream instanceof fs.ReadStream, "toStream(callback) returns a fs.ReadStream as second cb argument")
85-
destination = __dirname+'/streamed.pdf'
74+
destination = path.join(__dirname, 'streamed.pdf')
8675
stream.pipe fs.createWriteStream(destination)
8776
stream.on 'end', ->
8877
st.assert(fs.existsSync(destination), 'toStream returns a working readable stream')
@@ -95,14 +84,14 @@ test 'pdf.create(html[, options]).toStream(callback)', (st) ->
9584
test 'allows custom html and css', (st) ->
9685
st.plan(3)
9786

98-
template = path.join(__dirname,'businesscard.html')
87+
template = path.join(__dirname, 'businesscard.html')
9988
filename = template.replace('.html', '.pdf')
100-
html = fs.readFileSync(template, 'utf8')
89+
templateHtml = fs.readFileSync(template, 'utf8')
10190
options =
10291
width: '50mm'
10392
height: '90mm'
10493

105-
pdf.create(html, options).toFile filename, (err, pdf) =>
94+
pdf.create(templateHtml, options).toFile filename, (err, pdf) =>
10695
st.error(err)
10796
st.assert(pdf.filename, 'Returns the filename')
10897
st.assert(fs.existsSync(pdf.filename), 'Saves the file to the desired destination')
@@ -111,7 +100,7 @@ test 'allows custom html and css', (st) ->
111100
test 'allows custom page and footer options', (st) ->
112101
st.plan(3)
113102

114-
filename = path.join(__dirname,'custom.pdf')
103+
filename = path.join(__dirname, 'custom.pdf')
115104
options =
116105
width: '3in'
117106
height: '7in'
@@ -122,3 +111,14 @@ test 'allows custom page and footer options', (st) ->
122111
st.error(error)
123112
st.assert(pdf.filename == filename, 'Returns the filename from the phantom script')
124113
st.assert(fs.existsSync(pdf.filename), 'Saves the pdf with a custom page size and footer')
114+
115+
116+
test 'load external css', (st) ->
117+
st.plan(3)
118+
119+
enrichedHtml = fs.readFileSync(path.join(__dirname, 'external-css.html'), 'utf8')
120+
filename = path.join(__dirname, 'external-css.pdf')
121+
pdf.create(enrichedHtml).toFile filename, (error, pdf) ->
122+
st.error(error)
123+
st.assert(pdf.filename == filename, 'Returns the filename from the phantom script')
124+
st.assert(fs.existsSync(pdf.filename), 'Saves the pdf with a custom page size and footer')

0 commit comments

Comments
 (0)