how to print pdf inside embed/iframe using javascript

爱⌒轻易说出口 提交于 2019-12-12 02:17:49

问题


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.



来源:https://stackoverflow.com/questions/7494243/how-to-print-pdf-inside-embed-iframe-using-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!