我試圖從網頁上打印一個動態生成的PDF。如何打印動態生成的pdf dataUrl?
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 ATTR給出了警告:
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);
});
[跨網域iframe獲取DOM含量](可能重複http://stackoverflow.com/questions/6170925/get-dom- content-of-cross-domain-iframe) –
我不需要獲取內容,已經有內容!我只需要打印。 – Garfield
如果顯示而不是打印PDF,用戶是否更舒服?也許他們寧願要保存PDF或甚至爲某種原因製作截圖。我不認爲打印PDF按鈕會很方便。 – kay