Skip to content

Commit

Permalink
Move example html to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
marcbachmann committed Jan 25, 2015
1 parent 517d307 commit f24bc83
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 35 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ pdf.create(html, options).toFile(function(err, res) {
## API

```js
pdf.create(html [, options]).toFile(callback)
pdf.create(html [, options]).toFile([filepath, ]callback)
pdf.create(html [, options]).toBuffer(callback)
pdf.create(html [, options]).toStream(callback)

// for backward compatibility
// for backwards compatibility
pdf.create(html [, options], callback)

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

// Export options
"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.
"directory": "/tmp" // The directory the file gets written into if no filename is defined. default: '/tmp'
"directory": "/tmp" // The directory the file gets written into if not using .toFile(filename, callback). default: '/tmp'

// Papersize Options: http://phantomjs.org/api/webpage/property/paper-size.html
"height": "10.5in", // allowed units: mm, cm, in, px
Expand Down
7 changes: 3 additions & 4 deletions lib/pdf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions lib/scripts/pdf_a4_portrait.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/pdf.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module.exports = class PDF
@script = path.join(__dirname, 'scripts', 'pdf_a4_portrait.js')

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


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


toFile: (filename, callback) ->
assert(arguments.length > 0, 'html-pdf: The method pdf.toFile([filename, ]callback) requires a callback.')
assert(arguments.length > 0, 'html-pdf: The method .toFile([filename, ]callback) requires a callback.')
if filename instanceof Function
callback = filename
filename = undefined
else
@options.filename = filename
@options.filename = path.resolve(filename)

@exec(callback)

Expand Down
6 changes: 3 additions & 3 deletions src/scripts/pdf_a4_portrait.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ exit = (error) ->
buildStack = (msg, trace) ->
msgStack = [msg]
if trace?.length
msgStack.push('TRACE:')
msgStack.push('Stack:')
trace.forEach (t) ->
msgStack.push(" at #{t.file || t.sourceURL}: #{t.line} (in function #{t.function})")
msgStack.join('\n')
Expand Down Expand Up @@ -45,7 +45,7 @@ page.onError = (msg, trace) ->

# Set up content
# --------------
content = page.evaluate (options) ->
content = page.evaluate ->
styles = document.querySelector('head style')?.outerHTML || ''
if $header = document.getElementById('pageHeader')
header = $header.outerHTML
Expand Down Expand Up @@ -88,7 +88,7 @@ setContent = (type) ->
for type in ['header', 'footer']
setContent(type) if options[type] || content[type]

paper.header?.height ?= '45mm'
paper.header?.height ?= '46mm'
paper.footer?.height ?= '28mm'


Expand Down
20 changes: 20 additions & 0 deletions test/example.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<html>
<head>
<style>
* {
border: 0;
margin: 0;
padding: 0;
}
body {
background: #F0F0F0;
color: #333;
}
</style>
</head>
<body>
<div id="pageHeader">Header</div>
<div id="pageContent">Content</div>
<div id="pageFooter">Footer</div>
</body>
</html>
24 changes: 24 additions & 0 deletions test/external-css.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<link rel="stylesheet" type="text/css" href="http://getbootstrap.com/dist/css/bootstrap.min.css">
<style>
body {
padding: 1cm;
}

.jumbotron {
background: #F0F0F0!important;
}

.btn {
background: rgb(66,133,243)!important;
}
</style>
<div class="container">
<div class="jumbotron">
<h1>Navbar example</h1>
<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>
<p>To see the difference between static and fixed top navbars, just scroll.</p>
<p>
<a class="btn btn-lg btn-primary" href="http://getbootstrap.com/components/#navbar" role="button">View navbar docs &raquo;</a>
</p>
</div>
</div>
36 changes: 18 additions & 18 deletions test/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,7 @@ test.createStream()
fs = require('fs')
path = require('path')
pdf = require('../')


html = """
<html>
<head></head>
<body>
<div id="pageHeader">Header</div>
<div id="pageContent">Content</div>
<div id="pageFooter">Footer</div>
</body>
</html>
"""
html = fs.readFileSync(path.join(__dirname, 'example.html'), 'utf8')

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

file = path.join(__dirname,'simple.pdf')
file = path.join(__dirname, 'simple.pdf')
pdf.create(html).toFile file, (err, pdf) ->
st.error(err)
st.assert(pdf.filename == file, "toFile(filename, callback) returns {filename: '#{pdf.filename}'} as second cb argument")
Expand All @@ -82,7 +71,7 @@ test 'pdf.create(html[, options]).toStream(callback)', (st) ->
stream = pdf.create(html).toStream (err, stream) ->
st.error(err)
st.assert(stream instanceof fs.ReadStream, "toStream(callback) returns a fs.ReadStream as second cb argument")
destination = __dirname+'/streamed.pdf'
destination = path.join(__dirname, 'streamed.pdf')
stream.pipe fs.createWriteStream(destination)
stream.on 'end', ->
st.assert(fs.existsSync(destination), 'toStream returns a working readable stream')
Expand All @@ -95,14 +84,14 @@ test 'pdf.create(html[, options]).toStream(callback)', (st) ->
test 'allows custom html and css', (st) ->
st.plan(3)

template = path.join(__dirname,'businesscard.html')
template = path.join(__dirname, 'businesscard.html')
filename = template.replace('.html', '.pdf')
html = fs.readFileSync(template, 'utf8')
templateHtml = fs.readFileSync(template, 'utf8')
options =
width: '50mm'
height: '90mm'

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

filename = path.join(__dirname,'custom.pdf')
filename = path.join(__dirname, 'custom.pdf')
options =
width: '3in'
height: '7in'
Expand All @@ -122,3 +111,14 @@ test 'allows custom page and footer options', (st) ->
st.error(error)
st.assert(pdf.filename == filename, 'Returns the filename from the phantom script')
st.assert(fs.existsSync(pdf.filename), 'Saves the pdf with a custom page size and footer')


test 'load external css', (st) ->
st.plan(3)

enrichedHtml = fs.readFileSync(path.join(__dirname, 'external-css.html'), 'utf8')
filename = path.join(__dirname, 'external-css.pdf')
pdf.create(enrichedHtml).toFile filename, (error, pdf) ->
st.error(error)
st.assert(pdf.filename == filename, 'Returns the filename from the phantom script')
st.assert(fs.existsSync(pdf.filename), 'Saves the pdf with a custom page size and footer')

0 comments on commit f24bc83

Please sign in to comment.