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

查看:486
本文介绍了解码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, 阿托布

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天全站免登陆