Decoding Base64 pdf giving broken file

Anton picture Anton · Jun 6, 2019 · Viewed 9.6k times · Source

Can someone please explain why decoding Base64 giving a broken pdf? I need to find the way how to decode Base64 and get pdf out. When i use this service

https://emn178.github.io/online-tools/base64_decode_file.html

I am able to pass Base64 and get file out without problem.

But when i do same in node.js I am getting empty (broken) file consistently. I tried different packages like: js-base64, atob

and none of them worked, getting same empty file as the result.

Link to my code: https://repl.it/@afiliptsov/FaroffGloriousFormula

Answer

Victor picture Victor · Jun 9, 2019

You get a corrupted PDF, because:

  1. 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.
  2. 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 the fs.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!');
    }
});