gpt4 book ai didi

javascript - 如何打印动态生成的pdf dataUrl?

转载 作者:行者123 更新时间:2023-11-29 15:33:20 25 4
gpt4 key购买 nike

我正在尝试从网页打印动态生成的 PDF。

var $iframe = $('<iframe>');
$iframe.appendTo('body');
$iframe.load(function() {
var iframe = $iframe[0];
var result = iframe.contentWindow.document.execCommand("print", false, null);
if (!result) iframe.contentWindow.print();
$.remove($iframe);
});
$iframe.attr('src', dataUrl);

execCommand() 给出错误信息:

Uncaught SecurityError: Blocked a frame with origin "http://localhost:2520" from accessing a frame with origin "null". The frame requesting access has a protocol of "http", the frame being accessed has a protocol of "data". Protocols must match.

此外,设置 src 属性会给出警告:

Resource interpreted as Document but transferred with MIME type application/pdf:

dataUrl 如下所示:

data:application/pdf;base64,JVBERi0xLjQKJdP...

编辑:@Mike C

我可以创建 iframe 并显示 pdf,但是当我打印时,它是空白的。

<style type="text/css" media="print">
body * { display:none }
iframe#theframe { display:block }
</style>

var $iframe = $('<iframe id="theframe" src="'+dataUrl+'"></iframe>');
$iframe.appendTo('body');
$iframe.load(function() {
setTimeout(function() {
window.print();
}, 1000);
});

最佳答案

尝试使用 window.open() , document.write() , setTimeout()

var popup = window.open("", "w");

var html = '<!doctype html><html><head></head>'
+ '<body marginwidth="0" marginheight="0" style="background-color: rgb(38,38,38)">'
+ '<embed width="100%" height="100%" name="plugin" src="data:application/pdf;base64,JVBERi0xLjQKJdP..." type="application/pdf">'
+ '<script>setTimeout("print()", 1000)</script></body></html>';

popup.document.write(html);

关于javascript - 如何打印动态生成的pdf dataUrl?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32280613/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com