markdown-pdf icon indicating copy to clipboard operation
markdown-pdf copied to clipboard

Hard time using buffer for express response. Buffer example ?

Open Arteneko opened this issue 5 years ago • 3 comments

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 ?

Arteneko avatar Jul 30 '18 10:07 Arteneko

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?).

Arteneko avatar Jul 30 '18 12:07 Arteneko

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)
                }
            })
        })

Richard-Choooou avatar Jan 08 '19 06:01 Richard-Choooou

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)
    })
})

olastor avatar Apr 13 '19 06:04 olastor