【问题标题】:how to convert pdf array buffer to pdf file using nodejs如何使用nodejs将pdf数组缓冲区转换为pdf文件
【发布时间】:2020-03-04 09:16:49
【问题描述】:

以下是 pdf 缓冲区。我想将缓冲区转换为 pdf 文件。

data = {
    "file": {
        "type": "Buffer",
        "data": [
            102,
            24,
            62
        ]
    },
}
res.send( data );

【问题讨论】:

  • 缓冲区从何而来?如果您正在创建它,那么您应该将其write 放入一个文件并将其发回。
  • 缓冲区是pdf文件
  • 那为什么不直接把文件发回去呢?您可以创建一个 readstream 并将其传递给响应
  • 我试图发送 pdf 文件以及相关的 json 数据 {status:200, fileBuffer: fileBuffer, "otherJsonData": {"a": 1, "b":2} } 但它是无法同时使用 res.download 和 res.send,所以我将 pdf 文件转换为缓冲区然后发送它

标签: node.js express httpresponse pdfkit


【解决方案1】:

您可以使用res.sendFile 并使用标头发送一些数据。

// res.sendFile(path [, options] [, fn])

let options = {
  headers: {
      'TheDataYouWantToSend': Date.now() // for example a timestamp
  }
}

req.sendFile(absolutePathToFile, options)

希望对你有帮助

【讨论】:

  • 如何设置自定义标题?它抛出错误 { "statusCode": 425, "statusMsg": "Invalid character in header content [\"dataArr\"]" }
  • 我正在尝试在标头中传递 json 数组 {"Content-Type": "application/json", "dataArr": [{"a": 1, "b": 2}] } )
  • 我认为你可以在数组上使用 JSON.stringify,然后在前端解析它。此外,您可能不想将内容类型设置为 json,因为您正在发送文件。
猜你喜欢
  • 2023-03-02
  • 1970-01-01
  • 2021-01-14
  • 2018-09-18
  • 1970-01-01
  • 2021-06-24
  • 1970-01-01
  • 2016-11-20
相关资源
最近更新 更多