解码 Base64 pdf 给出损坏的文件 [英] Decoding Base64 pdf giving broken file

查看:89
本文介绍了解码 Base64 pdf 给出损坏的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能解释一下为什么解码 Base64 会给出损坏的 pdf 吗?我需要找到如何解码 Base64 并获取 pdf 的方法.当我使用这项服务时

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

我能够通过 Base64 并毫无问题地获取文件.

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

但是当我在 node.js 中做同样的事情时,我总是得到空(损坏的)文件.我尝试了不同的软件包,例如:js-base64,atob

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.

链接到我的代码:https://repl.it/@afiliptsov/FaroffGloriousFormula

推荐答案

你得到一个损坏的 PDF,因为:

You get a corrupted PDF, because:

  1. 根据官方文档Base64.decode() 函数将 Base64 值解码为 UTF-8 字符串.作为你可以看到,这是错误的功能,因为你需要解码值作为二进制数据.
  2. Base64.atob() 函数完全符合您的需要,但您保存数据时出错,因为,根据官方文档,默认为fs.writeFile()函数将数据保存为 UTF-8,而您要保存二进制数据.
  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.

要正确解码 Base64 值并将其存储为二进制数据,您可以根据需要选择以下方法之一:

To properly decode Base64 value and store it as binary data, depending on your needs, you can choose one of the following methods:

使用 Base64.atob() 解码 Base64 值,并在保存文件时指定二进制编码.这仅在您需要处理二进制数据时才有用.与其他方法不同,您必须安装和加载js-base64"模块.

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

使用 Buffer.from() 将 Base64 值转换为缓冲区,并将其保存到文件中,无需指定编码.这仅在您需要处理缓冲区时才有用.

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

这篇关于解码 Base64 pdf 给出损坏的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆