简体   繁体   中英

How to download pdf with pdfkit in node

Good morning, I wanted to be able to open the pdf after generating it. Or download it. I tried several ways but I couldn't. I'll leave a bit of my code below.

I'm using node for backend and react for fontend

const PDFDocument = require('pdfkit');
const doc = new PDFDocument;

routes.post('/pdf', (req, res, next) => {
  doc.pipe(fs.createWriteStream('file.pdf')); // write to PDF
  doc.pipe(res);                                       

  doc.fontSize(25)
  .text('text', 100, 100);

  doc.end();
});

At the end of your routes.post, you can return the path of the new pdf file:

res.json({path: '/file.pdf'});

and then open it in React:

window.open(response.data.path, '_blank');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM