简体   繁体   中英

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

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

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM