项目需要实现web批量打印工单,目前使用struts2+jasperreports-applet-5.6.1来实现这个功能。 package com.cywl.applet; import java.io.PrintWriter; import java.io.StringWriter; import java.net.URL; import javax.print.attribute.HashPrintRequestAttributeSet; import javax.print.attribute.PrintRequestAttributeSet; import javax.print.attribute.Size2DSyntax; import javax.print.attribute.standard.MediaSize; import javax.print.attribute.standard.MediaSizeName; import javax.swing.JOptionPane; import net.sf.jasperreports.engine.JRExporterParameter; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.JasperPrintManager; import net.sf.jasperreports.engine.export.JRPrintServiceExporter; import net.sf.jasperreports.engine.export.JRPrintServiceExporterParameter; import net.sf.jasperreports.engine.util.JRLoader; import net.sf.jasperreports.view.JRViewer; /** * Applet * @author zyz * */ @SuppressWarnings("serial") public class JRPrinterApplet extends javax.swing.JApplet { private URL url = null; private JasperPrint jasperPrint = null; private String flag; //判断打印还是打印预览,0表示打印,1表示打印预览 /** Creates new form AppletViewer */ public JRPrinterApplet(){ } /** * 初始化方法 */ public void init(){ String strUrl = getParameter("REPORT_URL"); flag = getParameter("REPORT_FLAG"); if (strUrl != null){ try{ url = new URL(getCodeBase(), strUrl); System.out.println("url="+url); }catch (Exception e){ StringWriter swriter = new StringWriter(); PrintWriter pwriter = new PrintWriter(swriter); e.printStackTrace(pwriter); JOptionPane.showMessageDialog(this, swriter.toString()); } }else{ JOptionPane.showMessageDialog(this, "init():Source URL not specified"); } } public void start() { if (url != null){ if (jasperPrint == null){ try{ System.out.println("开始获取报表数据"); jasperPrint = (JasperPrint)JRLoader.loadObject(url); System.out.println("完成获取报表数据"); }catch (Exception e){ StringWriter swriter = new StringWriter(); PrintWriter pwriter = new PrintWriter(swriter); e.printStackTrace(pwriter); JOptionPane.showMessageDialog(this, swriter.toString()); } } if (jasperPrint != null){ final JasperPrint print = jasperPrint; if(flag.equals("0")){ Thread thread = new Thread ( new Runnable(){ public void run(){ try{ /** 设置纸张 */ PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet(); System.out.println("开始设置打印纸张大小"); MediaSize ms = new MediaSize(241,279.4f,Size2DSyntax.MM,MediaSizeName.ISO_A4); printRequestAttributeSet.add(ms.getMediaSizeName()); System.out.println("完成设置打印纸张大小"); JRPrintServiceExporter pse = new JRPrintServiceExporter(); System.out.println("开始装载报表数据"); pse.setParameter(JRExporterParameter.JASPER_PRINT, print); System.out.println("完成装载报表数据"); System.out.println("开始加入纸张设置"); pse.setParameter(JRPrintServiceExporterParameter.PRINT_REQUEST_ATTRIBUTE_SET,printRequestAttributeSet); System.out.println("完成加入纸张设置"); pse.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG, Boolean.TRUE); pse.exportReport(); //JasperPrintManager.printReport(print, true); }catch (Exception e){ StringWriter swriter = new StringWriter(); PrintWriter pwriter = new PrintWriter(swriter); e.printStackTrace(pwriter); System.out.println(e.toString()); JOptionPane.showMessageDialog(null, swriter.toString()); } } } ); thread.start(); }else{ try { JRViewer view =new JRViewer(print); this.getContentPane().add(view); view.setVisible(true); view.requestFocus(); this.setVisible(true); view.setFocusable(false); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }else{ JOptionPane.showMessageDialog(this, "Empty report."); } }else{ JOptionPane.showMessageDialog(this, "start():Source URL not specified"); } } } Action package com.cywl.applet; import java.io.File; import java.io.ObjectOutputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import javax.servlet.ServletOutputStream; import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.JasperReport; import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource; import net.sf.jasperreports.engine.util.JRLoader; import org.apache.log4j.Logger; import org.apache.struts2.ServletActionContext; import com.action.BaseAction; import com.cywl.kis3.entity.Voucher; public class PrintVoucherAction extends BaseAction { private Voucher voucher; private String fkIsFalge; public Voucher getVoucher() { return voucher; } public void setVoucher(Voucher voucher) { this.voucher = voucher; } public static Logger logger = Logger.getLogger(PrintVoucherAction.class); public String execute() { printVoucher(); return SUCCESS; } /** * 工单打印 * * @return */ public String printVoucher() { // FkPrintTemple ft=new FkPrintTemple(); File reportFile = new File(ServletActionContext.getServletContext().getRealPath("/applets/voucher1.jasper")); List<Voucher> vouchers = new ArrayList<Voucher>(); if (fkIsFalge != null) {// 1 批量打印 0 打印 if (fkIsFalge.equals("1")) { // 判断是不是批量打印 1是批量打印,批量打印暂时不实现 // vouchers=this.printWorkOrderService.getWorkOrderforPrintInfo(workOrder, // coStrs, soStrs, ifMerge,fkIsFalge); } else { // vouchers=this.printWorkOrderService.getWorkOrderforPrintInfo(workOrder, // coStrs, soStrs, ifMerge,fkIsFalge); // 添加打印时间日志,暂时不要 // this.printWorkOrderService.saveWorkOrderlog(soStrs,coStrs, // empName, ifMerge); } } else { // vouchers=this.getPrintWorkOrderService().getWorkOrderforPrintInfo(workOrder, // coStrs, soStrs, ifMerge,fkIsFalge); } try { JasperReport jreport = (JasperReport) JRLoader.loadObject(reportFile); JasperPrint jprint = JasperFillManager.fillReport(jreport, new HashMap(),new JRBeanCollectionDataSource(vouchers)); ServletOutputStream ouputStream = this.getResponse().getOutputStream(); //JasperViewer.viewReport(jasperPrint,false); ObjectOutputStream oos = new ObjectOutputStream(ouputStream); oos.writeObject(jprint); oos.flush(); oos.close(); } catch (Exception e) { logger.error("Exception :printfkWorkOrder()>>>>>>>>>>>>>>>>工单打印异常 "); logger.error(e.getMessage(), e); e.printStackTrace(); } return null; } } jsp <html> <head> <title> JasperReports - Web Application Sample </title> <link rel="stylesheet" type="text/css" href="stylesheet.css" title="Style"> </head> <body BGCOLOR="#ffffff" LINK="#000099"> <span class="title">Embedded Viewer Applet</span> <br> <br> <APPLET CODE = "com.cywl.applet.JRPrinterApplet.class" CODEBASE = "applets" ARCHIVE = "jasperreports-applet-5.6.1.jar,commons-logging-1.1.1.jar,commons-collections-3.2.1.jar" WIDTH = "600" HEIGHT = "400"></XMP> <PARAM NAME = CODE VALUE = "com.cywl.applet.JRPrinterApplet.class" > <PARAM NAME = CODEBASE VALUE = "applets" > <PARAM NAME = ARCHIVE VALUE = "jasperreports-applet-5.6.1.jar,commons-logging-1.1.1.jar,commons-collections-2.1.1.jar" > <PARAM NAME="type" VALUE="application/x-java-applet;version=1.2.2"> <PARAM NAME="scriptable" VALUE="false"> <PARAM NAME = "REPORT_URL" VALUE ="printVoucher.do"> </APPLET> </body> </html> |
|
100分 |
获取动态数据的action,你首先测试一下确认可以使用…
在调试applet就确认原因….可以打开console调试… 还也许是applet的安全机制造成的…设置签名或者本地策略文件 |