在Chrome / Firefox / IE中通过iframe打印PDF [英] Print a PDF via an iframe in Chrome/Firefox/IE
问题描述
我想触发我加载的PDF文件和iframe的打印。
看了一下后,我想出了下面的内容
< iframe name =pdfnameid =pdfid>< / iframe>
< button id =printbtn>列印< /按钮>
< script language =javascripttype =text / javascript>
$(document).ready(function(){
$(#pdfid)load(function(){
window.frames [pdfname]。 focus();
window.frames [pdfname]。print();
});
$ b $(#printbtn)。
$(#pdfid)。attr(src,'@ Url.Action(PdfTest,Home)');
});
});
< / script>
在Firefox中,我得到了下面的错误(我读了一个错误,应该是在21版本中修复的,但它不是)
在Internet Explorer 10和9中,出现以下错误: 无效的调用对象 这似乎指向由我的MVC操作生成的PDF 我看过很多类似于我的问题的帖子,但到目前为止还没有遇到可行的解决方案。 我真的想知道RADPDF是如何设法在每个浏览器中都能正常工作的。 我知道这可以做,我需要你的帮助! 干杯 请尝试以下操作,可能适用于所有浏览器。 (我只用IE8和Chrome进行测试) 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 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 Try the following, might work in all browsers. ( I have tested with IE8 and chrome only )
这篇关于在Chrome / Firefox / IE中通过iframe打印PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
$ b
< 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);
$ b function printObjectPdf(){
try {
document.getElementById('idPdf')。Print();
}
catch(e){
printIframePdf();
console.log(e);
函数idPdf_onreadystatechange(){
if(idPdf.readyState === 4)
setTimeout(printObjectPdf,1000);
}
< / script>
< div class =dontprint>
< form>< input type =buttononClick =printObjectPdf()class =btnvalue =Print/>< / form>
< / div>
width =440height =580style =width:440px; height:580px; type =application / pdf
data =http://pdfUrl.pdf>
< embed src =http://pdfUrl.pdfwidth =440height =580style =width:440px; height:580px;类型= 应用/ PDF >
< / embed>
< span> PDF插件不可用。< / span>
< / object>
<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>
<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>