在 web 报表应用中,经常会遇到批量打印的需求,即点击一次打印按钮,实现多张报表的打印输出。润乾报表提供了两种不同的打印方式(applet 打印、PDF 打印)实现批量打印需求,同时也支持横纵报表打印。下面我们就来看一下每一种方式的具体实现方法。
一、applet 批量打印
1、环境配置要求
applet 打印要求客户端至少正确配置了 jre 环境
目前 applet 打印只支持 IE 内核浏览器
2、实现方法
可通过访问 jsp 拼接参数的方式实现,示例 URL 为:
不带参数:
http://localhost:6868/demo/reportJsp/directprint.jsp?report={t1.rpx}{t2.rpx}
带参数:
http://localhost:6868/demo/reportJsp/directprint.jsp?report={t1.rpx}{t2.rpx(arg1=1;arg2=2)}
参数拼接格式说明:
report={无参数报表名 1}{无参数报表名 2}{报表 1( 参数 1=value1; 参数 2=value2;…)}{报表 2( 参数 1=value1; 参数 2=value2;…)}…&prompt=yes&needSelectPrinter=yes
3、示例源码
directprint.jsp 页面完整代码:
<%@ page contentType="text/html;charset=UTF-8" %> <%@ page import="com.raqsoft.report.view.*"%> <html> <body> <% //此JSP参数格式为:report={无参数报表名1}{无参数报表名2}{报表1(参数1=value1;参数2=value2;...)}{报表2(参数1=value1;参数2=value2;...)}...&prompt=yes&needSelectPrinter=yes request.setCharacterEncoding( "UTF-8" ); String report = request.getParameter( "report" ); //"请输入报表文件名及参数串report={无参数报表名}{报表1(参数1=value1;参数2=value2;...)}{报表2(参数1=value1;参数2=value2;...)}..." if( report == null || report.trim().length() == 0 ) throw new Exception( ServerMsg.getMessage(request,"jsp.dpArgError") ); String prompt = request.getParameter( "prompt" ); String needSelectPrinter = request.getParameter( "needSelectPrinter" ); String pages = request.getParameter( "pages" ); String copies = request.getParameter( "copies" ); if( pages == null ) pages = ""; if( copies == null ) copies = "1"; String appmap = request.getContextPath(); String serverPort = String.valueOf( request.getServerPort() ); String serverName = request.getServerName(); String appRoot = "http://" + serverName + ":" + serverPort + appmap; String printLabel = ServerMsg.getMessage(request,"jsp.dpPrint"); %> <object classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" codebase="<%=appRoot%>/jre-6u24-windows-i586.exe#Version=1,6,0,0" width="80" height="32" id="report1_directPrintApplet" style="vertical-align:middle"> <param name="name" value="report1_directPrintApplet"> <param name="code" value="com.raqsoft.report.view.applet.DirectPrintWithoutShow.class"> <param name="archive" value="<%=appmap%>/raqsoftReportApplet.jar"> <param name="type" value="application/x-java-applet;version=1.6"> <param name="appRoot" value="<%=appRoot%>"> <param name="dataServlet" value="/reportServlet;jsessionid=<%=session.getId()%>?action=1"> <param name="fileName" value="<%=report%>"> <param name="srcType" value="file"> <param name="fontFace" value="dialog"> <param name="fontSize" value="10pt"> <param name="fontColor" value="#808040"> <param name="backColor" value="#126356"> <param name="label" value="<%=printLabel%>"> <param name="needPrintPrompt" value="<%=prompt%>"> <param name="needSelectPrinter" value="<%=needSelectPrinter%>"> <param name="needSetPageSize" value="no"> <param name="scriptable" value="true"> <param name="paramCharset" value="UTF-8"> <param name="blackWhitePrint" value="no"> <param name="mirrorPrint" value="no"> <param name="icon" value="/raqsoft/images/print.gif"> <param name="copies" value="<%=copies%>"> <param name="pages" value="<%=pages%>"> <comment> <embed type="application/x-java-applet;version=1.6" width="80" height="32" id="report1_directPrintApplet" code="com.raqsoft.report.view.applet.DirectPrintWithoutShow.class" archive="<%=appmap%>/raqsoftReportApplet.jar" type="application/x-java-applet;version=1.6" appRoot="<%=appRoot%>" dataServlet="/reportServlet;jsessionid=<%=session.getId()%>?action=1" fileName="<%=report%>" srcType="file" fontFace="dialog" fontSize="10pt" fontColor="#808040" backColor="#126356" label="<%=printLabel%>" needPrintPrompt="<%=prompt%>" needSelectPrinter="<%=needSelectPrinter%>" needSetPageSize="no" scriptable="true" paramCharset="UTF-8" blackWhitePrint="no" mirrorPrint="no" copies="<%=copies%>" pages="<%=pages%>" icon="/raqsoft/images/print.gif"> </embed> </comment> </object> <script language=javascript> /* function raqsoft_printOver() { window.close(); }*/ </script> </body> </html>
二、PDF 批量打印
1、环境配置要求
浏览器安装了 PDF 插件。特殊地,IE 浏览器只识别 adobe reader 插件,不识别其他 PDF 插件
PDF 打印方式无浏览器限制,可兼容常用浏览器。
2、实现方法
可通过访问 jsp 拼接参数的方式实现,示例 URL 为:
不带参数:
http://localhost:6868/demo/reportJsp/pdfBatchPrint.jsp?report={t1.rpx}{t2.rpx}
带参数:
http://localhost:6868/demo/reportJsp/pdfBatchPrint.jsp?report={t1.rpx}{t2.rpx(arg1=1;arg2=2)}
参数拼接格式说明:
report={无参数报表名}{报表 1( 参数 1=value1; 参数 2=value2;…)}{报表 2( 参数 1=value1; 参数 2=value2;…)}
3、示例源码
pdfBatchPrint.jsp 页面完整代码:
<%@ page contentType="text/html;charset=UTF-8" %> <%@ page import="java.net.*" %> <%@ page import="com.raqsoft.report.view.*"%> <html> <title>PDF打印报表</title> <head> </head> <body> <% //此JSP参数格式为:report={无参数报表名1}{无参数报表名2}{报表1(参数1=value1;参数2=value2;...)}{报表2(参数1=value1;参数2=value2;...)} request.setCharacterEncoding( "UTF-8" ); String report = request.getParameter( "report" ); if( report == null || report.trim().length() == 0 ) throw new Exception( "请输入报表文件名及参数串report={无参数报表名}{报表1(参数1=value1;参数2=value2;...)}{报表2(参数1=value1;参数2=value2;...)}..." ); String src = request.getContextPath() + ServletMappings.getMapping( "com.raqsoft.report.view.ReportServlet" ) + "?action=45&report=" + URLEncoder.encode( report, "UTF-8" ); %> <object classid="clsid:CA8A9780-280D-11CF-A24D-444553540000" id=pdfobj width="100%" height="100%" border="1"> <param name="_Version" value="65539"> <param name="_ExtentX" value="20108"> <param name="_ExtentY" value="10866"> <param name="_StockProps" value="0"> <param name="SRC" value="<%=src%>"> <comment> <embed id=pdfobj1 type="application/pdf" src="<%=src%>" width="100%" height="95%"></embed> </comment> </object> </body> </html>
三、常见问题
此问题需要将 URL 中的特殊字符进行转义处理,在 Tomcat7 以上版本以及 IE 浏览器中测试时易出现。例如:
{ 转义后是:%7B
} 转义后是:%7D
也就是我们在测试时,需要把如下 URL 换成转义后的 URL:
原始 URL:
http://localhost:6868/demo/reportJsp/pdfBatchPrint.jsp?report={t1.rpx}{t2.rpx}
转义后可正常访问的 URL:
http://localhost:6868/demo/reportJsp/pdfBatchPrint.jsp?report=%7Bt1.rpx%7D%7Bt2.rpx%7D