简体   繁体   English

使用javascript从iframe打印pdf

[英]print pdf from iframe using javascript

While running time I'm adding pdf in iframe dynamically and printing that iframe. 在运行时,我会在iframe中动态添加pdf并打印该iframe。

But it's showing Permission denied error 但它显示“权限被拒绝”错误

This is my code 这是我的代码

    g_iFrame = document.createElement("iframe");
    g_iFrame.id = "testFrame";
    var style = g_iFrame.style;
    style.border = "1px solid black";
    style.width = "300px";
    style.height = "200px";
    g_iFrame.src = "D:\\Testing\\PDF\\"+mYFileList[i][0];

    document.body.appendChild(g_iFrame);
    var iframe = document.getElementById('testFrame');
    var frm = iframe.contentWindow;
    frm.focus();
    frm.print();

Note : mYFileList[i][0] is my pdf file from array 注意:mYFileList [i] [0]是我来自数组的pdf文件

In Firefox you face this ? 在Firefox中您会遇到这个问题吗? See this 看到这个

I have built a way around, not perfect yet. 我已经建立了一种解决方法,但还不完善。 Need to effectively use window.postMessage ( this can be helpful ) to confirm pdf loaded, before printing. 在打印之前,需要有效地使用window.postMessage( 这可能会有所帮助 )来确认已加载pdf。

$(document).ready(function(){
g_iframe = document.createElement("iframe");
g_iframe.id = "testFrame";
var style = g_iframe.style;
style.padding = "0px";
g_iframe.width = "960px";
style.height = "1100px";
g_iframe.src = "D:\\Testing\\PDF\\"+mYFileList[i][0];


g_iframe.addEventListener('load', function(){

    setTimeout(function(){
        $("iframe[id='testFrame']").html('<script type="text/javascript"> window.print(); </script>');
    }, 2000); 
}, true);

document.body.appendChild(g_iframe);

}); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM