how to print pdf inside embed/iframe using javascr

2019-09-05 10:22发布

问题:

Me again. Unluckily our company does not want to buy the flexpaper. Bad for me I have to find an alternative on how to print the pdf inside embed tag / iframe.

For my first question, i have the following code :

<script>
    function printTrigger(elementId) {
       var getMyFrame = document.getElementById(elementId);
       getMyFrame.focus();
       getMyFrame.contentWindow.print();
    }
</script>

with an iframe :

<iframe id="mypdf" src="data/sampledocument.pdf" width="60%" style="height:10em"align="right">

When I try to trigger the printTrigger function thru a button, printing of pdf works (either ie or firefox). My problem is when I change the src property of the pdf this code does not work. the source of the pdf comes from a different web directory see example below

<iframe id="mypdf" src="../../ViewPDF.aspx?filetype=r3fileserver&amp;nameoffile=Certificate/2011\2011010000035-preview.pdf" width="60%"
style="height:10em" align="right">

intead of viewing the pdf inside the iframe, the browser asks if I want to open or save it. Can some please tell me how to force the pdf to view inside the iframe? or this is impossible because there are from different web dir / pdf comes from an apsx?

For my second question can someone please help me the alternative code for printWithDialog that will work in firefox?

so much thanks!

hope to hear from you guys!

回答1:

I believe your problem is that the browser is not identifying the resource as a PDF so you should set the content type header to application/pdf.