I am using a reporting software which returns me my report in an PDF file as part of a JSON doc in the data event and its base64 encoded. When i take the data i get from the web service and write it to file i get a file which seems to corrupt or so as it does not display the report data.
request(options, async(err, resp, body) => {
var bytes = await base64.decode(body.Data);
fs.writeFile("./Sample.pdf" , bytes , function(err) {
if(err) {
return console.log(err);
}
console.log("The file was saved!");
});
so not sure what i am missing here. I know i got a valid encoded string because i can go to Free Formatter and paste my string and when i click decode and download i have a valid PDF as expected.
Here is a sample of the encoded file which im trying to decode and save as a pdf..
After some trial and error i figured out the issue. When you have a base64 encoded file and you want to save as a binary you do not need to decode the file. you can just tell the fs.writeFile that you have a base64 encoded file and it will do the rest
fs.writeFile("./SamplePdf.pdf", body.Data, {encoding: 'base64'}, async(err, data) => {
if (err) {
console.log('err', err);
}
console.log('success');
});
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.