使用javascript在单击按钮时加载和打印PDF的可能方法
您能告诉我加载和打印pdf文件的可能方法吗?该方法适用于所有浏览器。目前,我正在使用iframe显示文件,然后触发打印方法,但它在IE中不起作用。我搜索了很多次,但没有一个解决方案有效。请让我知道任何其他方法来实现这一点。下面是我的示例代码:-使用javascript在单击按钮时加载和打印PDF的可能方法,javascript,jquery,iframe,Javascript,Jquery,Iframe,您能告诉我加载和打印pdf文件的可能方法吗?该方法适用于所有浏览器。目前,我正在使用iframe显示文件,然后触发打印方法,但它在IE中不起作用。我搜索了很多次,但没有一个解决方案有效。请让我知道任何其他方法来实现这一点。下面是我的示例代码:- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="pdfpoc.aspx.cs" Inherits="Game.pdfpoc" %> <!DOCTYPE html PU
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="pdfpoc.aspx.cs" Inherits="Game.pdfpoc" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="Scripts/jquery-1.10.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
function printpdf()
{
alert("pdfprint");
var iframe = document.frames ? window.frames.frames["frPDF"] : document.getElementById("frPDF");
var ifWin = iframe.contentWindow || iframe;
try {
ifWin.focus();
ifWin.print();
}
catch (e) {
window.print(false);
//or when you get Invalid calling object error for IE9 and above
//set the browser into IE8 compatibility mode will work
}
return false;
}
function changeSource() {
console.log("change");
var src = $("#frPDF").attr('src');
console.log("src", src);
if (src == "NCTP.pdf") {
// $("#frPDF").attr('src', "NCTP1.pdf");
url = "NCTP1.pdf";
}
else {
// $("#frPDF").attr('src', "NCTP.pdf");
url = "NCTP.pdf";
}
console.log("Url" + url);
var iframe = $('#frPDF')[0]; // reference to IFRAME element
$.get(url, function () {
iframe.src = url;
$("#frPDF").load(function () {
printpdf();
});
// $("#frPDF").trigger('onload');
}).error(function () { alert('PDF not found'); });
return false;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<input type="button" value="Change Source" onclick="javascript:changeSource()" />
<div> Its not the part of Iframè <br />
IFRAME PDF: <br />
<iframe id="frPDF" height="800" width="800" src="NB.pdf?a=1"></iframe>
</div>
</form>
</body>
</html>
函数printpdf()
{
警报(“pdfprint”);
var iframe=document.frames?window.frames.frames[“frPDF”]:document.getElementById(“frPDF”);
var ifWin=iframe.contentWindow | | iframe;
试一试{
ifWin.focus();
ifWin.print();
}
捕获(e){
窗口。打印(假);
//或者当IE9及以上版本出现无效调用对象错误时
//将浏览器设置为IE8兼容模式将起作用
}
返回false;
}
函数changeSource(){
控制台日志(“更改”);
var src=$(“#frpd”).attr('src');
console.log(“src”,src);
如果(src==“NCTP.pdf”){
//$(“#frpd”).attr('src',“NCTP1.pdf”);
url=“NCTP1.pdf”;
}
否则{
//$(“#frpd”).attr('src',“NCTP.pdf”);
url=“NCTP.pdf”;
}
console.log(“Url”+Url);
var iframe=$('#frpd')[0];//对iframe元素的引用
$.get(url,函数(){
iframe.src=url;
$(“#frpd”).load(函数(){
printpdf();
});
//$(“#frpd”).trigger('onload');
}).error(函数(){alert('PDF not found');});
返回false;
}
它不是Iframè的一部分
IFRAME PDF:
为了让IE浏览器更好地使用pdf的嵌入标记,请看这个(我将替换您的pdf源文件)。这是我在IE 8+和Chrome(以及Opera ofc)中的工作
它不是Iframè的一部分
IFRAME PDF:
不幸的是,对于每个浏览器,打印方式都不同。为了更好地使用ie,它没有在ie中加载文档,也没有在chrome中加载。我使用的是IE11。
<input type="button" value="Change Source" name="btnChangeSource" />
<div> Its not the part of Iframè <br />
IFRAME PDF: <br />
<embed id="frPDF" type="application/pdf" height="800" width="800" src="http://eurecaproject.eu/files/5013/9885/7113/example4.pdf"></embed>
</div>
jQuery(document).ready(function($) {
function print(url)
{
var _this = this,
iframeId = 'iframeprint',
$iframe = $('iframe#iframeprint');
$iframe.attr('src', url);
$iframe.load(function() {
_this.callPrint(iframeId);
});
}
//initiates print once content has been loaded into iframe
function callPrint(iframeId) {
var PDF = document.getElementById(iframeId);
PDF.focus();
PDF.contentWindow.print();
}
});