【Java】Web打印

      项目中总是会用到Web页面打印的功能,下面小编就简单介绍一下自己的经验。

【smsx控件】

下载地址:http://download.csdn.net/detail/nxgliming/6668999

jsp页面代码:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="../include.jsp"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
	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>回执打印</title>
<object id="factory" style="display: none"
	classid="clsid:1663ed61-23eb-11d2-b92f-008048fdd814"
	codebase="print/smsx.cab"> </object>
<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">
<script type="text/javascript"
	src="${pageContext.request.contextPath}/js/transport/printCarReceipt.js"></script>
<style type="text/css">
table.dataintable {
	margin-top: 10px;
	border-collapse: collapse;
	border: 1px solid #aaa;
	width: 100%;
}

table.dataintable td {
	vertical-align: baseline;
	padding: 5px 15px 5px 6px;
	border: 1px solid #aaa;
	text-align: left;
}
</style>
</head>
<body>
	<div id="NoPrint" class="noPrint"
		style="text-align: center; margin-top: 30px">
		<div>
			<a id="print" οnclick="print()" class="easyui-linkbutton c1"
				style="width: 120px">直接打印</a> <a id="printSet" οnclick="printSet()"
				class="easyui-linkbutton c1" style="width: 120px">打印设置</a> <a
				id="printView" οnclick="printPreivew()" class="easyui-linkbutton c1"
				style="width: 120px">打印预览</a> <a id="moveBackClick"
				οnclick="moveBackClick()" class="easyui-linkbutton c1"
				style="width: 120px">取消返回</a>
		</div>
	</div>
	<!-- 打印表格 -->
	<div id="report1page1" region="center"
		style="text-align: center; margin-top: 30px">
		<table border="1px" align="center" style="font: 18px 宋体;">
			<tr>
				<td colspan="4" style="text-align: center;height:35px;:valign:middle"><span style="font-size:20px;font-weight:bold;">到车回执</span></td>
			</tr>
			<tr>
				<td>承运单号</td>
				<td id="S_ID">${S_ID }</td>
				<td>运输商名称</td>
				<td id="S_TCompany">${S_TCompany }</td>
			</tr>
			<tr>
				<td>售达方名称</td>
				<td id="S_CCompany">${S_CCompany }</td>
				<td>送达方名称</td>
				<td id="S_RCompany">${S_RCompany }</td>
			</tr>
			<tr>
				<td>发运总量</td>
				<td id="sendCount">${receiveCount }</td>
				<td>车辆标识</td>
				<td id="uniqueNo">${uniqueNo }</td>
			</tr>
			<tr>
				<td>签收数量</td>
				<td id="receiveCount">${receiveCount }</td>
				<td>签收备注</td>
				<td id="S_TReturnRemark">无</td>
			</tr>
			<tr>
				<td>发车确认时间</td>
				<td id="S_CTime">${S_CTime }</td>
				<td>到车确认时间</td>
				<td id="S_TReturnTime">${S_TReturnTime }</td>
			</tr>
			<tr>
				<td>核定在途时间</td>
				<td id="S_InTrasitTime">${S_InTrasitTime }</td>
				<td>到车延误时间</td>
				<td id="receivedDelayTime">无延误</td>
			</tr>
			<tr>
				<td>接车人姓名</td>
				<td id="S_RCPerson">${S_RCPerson }</td>
				<td>接车人电话</td>
				<td id="S_RTel">${S_RTel }</td>
			</tr>
			<tr>
				<td colspan="4" style="text-align: center;height:35px;:valign:middle">受达方签章:</td>
			</tr>
		</table>
	</div>
</body>
</html>
js代码:

// 打印设置按钮
function printSet() {
	if (!finishText()) {
		return false;
	}
	factory.printing.PageSetup();
}

// 默认的打印设置
function SetPrintSettings() {

	factory.printing.header = "";
	factory.printing.footer = "";
	factory.printing.portrait = true;// 方向,true
	factory.printing.leftMargin = 5.0;// mm
	factory.printing.topMargin = 5.0;
	factory.printing.rightMargin = 5.0;
	factory.printing.bottomMargin = 3.0;

}

// 打印预览按钮
function printPreivew() {
	if (!finishText()) {
		return false;
	}
	document.getElementById("NoPrint").style.display = "none";
	factory.printing.Preview();
	document.getElementById("NoPrint").style.display = "block";

}

// 直接打印按钮
function print() {
	if (!finishText()) {
		return false;
	}
	document.getElementById("NoPrint").style.display = "none";
	factory.printing.Print(false); // 设置为false,直接打印
	document.getElementById("NoPrint").style.display = "block";
}

// 返回取消
function moveBackClick() {
	window.parent.$('#tabs').tabs('close', '回执打印');
}
实现效果:

【jatoolsPrinter插件】

        这个提供一个网址给大家,详尽,可以看看:http://print.jatools.com/cases.htm

【Webbrowser插件】

       跟第一种方式类似,也需要在jsp页面中通过<object>标签引入控件,在js中直接调用方法即可,下面就简写代码了

<body>    
<object id="WebBrowser" classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height="0" width="0"> </object>
</body>  
<script>    
WebBrowser.ExecWB(1,1) //打开     
WebBrowser.ExecWB(2,1) //关闭现在所有的IE窗口,并打开一个新窗口     
WebBrowser.ExecWB(4,1) //保存网页        
WebBrowser.ExecWB(6,1) //打印     
WebBrowser.ExecWB(7,1) //打印预览     
WebBrowser.ExecWB(8,1) //打印页面设置       
WebBrowser.ExecWB(10,1) //查看页面属性     
WebBrowser.ExecWB(15,1) //撤销     
WebBrowser.ExecWB(17,1) //全选     
WebBrowser.ExecWB(22,1) //刷新     
WebBrowser.ExecWB(45,1) //关闭窗体无提示  
</script> 
【小结】

        以上就是小编实践过的方式了,还有其他的类似JQuery方式等,可以研究一下。未完待续。。。


评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值