我看了很多关于人们如何将其基本的64字节数组转换为PDF的示例,但不幸的是似乎没有一个对我有用。看着 : Javascript: Open PDF in new tab from byte array和how to convert byte array to pdf and download
我的代码与我所讲的并没有什么不同。 I have a JS-Fiddle here带有测试字节数组数据,我可以通过不同的base64编码器/解码器确认它是有效的。 我希望有人能看到我在做什么错。我正在使用atob()包装器对base64字符串进行解码,它的确似乎在正确地执行此操作。感谢任何有建议的人。
对于那些只想在这里查看的人:
function go() {
var data = byteData ;
// console.log(data);
var pdfData = atob(data);
console.log(pdfData)
var file = new Blob([pdfData], {type:'application/pdf'});
var fileUrl = URL.createObjectURL(file);
//open it via a link
var fileName = "test.pdf";
var a = document.createElement("a");
document.body.appendChild(a);
a.href = fileUrl;
a.download = fileName;
a.click();
//open it directly
window.open(fileUrl);
}
const byteData = "JVBERi0xLjUNJeLjz9MNCjEwIDAgb2JqDTw8L0xpbmVhcml6ZWQgMS9MIDcwNTMvTyAxMi9FIDI3NjgvTiAxL1QgNjc1MS9IIFsgNDQ4IDE0NF0+Pg1lbmRvYmoNICAgICAgICAgICAgICAgICAgICAgDQoxNSAwIG9iag08PC9EZWNvZGVQYXJtczw8L0NvbHVtbnMgNC9QcmVkaWN0b3IgMTI+Pi9GaWx0ZXIvRmxhdGVEZWNvZGUvSURbPDJEMjJDQUFDOTI0MThBN0I4N0ZDMTg1Nzc1RjcwMDM0PjxGRDQ5MTMxQTQwNDIzODRCOTYzMUY0QUY3RDQ0MUI2MD5dL0luZGV4WzEwIDExXS9JbmZvIDkgMCBSL0xlbmd0aCA0OC9QcmV2IDY3NTIvUm9vdCAxMSAwIFIvU2l6ZSAyMS9UeXBlL1hSZWYvV1sxIDIgMV0+PnN0cmVhbQ0KaN5iYmQQYGBiYHIAEgx+QIKxEURcABJ/WhmYGBlmgiQYGJGI/4yb/wIEGACJcQbZDQplbmRzdHJlYW0NZW5kb2JqDXN0YXJ0eHJlZg0KMA0KJSVFT0YNCiAgICAgICANCjIwIDAgb2JqDTw8L0Zpb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cj5BY3JvYmF0IERpc3RpbGxlciAxNS4wIChXaW5kb3dzKTwvcGRmOlByb2R1Y2VyPgogICAgICAgICA8ZGM6Zm9ybWF0PmFwcGxpY2F0aW9uL3BkZjwvZGM6Zm9ybWF0PgogICAgICAgICA8ZGM6dGl0bGU+CiAgICAgICAgICAgIDxyZGY6QWx0PgogICAgICAgICAgICAgICA8cmRmOmxpIHhtbDpsYW5nPSJ4LWRlZmF1bHQiPm5ldyAxPC9yZGY6bGk+CiAgICAgICAgICAgIDwvcmRmOkFsdD4KICAgICAgICAgPC9kYzp0aXRsZT4KICAgICAgICAgPGRjOmNyZWF0b3I+CiAgICAgICAgICAgIDxyZGY6U2VxPgogICAgICAgICAgICAgICA8cmRmOmxpPk1NY0RlbGw8L3JkZjpsaT4KICAgICAgICAgICAgPC9yZGY6U2VxPgogICAgICAgICA8L2RjOmNyZWF0b3I+CiAgICAgICAgIDx4bXBNTTpEb2N1bWVudElEPnV1aWQ6OTg1OWJhYTMtNTI1ZS00MjA3LTljNWUtN2IxMGVmYmM2NzRmPC94bXBNTTpEb2N1bWVudElEPgogICAgICAgICA8eG1wTU06SW5zdGFuY2VJRD51dWlkOjIwNzU1NmJjLTBmMWUtNGNhYS1iNWJhLWUwNzAwZTY5Yzc2MjwveG1wTU06SW5zdGFuY2VJRD4KICAgICAgPC9yZGY6RGVzY3JpcHRpb24+CiAgIDwvcmRmOlJERj4KPC94OnhtcG1ldGE+CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgICAgICAgICAgICAKPD94cGFja2V0IGVuZD0idyI/Pg0KZW5kc3RyZWFtDWVuZG9iag0zIDAgb2JqDTw8L0ZpbHRlci9GbGF0ZURlY29kZS9GaXJzdCA0L0xlbmd0aCA0OS9OIDEvVHlwZS9PYmpTdG0+PnN0cmVhbQ0KaN6yUDBQsLHRd84vzStRMNT3zkwpjjY0AgoGxeqHVBak6gckpqcW29kBBBgA3/oLrQ0KZW5kc3RyZWFtDWVuZG9iag00IDAgb2JqDTw8L0ZpbHRlci9GbGF0ZURlY29kZS9GaXJzdCA0L0xlbmd0aCAxNTMvTiAxL1R5cGUvT2JqU3RtPj5zdHJlYW0NCmjefMy9CoMwFEDhV7mbydD8WKVaRJBmFYSWdnGxyQUDwZRrxNevQ+fu5zs1KGga2W1pjsT63hoMgcsb4ZR8XMyUkJlrrnSlKn3Wha6L4qQumVLZrzrYcLfkP6kULgR4Iq2HhFLkIueyj+7/ZKDoNovEOkvxPSUwfk0+BCTQpVAwspdfXNzXkXP58CkgW3AHzdv2K8AAYHM3rA0KZW5kc3RyZWFtDWVuZG9iag01IDAgb2JqDTw8L0RlY29kZVBhcm1zPDwvQ29sdW1ucyA0L1ByZWRpY3RvciAxMj4+L0ZpbHRlci9GbGF0ZURlY29kZS9JRFs8MkQyMkNBQUM5MjQxOEE3Qjg3RkMxODU3NzVGNzAwMzQ+PEZENDkxMzFBNDA0MjM4NEI5NjMxRjRBRjdENDQxQjYwPl0vSW5mbyA5IDAgUi9MZW5ndGggNDgvUm9vdCAxMSAwIFIvU2l6ZSAxMC9UeXBlL1hSZWYvV1sxIDIgMV0+PnN0cmVhbQ0KaN5iYgACJkauCwxMDIxtQIK3HcTqBRHfgBLPFgJZDAyMQILpP5BgZAAIMACRngYbDQplbmRzdHJlYW0NZW5kb2JqDXN0YXJ0eHJlZg0KMTE2DQolJUVPRg0K";
答案 0 :(得分:0)
您还需要将base64字符串转换为arraybuffer。
function _base64ToArrayBuffer(base64) {
var binary_string = window.atob(base64);
var len = binary_string.length;
var bytes = new Uint8Array(len);
for (var i = 0; i < len; i++) {
bytes[i] = binary_string.charCodeAt(i);
}
return bytes.buffer;
}
来自此convert base64 string to arraybuffer
并按如下所示在您的代码示例中调用它:-
function go() {
var data = byteData ;
// console.log(data);
// HERE THIS CHANGE IN YOUR CODE
var pdfData = _base64ToArrayBuffer(data);
var file = new Blob([pdfData], {type:'application/pdf'});
var fileUrl = URL.createObjectURL(file);
//open it via a link
var fileName = "test.pdf";
var a = document.createElement("a");
document.body.appendChild(a);
a.href = fileUrl;
a.download = fileName;
a.click();
//open it directly
window.open(fileUrl);
}
此jsfiddle:-fiddle