通过Chrome/Firefox/IE中的iframe打印PDF

Fré*_*dry 5 pdf iframe firefox internet-explorer google-chrome

我想触发我加载的PDF文件和iframe的打印.

环顾四周后,我想出了以下几点

<iframe name="pdfname" id="pdfid"></iframe>

<button id="printbtn">Print</button>

<script language="javascript" type="text/javascript">
    $(document).ready(function () {

        $("#pdfid").load(function() {
            window.frames["pdfname"].focus();
            window.frames["pdfname"].print();
        });

        $("#printbtn").click(function () {
            $("#pdfid").attr("src", '@Url.Action("PdfTest", "Home")');
        });
    });
</script>
Run Code Online (Sandbox Code Playgroud)

这在Chrome中完美运行.

在Firefox中,我收到以下错误(我在某处读到这是一个应该在版本21中修复的错误,但事实并非如此)

访问属性'print'的权限被拒绝

在Internet Explorer 10和9中,我收到以下错误

无效的调用对象

这似乎指向我的MVC动作生成的PDF.

我看过很多帖子都有类似我的问题,但到目前为止还没有遇到过工作的解决方案.

我真正想知道的是RADPDF如何在每个浏览器中实现这一点

单击此页面上的打印按钮

我知道这可以做到,我需要你的帮助!

干杯

Dil*_*epa 5

尝试以下操作,可能适用于所有浏览器。(我只用 IE8 和 chrome 测试过)

<style type="text/css">
    @media print 
    {
        .dontprint{display:none} 
    }
</style>
<script type="text/javascript">
    function printIframePdf(){
        window.frames["printf"].focus();
        try {
            window.frames["printf"].print();
        }
        catch(e){
            window.print();
            console.log(e);
        }
    }
    function printObjectPdf() {
        try{            
            document.getElementById('idPdf').Print();
        }
        catch(e){
            printIframePdf();
            console.log(e);
        }
    }

    function idPdf_onreadystatechange() {
        if (idPdf.readyState === 4)
            setTimeout(printObjectPdf, 1000);
    }
</script>
<div class="dontprint" >
    <form><input type="button" onClick="printObjectPdf()" class="btn" value="Print"/></form>
</div>

<iframe id="printf" name="printf" src="http://pdfUrl.pdf" frameborder="0" width="440" height="580" style="width: 440px; height: 580px;display: none;"></iframe>
<object id="idPdf" onreadystatechange="idPdf_onreadystatechange()"
    width="440" height="580" style="width: 440px; height: 580px;" type="application/pdf"
    data="http://pdfUrl.pdf">
    <embed src="http://pdfUrl.pdf" width="440" height="580" style="width: 440px; height: 580px;" type="application/pdf">
    </embed>
    <span>PDF plugin is not available.</span>
</object>
Run Code Online (Sandbox Code Playgroud)