如何在Node环境下将BLOB转成PDF文件?

How to convert BLOB into PDF file in the Node environment?

我有一个 Node Js 服务器,我在其中从另一个 Web 服务(用于 PDF 文件)获取 blob 数据,现在在收到 blob 之后,我想再次将其转换为 PDF 文件。

任何知道如何实现这个的人请帮忙。

这是我到目前为止尝试过的代码块:

const fetch = require('node-fetch');
const Blob = require('fetch-blob');
const fs = require('fs');

fetch(url, options)
   .then(res => {
      console.log(res);
      res.blob().then(async (data) => {

         const result = data.stream();
         // below line of code saves a blank pdf file
         fs.createWriteStream(objectId + '.pdf').write(result);
      })
   })
   .catch(e => {
      console.log(e);
   });

修改点:

  • 对于fs.createWriteStream(objectId + '.pdf').write(data),请将res.blob()修改为res.buffer()
  • 请将.then(res => {res.blob().then()修改为.then(res => res.buffer()).then(

修改后的脚本:

fetch(url, options)
  .then(res => res.buffer())
  .then(data => {
    fs.createWriteStream(objectId + '.pdf').write(data);
  })
  .catch(e => {
    console.log(e);
  });

注:

  • 在此修改中,假设使用 urloptions 的提取过程工作正常。

参考文献: