Skip to content

Commit

Permalink
Add filename support
Browse files Browse the repository at this point in the history
Specify the filename and disposition of the PDF using the Content-Disposition header. If no filename is specified via metadata, generate a UUID.
  • Loading branch information
danielwestendorf committed Jun 19, 2018
1 parent 8a0aa9f commit aa4f4a5
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,6 @@ Set `DEBUG` Config var to `breezy-pdf-lite:*` to get debugged output.

### Run Tests

`$ /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --interpreter none --headless --disable-gpu --disable-translate --disable-extensions --disable-background-networking --safebrowsing-disable-auto-update --disable-sync --metrics-recording-only --disable-default-apps --no-first-run --mute-audio --hide-scrollbars --remote-debugging-port=9222`

`$ npm test`
5 changes: 5 additions & 0 deletions lib/render.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const htmlPdf = require('html-pdf-chrome')
const debug = require('debug')
const uuid = require('uuid')
const meta = require('./meta')
const PrintOptions = require('./pdf/print-options')
const CompletionTrigger = require('./pdf/completion-trigger')
Expand Down Expand Up @@ -33,6 +34,10 @@ module.exports = class Render {
return this.metadata
}

filename() {
return `${this.meta().filename || uuid()}.pdf`
}

printOptions() {
return new PrintOptions(this.meta()).build()
}
Expand Down
4 changes: 3 additions & 1 deletion lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ module.exports = class Server {
const renderer = new Render(req.body)
try {
pdf = await renderer.toPdf()
res.status(201).send(pdf.toBuffer())
res.status(201)
.set('Content-Disposition', `attachment; filename="${renderer.filename()}"`)
.send(pdf.toBuffer())
} catch (error) {
this.log(error)
res.status(500).send('Encountered an error while generating PDF. Check the server logs for more details')
Expand Down
5 changes: 5 additions & 0 deletions test/lib/integration/web-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const port = server.port,
const htmlString = `
<html>
<head>
<meta name='breezy-pdf-filename' content='fancyfilename'/>
<meta name='breezy-pdf-width' content='10'/>
</head>
<body>
Expand Down Expand Up @@ -50,6 +51,10 @@ module.exports = {

assert(Buffer, buffer.constructor)
done()
},

contentDisposition() {
assert.equal(this.response.headers.get('content-disposition'), 'attachment; filename="fancyfilename.pdf"')
}
},

Expand Down
11 changes: 11 additions & 0 deletions test/lib/render-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ let Render
const htmlString = `
<html>
<head>
<meta name='breezy-pdf-filename' content='fancyfilename'/>
<meta name='breezy-pdf-width' content='10'/>
</head>
<body>
Expand Down Expand Up @@ -38,5 +39,15 @@ module.exports = {
toPdf() {
td.when(this.htmlPdf.create(htmlString, td.matchers.isA(Object))).thenReturn(true)
assert.equal(true, new Render(htmlString).toPdf())
},

filename: {
specified() {
assert.equal('fancyfilename.pdf', new Render(htmlString).filename())
},

notSpecified() {
assert(new Render('').filename().match(/\.pdf$/))
}
}
}

0 comments on commit aa4f4a5

Please sign in to comment.