Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

241
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda