一、需求
Flex 作为用户界面,调用打印机(需要指定打印机名称)直接进行打印。
二、分析
Flex就目前而言,调用打印机时,每次都会弹出选择打印机的对话框。需点击确认按钮然后才进行打印。我需要打印时,直接打印出来,打印机由程序指定。同时为了打印内容易设计,我打印时用到了JasperReport。由于Applet可以指定打印机名称,我实现的方式如下:
Flex 调用 JavaScript
JavaScript 调用Applet
Applet调用Servlet
Servlet利用Jasperreport进行打印
三、实现(关键代码)
1.创建一个Servlet,负责调用jasperreport
public class JasperPrintServlet extends HttpServlet
{
/**
*
*/
public void service(
HttpServletRequest request,
HttpServletResponse response
) throws IOException, ServletException
{
ServletContext context = this.getServletConfig().getServletContext();
String param=request.getParameter("param");
String[] ps=param.split(",");
try
{
JasperCompileManager.compileReportToFile(context.getRealPath("/reports/WebappReport.jrxml"));
File reportFile = new File(context.getRealPath("/reports/WebappReport.jasper"));
if (!reportFile.exists())
throw new JRRuntimeException("File WebappReport.jasper not found. The report design must be compiled first.");
Map parameters = new HashMap();
parameters.put("P1", ps[0]);
parameters.put("P2", ps[1]);
parameters.put("BaseDir", reportFile.getParentFile());
JasperPrint jasperPrint = null;
JasperReport jasperReport = (JasperReport)JRLoader.loadObject(reportFile.getPath());
jasperPrint =
JasperFillManager.fillReport(
jasperReport,
parameters,
//new WebappDataSource()
new JREmptyDataSource()
);
request.getSession().setAttribute(BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
if (jasperPrint != null)
{
response.setContentType("application/octet-stream");
ServletOutputStream ouputStream = response.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(ouputStream);
oos.writeObject(jasperPrint);
oos.flush();
oos.close();
ouputStream.flush();
ouputStream.close();
}
else
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>JasperReports - Web Application Sample</title>");
out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"../stylesheet.css\" title=\"Style\">");
out.println("</head>");
out.println("<body bgcolor=\"white\">");
out.println("<span class=\"bold\">Empty response.</span>");
out.println("</body>");
out.println("</html>");
}
System.out.println("print ---------------------");
}
catch (Exception e)
{
e.printStackTrace();
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>JasperReports - Web Application Sample</title>");
out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"../stylesheet.css\" title=\"Style\">");
out.println("</head>");
out.println("<body bgcolor=\"white\">");
out.println("<span class=\"bnew\">JasperReports encountered this error :</span>");
out.println("<pre>");
e.printStackTrace(out);
out.println("</pre>");
out.println("</body>");
out.println("</html>");
return;
}
}
}
2.创建 Applet
public class HelloSwingApplet extends JApplet
{
private URL url = null;
private JasperPrint jasperPrint = null;
public HelloSwingApplet()
{
}
public void init2(String param)
{
String strUrl = getParameter("REPORT_URL");
if (strUrl != null)
{
try
{
url = new URL(getCodeBase(), strUrl+"?param="+param);
}
catch (Exception e)
{
StringWriter swriter = new StringWriter();
PrintWriter pwriter = new PrintWriter(swriter);
e.printStackTrace(pwriter);
JOptionPane.showMessageDialog(this, swriter.toString());
}
}
if (url != null)
{
try
{
jasperPrint = (JasperPrint)JRLoader.loadObject(url);
if (jasperPrint == null)
JOptionPane.showMessageDialog(this, "Empty report.");
}
catch (Exception e)
{
StringWriter swriter = new StringWriter();
PrintWriter pwriter = new PrintWriter(swriter);
e.printStackTrace(pwriter);
JOptionPane.showMessageDialog(this, swriter.toString());
}
}
}
}
}
3.Flex 的swf文件对应的html部分代码
function print(param){
document.applets[0].printReport(param);
}
function focusswf(){
report.focus();
}
</script>
<APPLET CODE = "HelloSwingApplet.class" ARCHIVE = "jasperreports-3.0.0-applet.jar" codebase="http://localhost:8500/webprint">
<PARAM NAME = "REPORT_URL" VALUE ="servlets/jasperprint">
</APPLET>
注意事项
注意红色代码:codebase="http://localhost:8500/webprint",这个值随具体环境做相应变化,Archive这个属性值也不可丢弃
4.Flex 调用 js 代码
if(ExternalInterface.available){
ExternalInterface.call("print",paravalues);
}
5.附上工程代码
速度极其慢,无法上传(好像現在無法上傳哦)
我现在将后缀名rar改成jpg上传,下载完了的话将jpg改成rar,再解压即可。
说明:client.jpg 是Flex 客户端;webprint.jpg 是服务器端,服务器端涉及到的lib是jasperreport3.0的lib加上blaszeDS的lib。在此不上传