Node-html-pdf: Stream pdf to a URL

Created on 2 Aug 2017  Â·  5Comments  Â·  Source: marcbachmann/node-html-pdf

How can i stream the pdf to a url so that users are able to download it via browsers ?

All 5 comments

@vivekiyer114 this works for me:

    `let tmpl = fs.readFileSync('./views/exporttopdf.html', 'utf8');
    let compiled = _.template(tmpl);
    let html = compiled(data);
    pdf.create(html).toStream((err, stream) => {

        res.setHeader('Content-disposition', 'inline; filename="Test.pdf"');
        res.setHeader('Content-type', 'application/pdf');

        stream.pipe(res);
    });`

I can receive string like below as ajax response.

%PDF-1.4
1 0 obj
<<
/Title (��)
/Creator (��)
/Producer (��
...
...

how can I show it in new browser tab

Don't set the Content-Disposition header. Just the Content-Type.

const http = require('http')
const pdf = require('../../')

const server = http.createServer(function (req, res) {
  const html = `<html><body>Test</body></html>`
  pdf.create(html).toStream((err, stream) => {
    if (err) return res.end(err.stack)
    res.setHeader('Content-Type', 'application/pdf')
    stream.pipe(res)
  })
})

server.listen(8080, function (err) {
  if (err) throw err
  console.log('Listening on http://localhost:%s', server.address().port)
})

The below doesn't work, still getting

PDF-1.4↵1 0 obj↵<<↵/Title (��)↵/
                htmlPdf.create(html).toStream((err, stream) => {
                    if (err) return next(err);
                    res.setHeader('Content-type', 'application/pdf');
                    stream.pipe(res);
                });
Was this page helpful?
0 / 5 - 0 ratings

Related issues

antiframes picture antiframes  Â·  4Comments

rayax86 picture rayax86  Â·  5Comments

jimit-hothi picture jimit-hothi  Â·  4Comments

Mortuie picture Mortuie  Â·  4Comments

B-StS picture B-StS  Â·  5Comments