Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
907 views
in Technique[技术] by (71.8m points)

internet explorer - Print a PDF via an iframe in Chrome/Firefox/IE

I want to trigger the print of a PDF file which I load in and iframe.

After looking around, I came up with the following bit

<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>

This works perfectly in Chrome.

In Firefox, I get the following error (I read somewhere it was a bug that was supposed to be fixed in version 21, but it wasn't)

Permission denied to access property 'print'

In Internet Explorer 10 and 9, I get the following error

Invalid calling object

which seems to point to the PDF generated by my MVC action.

I've seen numerous posts with problems similar to mine, yet haven't come across a working solution so far.

What I would really want to know is how RADPDF managed to get this working in every browser

Click the print button on this page

I know this can be done, I need help from you brains out there!

Cheers

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try the following, might work in all browsers. ( I have tested with IE8 and chrome only )

<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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...