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

239
Visualizações
How to send the pdf file as res in loopback remote method?

I am defining my remote method of loopback as follows

Visitor.remoteMethod('getVisitorsPDF', {
description: 'Get visitors list in PDF file',
accepts: [
  { arg: 'res', type: 'object', http: { source: 'res' } },
  { arg: 'dateInStart', type: 'string' },
  { arg: 'dateInEnd', type: 'string' },
  { arg: 'employeeSiteId', type: 'string' },
  { arg: 'name', type: 'string' },
  { arg: 'visitorCompany', type: 'string' },
  { arg: 'employeeName', type: 'string' },
  { arg: 'typeId', type: 'number' },
  { arg: 'shift', type: 'number' }
],
returns: {},
http: { path: '/getpdf', verb: 'get' }
});

and its implementation is as follows:

   Visitor.app.models.user.testAccess(res).then(
      (current) => {
        ....
        res.set('Content-Type', 'application/pdf');
        res.set('Content-Disposition', 'attachment;filename="visitors.pdf"');
        ...
        Visitor._getVisitors(query, current).then(
          (visitors) => {
            let visitorsFiltered = visitors.filter((v) => {
              if (!v.tztimeIn) return v;
              let d = v.timeIn + v.tztimeIn * 60000;
              if (dStart < d && d < dEnd) return v;
            });
            Visitor._getVisitorsPDF(visitorsFiltered)
              .then(result => {
                res.send(result);
              },
              );
          },
        );

      }
    )
  };

And the function that is generating pdf is follows:

Visitor._getVisitorsPDF = async function (visitors) {
    ...
    const rows = tempData;
    const doc = new jsPDF();
    doc.autoTable(columns, rows);
    doc.save('visitors.pdf');
    
  } 

how to send this doc back as application/pdf response ? I am stuck here

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I have used node-html-pdf to generate and send PDFs. It has built in method to return a buffer.

In your code, doc.save('file') saves the file to the pwd.

Then you could use fs to read the saved file asynchronously then buffer it to the client

import stream from "stream";
...

fs.readFile('file', (err, fileBuffer) => {
  if (err) throw err;
  // console.log(fileBuffer);

  let encodedBuffer = Buffer.from(JSON.stringify(fileBuffer));
  // create a stream to the receiver
  let readStream = new stream.PassThrough();
  readStream.end(encodedBuffer);
  res.set("Content-disposition", "attachment; filename=" + filename);
  res.set("Content-Type", "application/pdf");
  readStream.pipe(res);    
 });
about 4 years ago · Juan Pablo Isaza Relatório

0

Where res the regular the response object (of type Request in @loopback/rest), you can do:

res.download(result)

Instead of

res.send(result).
about 4 years ago · Juan Pablo Isaza 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