【问题标题】:Send binary pdf file with express使用 express 发送二进制 pdf 文件
【发布时间】:2016-08-08 15:21:35
【问题描述】:

我使用html-pdf 包作为节点,我需要发送文件作为响应。 这是我在 pdf.create 函数上的实际代码

pdf.create(html, options).toStream((err, stream) => {
  if(err) throw err;
  stream.pipe(fs.createWriteStream(__dirname + '/gen.pdf'));


  var file = fs.createReadStream(__dirname + '/gen.pdf');
  //file.pipe(res);

  res.setHeader('Content-Type', 'application/pdf');
  res.setHeader('Content-Disposition', 'attachment; filename="gen.pdf"');
  res.sendFile(__dirname+'/gen.pdf');
});

【问题讨论】:

    标签: node.js file pdf express binaryfiles


    【解决方案1】:

    为什么不直接流式传输到响应?例如:

    pdf.create(html, options).toStream((err, stream) => {
      if (err) throw err;
      res.setHeader('Content-Type', 'application/pdf');
      res.setHeader('Content-Disposition', 'attachment; filename="gen.pdf"');
      stream.pipe(res);
    });
    

    【讨论】:

    • 我正在使用 postman(chrome 应用程序)进行测试,我得到一个名为 response.pdf.pdf 的 txt 文件
    猜你喜欢
    • 2020-07-20
    • 1970-01-01
    • 2015-07-01
    • 2015-04-09
    • 2014-03-31
    • 1970-01-01
    • 2016-06-07
    • 2017-10-31
    • 1970-01-01
    相关资源
    最近更新 更多