markdown-pdf
markdown-pdf copied to clipboard
Hard time using buffer for express response. Buffer example ?
Hello,
I'm having trouble using the buffer output system to stream back the resulting PDF in Expressjs.
Basically, I have a route that sends a markdown document as parameter, and expects a response of type application/pdf.
I tried a few solutions but I really don't know how to use to.buffer
to buffer to the Express response object without having to go through a temporary file which would then be statically sent and removed.
Could a buffer example be added to the README ?
By digging into the stream-from-to module (the method to.buffer
), I could finally understand how it worked.
I needed some time to understand how it actually behaved, so maybe the documentation could specify the callback format (I now know that the callback format is "standard", but it may be simpler to understand to simply give the callback prototype in the README, like function (error, data?)
.
you can use like this:
return new Promise((resolve, reject) => {
markdownPdf().from(path).to.buffer(null, function(err, PdfBuffer) {
if (err) {
reject(e)
} else {
resolve(PdfBuffer)
}
})
})
I have a working example:
app.post('/convert', (req, res) => {
const text = req.body.text
const filename = req.body.filename
markdownpdf()
.from.string(text)
.to.buffer({}, (error, buf) => {
if (error) {
return res.status(500).send(error)
}
res.set({
'Content-Disposition': `attachment; filename=${filename}.pdf`,
'Content-Type': 'application/pdf'
})
res.send(buf)
})
})