在浏览器中解码Base64编码的PDF内容

在浏览器中解码Base64编码的PDF内容

问题描述:

我们使用 dompdf 在后端(PHP)中将HTML转换为PDF. dompdf生成的输出是通过Base64编码的,

We transform HTML to PDF in the backend (PHP) using dompdf. The generated output from dompdf is Base64 encoded with

$output = $dompdf->output();
base64_encode($output);

此Base64编码的内容另存为文件在服务器上.当我们像这样解码文件内容时:

This Base64 encoded content is saved as a file on the server. When we decode this file content like this:

cat /tmp/55acbaa9600f4 | base64 -D > test.pdf

我们获得了正确的PDF文件.

we get a proper PDF file.

但是当我们将Base64内容作为JSON对象内的字符串值传输到客户端时(服务器提供了RESTful API ...):

But when we transfer the Base64 content to the client as a string value inside a JSON object (the server provides a RESTful API...):

{
  "file_data": "...the base64 string..."
}

并使用atob()对其进行解码,然后创建一个Blob对象以稍后下载文件,PDF始终为空"/损坏.

And decode it with atob() and then create a Blob object to download the file later on, the PDF is always "empty"/broken.

$scope.downloadFileData = function(doc) {
  DocumentService.getFileData(doc).then(function(data) {
    var decodedFileData = atob(data.file_data);
    var file = new Blob([decodedFileData], { type: doc.file_type });
    saveAs(file, doc.title + '.' + doc.extension);
  });
};

当我们记录解码后的内容时,该内容似乎已损坏",因为与使用base64 -D在服务器上对内容进行解码时,几个符号并不相同.

When we log the decoded content, it seems that the content is "broken", because several symbols are not the same as when we decode the content on the server using base64 -D.

当我们对简单文本/普通文档的内容进行编码/解码时,它可以按预期工作.但是所有二进制(或非ASCII格式)都无法正常工作.

When we encode/decode the content of simple text/plain documents, it's working as expected. But all binary (or not ASCII formats) are not working.

我们已经在网络上搜索了许多小时,但没有找到适合我们的解决方案.有人有同样的问题,可以为我们提供有效的解决方案吗?预先感谢!

We have searched the web for many hours, but didn't find a solution for this that works for us. Does anyone have the same problem and can provide us with a working solution? Thanks in advance!

这是服务器上PDF文档的Base64编码内容的示例:

This is a example for a on the server Base64 encoded content of a PDF document:

JVBERi0xLjMKMSAwIG9iago8PCAvVHlwZSAvQ2F0YWxvZwovT3V0bGluZX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RjEKL0Jhc2VGb250IC9UaW1lcy1Cb2xkCi9FbmNvZGluZyAvV2luQW5zaUVuY29kaW5nCj4+CmVuZG9iagp4cmVmCjAgOQowMDAwMDAwMDAwIDY1NTM1IGYgCjAwMDAwMDAwMDggMDAwMDAgbiAKMDAwMDAwMDA3MyAwMDAwMCBuIAowMDAwMDAwMTE5IDAwMDAwIG4gCjAwMDAwMDAyNzMgMDAwMDAgbiAKMDAwMDAwMDMwMiAwMDAwMCBuIAowMDAwMDAwNDE2IDAwMDAwIG4gCjAwMDAwMDA0NzkgMDAwMDAgbiAKMDAwMDAwMDYxNiAwMDAwMCBuIAp0cmFpbGVyCjw8Ci9TaXplIDkKL1Jvb3QgMSAwIFIKL0luZm8gNSAwIFIKPj4Kc3RhcnR4cmVmCjcyNQolJUVPRgo=

如果使用atob(),则不会获得与使用base64 -D的控制台相同的结果.为什么?

If you atob() this, you don't get the same result as on the console with base64 -D. Why?

您的问题与我最近需要解决的问题相同.

Your issue looks identical to the one I needed to solve recently.

这对我有用:

const binaryImg = atob(base64String);
const length = binaryImg.length;
const arrayBuffer = new ArrayBuffer(length);
const uintArray = new Uint8Array(arrayBuffer);

for (let i = 0; i < length; i++) {
    uintArray[i] = binaryImg.charCodeAt(i);
}

const fileBlob = new Blob([uintArray], { type: 'application/pdf' });

saveAs(fileBlob, 'filename.pdf');

似乎仅执行base64解码是不够的...您需要将结果放入Uint8Array.否则,pdf页面将显示为空白.

It seems that only doing a base64 decode is not enough...you need to put the result into a Uint8Array. Otherwise, the pdf pages appear blank.

我在这里找到了这个解决方案: https://github.com/sayanee/angularjs-pdf/issues/110#issuecomment-579988190

I found this solution here: https://github.com/sayanee/angularjs-pdf/issues/110#issuecomment-579988190