所以我有这个脚本来将b64字符串解码为pdf
// JavaScript Document
function decode() {
// The Base64 string of a simple PDF file
var b64decode = BASE64 STRING
// Decode Base64 to binary and show some information about the PDF file (note that I skipped all checks)
var bin = atob(b64decode);
// Embed the PDF into the HTML page and show it to the user
var obj = document.createElement('object');
obj.style.width = '100%';
obj.style.height = '842pt';
obj.type = 'application/pdf';
obj.data = 'data:application/pdf;base64,' + b64decode;
document.body.appendChild(obj);
// Insert a link that allows the user to download the PDF file
var link = document.createElement('a');
link.innerHTML = 'Download PDF file';
link.download = 'file.pdf';
link.href = 'data:application/octet-stream;base64,' + b64decode;
document.body.appendChild(link);
}脚本在页面底部创建一个对象。相反,我需要脚本在一个空的div容器中创建一个对象。
正如您可能猜到的,我不是JS专家(也不是业余爱好者)。
谢谢!
发布于 2020-06-01 05:30:45
下面这行代码执行附加操作:
document.body.appendChild(link);您必须将其更改为您的div。为此,你需要给你的div一个id:
<div id="pdf-div"></div>现在,您可以将appendChild指令替换为:
document.getElementById('pdf-div').innerHTML = link;https://stackoverflow.com/questions/62122656
复制相似问题