介绍
js实现html打印,并且设置页眉页脚
参考
• 1、首先下载jquery.jqprint-0.3.js
• 2、 页面引入jquery.jqprint-0.3.js
• 3、js代码
[codesyntax lang="java"]
<script type="text/javascript">
$(document).ready(function() {
$("#btnPrint").click(function(){
jqprintDiv();
});
});
function jqprintDiv() {
//打印初始化
if ($.browser.msie) {
//IE浏览器执行
printitIE('content');
} else {
//其他浏览器执行通用打印
$("#content").jqprint();
}
}
function printitIE(MyDiv) {
setCload();
//提示窗口
if (confirm('确定打印吗?')) {
var winname = window.open('', "_blank",'');
var newstr = document.getElementById(MyDiv).innerHTML;
var str = "<div style='position:absolute;padding-left: 50px;";
str+="padding-right: 50px;padding-top:50px;padding-bottom:50px;'>";
str+= newstr + "</div>";
winname.document.body.innerHTML= str;
winname.print();
return false;
}
}
//清空页眉页脚
function setCload(){
HKEY_Root="HKEY_CURRENT_USER";
HKEY_Path="\\Software\\Microsoft\\Internet Explorer\\PageSetup\\";
var head,foot,top,bottom,left,right;
try{
var Wsh=new ActiveXObject("WScript.Shell");
HKEY_Key="header";
//设置页眉(为空) 根据你自己要设置的填入
Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"");
HKEY_Key="footer";
//设置页脚(为空) 根据你自己要设置的填入
Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"");
HKEY_Key="margin_bottom";
//设置下页边距(0) 根据你自己要设置的填入
Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"200");
HKEY_Key="margin_left";
//设置左页边距(0) 根据你自己要设置的填入
Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"50");
HKEY_Key="margin_right";
//设置右页边距(0)
Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"50");
HKEY_Key="margin_top";
//设置上页边距(8)
Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"200");
}catch(e){
}
}
</script>
<div class="float_cls" style="text-align:right;padding-top: 0px;" >
<div style="text-align: right;margin-bottom: 0px;padding-right: 10px;">
<input type="hidden" id="leaderId" value="${id }"/>
<input id="btnUpdate" class="btn btn-primary" type="button" value="编辑"/>
<input id="btnPrint" class="btn btn-primary" type="button" value="打印"/>
<input id="btnClose" class="btn btn-primary" type="button" value="关闭"/>
</div>
</div>
<div id="content" style="padding-left:50px;padding-right:50px;padding-top: 50px;
padding-bottom: 50px;">
${ content }
</div>
[/codesyntax]