Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@vnglst
Last active November 4, 2016 12:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vnglst/b15187c065dd4f065c0949b6177c6cab to your computer and use it in GitHub Desktop.
Save vnglst/b15187c065dd4f065c0949b6177c6cab to your computer and use it in GitHub Desktop.
const express = require('express')
const router = express.Router()
const PDFDocument = require('pdfkit')
router.post('/', (req, res) => {
const doc = new PDFDocument()
let filename = req.body.filename
// Stripping special characters
filename = encodeURIComponent(filename) + '.pdf'
// Setting response to 'attachment' (download).
// If you use 'inline' here it will automatically open the PDF
res.setHeader('Content-disposition', 'attachment; filename="' + filename + '"')
res.setHeader('Content-type', 'application/pdf')
const content = req.body.content
doc.y = 300
doc.text(content, 50, 50)
doc.pipe(res)
doc.end()
})
module.exports = router
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment