Decoding Base64 pdf giving broken file
A related issue for me which was solved by reading @victor 's answer is where an Express.js app get's a bas64 encoded PDF from an API and wants to return it to the client as a 'proper' pdf:
res.set({
'Content-Disposition' : 'attachment; filename='+ data.fileName,
'Content-Type': 'application/pdf',
});
res.send(Buffer.from(data.content, 'base64'));
You get a corrupted PDF, because:
- According to the officially documentation, the
Base64.decode()
function decodes Base64 value to UTF-8 string. As you can see, this is the wrong function, because you need to decode value as binary data. - The
Base64.atob()
function does exactly what you need, but you make a mistake when saving data, because, according to the officially documentation, by default thefs.writeFile()
function saves data as UTF-8, while you want to save binary data.
To properly decode Base64 value and store it as binary data, depending on your needs, you can choose one of the following methods:
require('js-base64').Base64.atob()
Decode the Base64 value using Base64.atob()
and specify binary encoding when saving the file. This is useful only if you need to handle binary data. Unlike other methods you must install and load the "js-base64" module.
var bin = Base64.atob(stringToDecode);
// Your code to handle binary data
fs.writeFile('result_binary.pdf', bin, 'binary', error => {
if (error) {
throw error;
} else {
console.log('binary saved!');
}
});
Buffer.from
Convert the Base64 value to buffer using Buffer.from()
and save it into file without specifying encoding. This is useful only if you need to handle buffer.
var buf = Buffer.from(stringToDecode, 'base64');
// Your code to handle buffer
fs.writeFile('result_buffer.pdf', buf, error => {
if (error) {
throw error;
} else {
console.log('buffer saved!');
}
});
The encoding option
If you do not need to read/modify the binary data or the buffer, just specify encoding option when saving file. This method is the simplest one and may be the fastest and most memory efficient.
fs.writeFile('result_base64.pdf', stringToDecode, 'base64', error => {
if (error) {
throw error;
} else {
console.log('base64 saved!');
}
});
Simple is the best! Just use fs
package to save the base64 string to a file, remember that you have to set base64
for encoding
option.
fs.writeFile('result_document.pdf', stringToDecode, 'base64', (error) => {
if (error) throw error;
console.log("Doc saved!");
});