javascript - How to convert BLOB into PDF file in the Node environment? - Stack Overflow

admin2025-04-20  0

I have a Node Js server, in it, I am fetching a blob data from another web service (which is for a PDF file), now after receiving blob, I want to convert it again into PDF file.

Anyone, who knows how to achieve this please help.

Here is my code block I have tried so far:

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);
   });

I have a Node Js server, in it, I am fetching a blob data from another web service (which is for a PDF file), now after receiving blob, I want to convert it again into PDF file.

Anyone, who knows how to achieve this please help.

Here is my code block I have tried so far:

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);
   });
Share Improve this question edited Apr 16, 2020 at 9:09 Zuckerberg 2,1712 gold badges12 silver badges19 bronze badges asked Apr 16, 2020 at 6:00 Mayank yaduvanshiMayank yaduvanshi 1701 silver badge7 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 3

Modification points:

  • For fs.createWriteStream(objectId + '.pdf').write(data), please modify res.blob() to res.buffer().
  • Please modify .then(res => {res.blob().then() to .then(res => res.buffer()).then(.

Modified script:

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

Note:

  • In this modification, it supposes that the fetch process using url and options works fine.

References:

  • node-fetch
  • write()
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745149519a287537.html

最新回复(0)