【问题标题】:How to download pdf with pdfkit in node如何在节点中使用 pdfkit 下载 pdf
【发布时间】:2020-04-13 15:27:28
【问题描述】:

早上好,我希望能够在生成 pdf 后打开它。 或者下载它。 我尝试了几种方法,但我做不到。 我会在下面留下一些我的代码。

我将 node 用于后端并为 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();
});

【问题讨论】:

    标签: node.js reactjs pdfkit node-pdfkit


    【解决方案1】:

    在你的 routes.post 的末尾,你可以返回新的 pdf 文件的路径:

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

    然后在 React 中打开它:

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

    【讨论】:

    • 但是我发现了。但我会投票,因为它是正确的。
    猜你喜欢
    • 2017-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多