html或jsp实现打印三种方法

时间:2022-02-11 03:09:15

1.使用window.print()方法

优点:支持多浏览器
缺点:取消打印,隐藏打印不必要的信息后再显示比较麻烦
如下实现,可以打印当前页面
<input name ="Button"  value="火狐谷歌打印" type="button" onclick="window.print()" />
2.使用第三方插件的功能进行打印,eg:ireport报表插件
优点:支持多浏览器
缺点:实现起来比较麻烦

3.调用ie内置打印控件IEWebBrowser实现打印功能
优点:针对ie功能强大
缺点:只支持ie

<%@ page language ="java" import="java.util.*" pageEncoding="utf-8" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://" +request.getServerName()+":"+request.getServerPort()+path+ "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href =" <%=basePath%> ">
<title> My JSP 'print.jsp' starting page</title >
<meta http-equiv ="pragma" content="no-cache" >
<meta http-equiv ="cache-control" content="no-cache" >
<meta http-equiv ="expires" content="0" >
<meta http-equiv ="keywords" content="keyword1,keyword2,keyword3" >
<meta http-equiv ="description" content="This is my page" >
<!-- 打印时下列方法生效 -->
<style media ="print" >
<!--
. Noprint{display: none;}
.PageN ext{pag e-break-after:always;}
-->
</style>
<style type ="text/css" >
<!--
.STYLE1 {font-size : 12px }
-->
</style>
</head> <body>
<br>
<table border ='8' width="500" height="500" >
<tr> <td>haha</td><td>haha</td><td>haha</td><td>haha</td></tr>
<tr><td></ td><td></ td><td></ td><td></ td></tr>
<tr><td></ td><td></ td><td></ td><td></ td></tr>
</table>
<p class ="dis" align="center" >
<OBJECT classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height=0
id= "WebBrowser" width=0>
</OBJECT>
<input name =Button onClick=document.all.WebBrowser.ExecWB(1,1) class="Noprint" type=button value=打开 />
<input name =Button onClick=document.all.WebBrowser.ExecWB(2,1) class="Noprint" type=button value=关闭所有 >
<input name =Button onClick=document.all.WebBrowser.ExecWB(4,1) class="Noprint" type=button value=另存为 >
<input name =Button onClick=document.all.WebBrowser.ExecWB(6,1) class="Noprint" type=button value=打印 >
<input name =Button onClick=document.all.WebBrowser.ExecWB(6,6) class="Noprint" type=button value=直接打印 >
<input name =Button onClick=document.all.WebBrowser.ExecWB(7,1) class="Noprint" type=button value=打印预览 >
<input name =Button onClick=document.all.WebBrowser.ExecWB(8,1) class="Noprint" type=button value=页面设置 >
<input name =Button onClick=document.all.WebBrowser.ExecWB(10,1) class="Noprint" type=button value=属性 >
<input name =Button onClick=document.all.WebBrowser.ExecWB(17,1) class="Noprint" type=button value=全选 >
<input name =Button onClick=document.all.WebBrowser.ExecWB(22,1) class="Noprint" type=button value=刷新 >
<input name =Button onClick=document.all.WebBrowser.ExecWB(45,1) class="Noprint" type=button value=关闭 >
</p>
</body>
</html>