How can i stream the pdf to a url so that users are able to download it via browsers ?
@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)
})
I've added an example to https://github.com/marcbachmann/node-html-pdf/blob/master/examples/serve-http/index.js
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);
});