ireport4.5.1 客户端applet打印开发讲解

java后台代码:

		Connection conn = null;
		try{
			//加载驱动
			Properties prop = new Properties();
			InputStream inStream = PrintUtil.class.getClassLoader().getResourceAsStream("application.properties");//new FileInputStream("application.properties");
			prop.load(inStream);
			Class.forName(prop.getProperty("jdbc.driver"));
			conn = DriverManager.getConnection(prop.getProperty("jdbc.url"),prop.getProperty("jdbc.username"),prop.getProperty("jdbc.password"));
			prop.clear();
			inStream.close();
			String path = request.getSession().getServletContext().getRealPath("/ireports/"+ireportName);
			JasperPrint jasperPrint=JasperFillManager.fillReport(path,parameters,conn);//填充报表数据生成JasperPrint对象 
			HttpServletResponse response = Struts2Utils.getResponse();
			System.out.println("----------------------------------000000000000000");
			// response.setContentType("application/octet-stream"); 
	         ServletOutputStream ouputStream = (ServletOutputStream) response.getOutputStream(); 
	         ObjectOutputStream oos = new ObjectOutputStream(ouputStream); 
	         oos.writeObject(jasperPrint);//将JasperPrint对象写入对象输出流中
	         System.out.println("-----------------");
	         oos.flush(); 
	         oos.close(); 
			//JasperPrintManager.printReport(jasperPrint,false);//直接打印,不用预览PDF直接打印true为弹出打印机选择false为直接打印
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				conn.close();
			} catch (java.sql.SQLException e) {
				e.printStackTrace();
			}
		}

applet代码

package net.sf.jasperreports.applets;
/*
/*
 * JasperReports - Free Java Reporting Library.
 * Copyright (C) 2001 - 2011 Jaspersoft Corporation. All rights reserved.
 * http://www.jaspersoft.com
 *
 * Unless you have purchased a commercial license agreement from Jaspersoft,
 * the following license terms apply:
 *
 * This program is part of JasperReports.
 *
 * JasperReports is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * JasperReports is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with JasperReports. If not, see <http://www.gnu.org/licenses/>.
 */
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.URL;

import javax.swing.JOptionPane;

import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperPrintManager;
import net.sf.jasperreports.engine.util.JRLoader;


/**
 * @author Teodor Danciu (teodord@users.sourceforge.net)
 * @version $Id: PrinterApplet.java 4595 2011-09-08 15:55:10Z teodord $
 */
public class PrinterApplet extends javax.swing.JApplet{
	
	private static final long serialVersionUID = 1L;
	private URL url;
	private JasperPrint jasperPrint;
	/** Creates new form AppletViewer */
	public PrinterApplet(){
	}

	public void init(){
		String strUrl = getParameter("REPORT_URL");
		if (strUrl != null){
			try{
				url = new URL(getCodeBase(), strUrl);
			}catch (Exception e){
				e.printStackTrace();
				JOptionPane.showMessageDialog(this, "Source URL not exist");
			}
		}else{
			JOptionPane.showMessageDialog(this, "Source URL not specified");
		}
	}

	public void start() {//GEN-FIRST:event_btnPrintActionPerformed
		// Add your handling code here:
		if (url != null){
			if (jasperPrint == null){
				try{
					jasperPrint = (JasperPrint)JRLoader.loadObject(url);
				}catch (Exception e){
					e.printStackTrace();
					JOptionPane.showMessageDialog(this, "Source URL is error");
				}
			}
			
			if (jasperPrint != null){				
				try {
					JasperPrintManager.printReport(jasperPrint, true);
				}catch (Exception e){
					e.printStackTrace();
					StringWriter swriter = new StringWriter();
					PrintWriter pwriter = new PrintWriter(swriter);
					e.printStackTrace(pwriter);
					JOptionPane.showMessageDialog(null, swriter.toString());
				}
			}else{
				JOptionPane.showMessageDialog(this, "Empty report.");
			}
		}else{
			JOptionPane.showMessageDialog(this, "Source URL not specified");
		}
	}
	
}


页面代码:

function print(oid){
    	if(oid==""){
    		alert("报表没有数据!");
      		return;
     	}
     	document.getElementById("applets").innerHTML = '<APPLET CODE="net.sf.jasperreports.applets.PrinterApplet.class" NAME="printerApplet"'+
	     'CODEBASE="./" ARCHIVE="${ctx}/myeditor/applets/reportPrint.jar,${ctx}/myeditor/applets/commons-logging-1.1.1.jar,${ctx}/myeditor/applets/commons-collections-3.2.1.jar,${ctx}/myeditor/applets/commons-digester-2.1.jar" WIDTH="0" HEIGHT="0">'+
	     '<PARAM NAME="type" VALUE="application/x-java-applet;version=1.6.0">'+
	     '<PARAM NAME="scriptable" VALUE="false">'+
	     '<PARAM NAME="REPORT_URL" value="${ctx}/background/order!printdn.action?orderId='+oid+'">'+
	     '</APPLET>';
    }

开发说明,在任一磁盘根目录下新建一文件夹 temp,页面把jasperreports-4.5.1.jar解压,将解压后的 META-INF、net、default.jasperreports.properties(这个一定要复制,不然会报 NullPointer 异常)全部复制到 temp下,把PrinterApplet类的calss放到net\sf\jasperreports\applets里面,一定要注意PrinterApplet类的package net.sf.jasperreports.applets,CMD 命令, E:/temp> 下键入 jar -cvf reportprint.jar *,得到reportprint.jar 包。

在边同commons-logging-1.1.1.jar、commons-collections-3.2.1.jar、commons-digester-2.1.jar放在一起进行签名。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值