项目需要在WEB项目中利用JS打印项目的考试标签基本信息。之前用了很多种方式。使用了LodopFuncs。不得不说LodopFuncs功能是比较的强大。但是需要收费,否则打印出来会出现“XX测试提供的水印”,不满足要求。
后来在网上试用了很多的方式。个人觉得杰表云打印挺适合的,代码简单。
杰表打印控件的英文名称为jatoolsPrinter,这是专为开发者打造的一款免费打印控件,安装后可以加载到网页中使用,软件支持主流的网页浏览器,比如 firefox、chrome、safari等,拥有批量打印、自动打印、表格自动分页以及支持codabar、code39、code128等主流的二维码打印操作,具有配置简单,打印质量清晰以及代码简洁的特点。
简单例子:
第一步:网上下载杰表云打印的JS。
jcpfree.js (下载地址:http://www.121down.com/soft/softview-93597.html)
第二步:撸代码
因我我需要,需要打印至少60个以上的标签。我用ForEach循环生成。
注意:控件按照你id去选择打印。默认是page,后面加上需要打印的后缀。此时打印搞定。
<%@ page contentType="text/html; charset=UTF-8" language="java"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>打印</title>
<%
String baseURL = request.getContextPath();
%>
<script type="text/javascript" src="<%=baseURL %>/liwei/examCard/jcpfree.js"></script>
<script>
function doPrint(how) {
var myDoc = {
documents : document, // 打印页面(div)们在本文档中
copyrights : '杰创软件拥有版权 www.jatools.com' // 版权声明必须
};
var jcp = getJCP();
// 调用打印方法
if (how == '打印预览')
jcp.printPreview(myDoc, false);
else if (how == '弹出打印机选择对话框打印') {
jcp.print(myDoc, true);
} else
jcp.print(myDoc, false); // 不弹出对话框打印
}
</script>
</head>
<body>
<input type="button" value="打印" onClick="doPrint('打印预览')">
<br />
<c:forEach items="${examCardList }" var="examCard" varStatus="status">
<div id="page${status.index +1 }">
<div style="width: 215px;height: 120px; margin-top:15px; margin-left:5px; border:1px solid green;font-size: 11px;">
<div style="float: left;width: 110px">
<div style="margin-top: 10px; margin-left: 2px;">准考证:${examCard.examCardNo }</div>
<div style="margin-top: 10px; margin-left: 2px;">姓名:${examCard.studentName }</div>
<div style="margin-top: 10px; margin-left: 2px;">科目:${projectValue }</div>
<div style="margin-top: 10px; margin-left: 2px;">考场:${examCard.roomNo }</div>
<div style="margin-top: 10px; margin-left: 2px;">画室:${examCard.studioName }</div>
</div>
<div style="float: right;margin-right: 2px;" >
<img style="margin-top: 15px;" width="91px" height="91px" src="<%=LwCommon.QRCODE_HTTP %>/${examCard.batchId }/${project }/${examCard.resourceId }.png">
</div>
</div>
</div>
</c:forEach>
</body>
</html>
页面选择打印标签:
个人没去研究其他的什么办法.只是利用了CSS去控制DIV的id。
<%@ page contentType="text/html; charset=UTF-8" language="java"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>打印</title>
<%
String baseURL = request.getContextPath();
%>
<style type="text/css">
@media print {
.noprint{display:none;}
}
</style>
<script type="text/javascript" src="<%=baseURL %>/liwei/examCard/jcpfree.js"></script>
<!-- jquery1.6.2 -->
<script type="text/javascript" src="<%=baseURL%>/js/jquery/jquery-ui-1.8.15.custom/js/jquery-1.6.2.min.js"></script>
<script>
function doPrint(how) {
var myDoc = {
documents : document, // 打印页面(div)们在本文档中
copyrights : '杰创软件拥有版权 www.jatools.com' // 版权声明必须
};
var jcp = getJCP();
// 调用打印方法
if (how == 'cardNo') {
$(".cardNo").attr("id","page1");
$(".sc").removeAttr("id");
$(".sm").removeAttr("id");
$(".sx").removeAttr("id");
jcp.printPreview(myDoc, false);
} else if (how == 's') {
$(".sc").attr("id","page1");
$(".cardNo").removeAttr("id");
$(".sm").removeAttr("id");
$(".sx").removeAttr("id");
jcp.printPreview(myDoc, false);
} else if (how == 'm') {
$(".sm").attr("id","page1");
$(".sc").removeAttr("id");
$(".cardNo").removeAttr("id");
$(".sx").removeAttr("id");
jcp.printPreview(myDoc, false);
} else if (how == 'x') {
$(".sx").attr("id","page1");
$(".sc").removeAttr("id");
$(".sm").removeAttr("id");
$(".cardNo").removeAttr("id");
jcp.printPreview(myDoc, false);
}
}
</script>
</head>
<body>
<input type="button" value="打印准考证" onClick="doPrint('cardNo')">
<input type="button" value="打印色彩" onClick="doPrint('s')">
<input type="button" value="打印素描" onClick="doPrint('m')">
<input type="button" value="打印速写" onClick="doPrint('x')">
<br />
<div class="cardNo">
<div style="width: 200px;height: 120px;font-size: 12px;border:1px solid #0099ff;margin-top:10px;margin-left:5px;">
<div style="margin-top: 15px;margin-left: 7px;">姓名:${examCardEntity.studentName }</div>
<div style="margin-top: 15px;margin-left: 7px;">考室:${examCardEntity.roomNo }</div>
<div style="margin-top: 15px;margin-left: 7px;">画室:${examCardEntity.studioName }</div>
<div style="margin-top: 15px;margin-left: 7px;">准考证号:${examCardEntity.examCardNo }</div>
</div>
</div>
<div class="sc">
<div style="width: 215px;height: 120px; margin-top:15px; margin-left:5px; border:1px solid green;font-size: 11px;">
<div style="float: left;width: 110px">
<div style="margin-top: 10px; margin-left: 2px;">准考证:${examCardEntity.examCardNo }</div>
<div style="margin-top: 10px; margin-left: 2px;">姓名:${examCardEntity.studentName }</div>
<div style="margin-top: 10px; margin-left: 2px;">科目:色彩</div>
<div style="margin-top: 10px; margin-left: 2px;">考场:${examCardEntity.roomNo }</div>
<div style="margin-top: 10px; margin-left: 2px;">画室:${examCardEntity.studioName }</div>
</div>
<div style="float: right;margin-right: 2px;" >
<img style="margin-top: 15px;" width="91px" height="91px" src="<%=LwCommon.QRCODE_HTTP %>/${examCardEntity.batchId }/S/${examCardEntity.resourceId }.png">
</div>
</div>
</div>
<div class="sm">
<div style="width: 215px;height: 120px; margin-top:15px; margin-left:5px; border:1px solid green;font-size: 11px;">
<div style="float: left;width: 110px">
<div style="margin-top: 10px; margin-left: 2px;">准考证:${examCardEntity.examCardNo }</div>
<div style="margin-top: 10px; margin-left: 2px;">姓名:${examCardEntity.studentName }</div>
<div style="margin-top: 10px; margin-left: 2px;">科目:素描</div>
<div style="margin-top: 10px; margin-left: 2px;">考场:${examCardEntity.roomNo }</div>
<div style="margin-top: 10px; margin-left: 2px;">画室:${examCardEntity.studioName }</div>
</div>
<div style="float: right;margin-right: 2px;" >
<img style="margin-top: 15px;" width="91px" height="91px" src="<%=LwCommon.QRCODE_HTTP %>/${examCardEntity.batchId }/M/${examCardEntity.resourceId }.png">
</div>
</div>
</div>
<div class="sx">
<div style="width: 215px;height: 120px; margin-top:15px; margin-left:5px; border:1px solid green;font-size: 11px;">
<div style="float: left;width: 110px">
<div style="margin-top: 10px; margin-left: 2px;">准考证:${examCardEntity.examCardNo }</div>
<div style="margin-top: 10px; margin-left: 2px;">姓名:${examCardEntity.studentName }</div>
<div style="margin-top: 10px; margin-left: 2px;">科目:速写</div>
<div style="margin-top: 10px; margin-left: 2px;">考场:${examCardEntity.roomNo }</div>
<div style="margin-top: 10px; margin-left: 2px;">画室:${examCardEntity.studioName }</div>
</div>
<div style="float: right;margin-right: 2px;" >
<img style="margin-top: 15px;" width="91px" height="91px" src="<%=LwCommon.QRCODE_HTTP %>/${examCardEntity.batchId }/X/${examCardEntity.resourceId }.png">
</div>
</div>
</div>
</body>
</html>
网上查看还有其他的高级功能。设置打印的纸张,设置默认打印机等,本项目我没有去研究了。