Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

236
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!