Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

361
Visualizações
Node express HTML to PDF

I'm looking to render a pdf version of a webpage directly to the browser using express. Something like express.render() only render the page as pdf

I have found a module that will convert HTML or a URL to a pdf

https://github.com/marcbachmann/node-html-pdf

What i need to know is how can I use the response from that libary directly in a HTTP route handler to respond to requests with the PDF, I would prefer not to store the PDF, I just want to render it on the fly, and return it as a buffer or a stream to the browser

This is the basic API the module provides:

var pdf = require('html-pdf');
pdf.create(html).toFile([filepath, ]function(err, res){
  console.log(res.filename);
});

pdf.create(html).toStream(function(err, stream){
  stream.pipe(fs.createWriteStream('./foo.pdf'));
});

pdf.create(html).toBuffer(function(err, buffer){
  console.log('This is a buffer:', Buffer.isBuffer(buffer));
});

I want to use one of these methods stream or buffer and wrap it in a route handler like this one:

router.get('invoice/pdf', function(req, res) {
    res.status(200).send(..pdf data);
});
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

This is quite easy to do in Node just using streams. The primary reason to use a stream over a Buffer is that a stream doesn't need to keep all of its data in memory like a Buffer does. Instead it can provide the data as needed to the reader or writer. This means that it is lightweight and will be better for performance in regards to latency and throughput.

In your case, you just want to pipe() the contents of the stream directly to your res object.

router.get('/invoice/pdf', (req, res) => {
  pdf.create(html).toStream((err, pdfStream) => {
    if (err) {   
      // handle error and return a error response code
      console.log(err)
      return res.sendStatus(500)
    } else {
      // send a status code of 200 OK
      res.statusCode = 200             

      // once we are done reading end the response
      pdfStream.on('end', () => {
        // done reading
        return res.end()
      })

      // pipe the contents of the PDF directly to the response
      pdfStream.pipe(res)
    }
  })
})
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda