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
1 Answer
Reset to default 3Modification points:
- For
fs.createWriteStream(objectId + '.pdf').write(data)
, please modifyres.blob()
tores.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
andoptions
works fine.
References:
- node-fetch
- write()