Decoding Base64 pdf giving broken file

有些话、适合烂在心里 提交于 2020-02-02 11:15:13

问题


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


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/56483097/decoding-base64-pdf-giving-broken-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!