marcbachmann / node-html-pdf

This repo isn't maintained anymore as phantomjs got dreprecated a long time ago. Please migrate to headless chrome/puppeteer.
MIT License
3.55k stars 544 forks source link

Broken PDF generated in server #425

Open antiframes opened 6 years ago

antiframes commented 6 years ago

I am developing a nodejs application which works fine when I run on localhost, but I get a blank PDF after uploading it to AWS serverless. That's my code:

pdf.create(html,{"format": "A4"}).toStream(function(err, stream){ res.writeHead(200, {"Content-type": "application/pdf"}); stream.pipe(res); });

It's not actually a blank PDF, but the content is considerably different. The same thing happens if I choose buffer instead of stream.

b4usat commented 6 years ago

same problem for me. I am sending my entire html into node api which uses html-pdf to generate pdf. In local its works like charm. But after deployment my pdf looks weired. Fonts are too bigger and alignment is all gone.

nguyenminhtiend commented 6 years ago

I got the same issue, not sure need any configuration in server?

nauberinho commented 6 years ago

Ive got the same issue. A blank pdf is generated when piping to client side. Is there a way to compress the stream before piping?

oqx commented 4 years ago

Ive got the same issue. A blank pdf is generated when piping to client side. Is there a way to compress the stream before piping?

I had an issue where the PDF was corrupted during output from server to client. I managed to fix it by adding a gzip transform pipe prior to piping to res.

Here's the gist:

const { createGzip } = require('zlib')

  res.writeHead(200, {
    'Content-Type': 'application/pdf; charset=utf-8',
    'Content-Disposition': 'attachment; filename=some_doc.pdf;',
    'Content-Encoding': 'gzip'
  })

  pdf.create(html, options).toStream((err, stream) => {
    if (err) {
      console.log(err)
      return res.sendStatus(500)
    } else {
      stream.pipe(createGzip()).pipe(res)
    }
  })