对于jasperreport打印这个功能,遇到了一大堆问题,也只能一点一点解决我:

    1.现在我用的是jasperreport.jar是4.6版本。

      (1).因为网上查到:6.0以上版本已经不再支持java打印功能,只能支持页面打印。

      (2).如过可以用浏览器打印,那这个打印问题已经解决了

      (3).我不想再用5.6版本重新测试一遍了,因为官网给的jasperreport-apples.jar例子里面有很多问题,不想在是一遍

    2.对于程序的需要,:

      (1).最好在webapp(根目录)下新建一个applets(自命名)文件夹

      (2).applets文件夹放入jre1.6.exe或以上版本,因为jasperreport-4.5是jre1.6写的

      (3).从官网的jasperreport-4.6例子中,找到commons-collections.jar、commons-digester.jar、commons-logging.jar,和自己的log4j.jar、log4j-core.jar,放入applets

    3.对于jasperreports-applet-4.6.0.jar,也放入applets,但是等到测试的时候,这个jar包里有很多问题,如果提示这个class找不到,就从jasperreoprt-4.6.jar中粘进去

      创建jar包

      (1).官网给的例子jasperreport-applets-4.6.jar解压后,复制default.jasperreports.properties、net,新建文件夹,粘进去

      (2).将缺少的class从jasperreport-4.6.jar粘到你放的位置对应的位置

      (3).ctrl+p命令,将default.jasperreports.properties、net生成一个jasperreport-applets-4.6.jar,放入applets

    4.这是我把官网例子改了以后的代码

  

import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperPrintManager;
import net.sf.jasperreports.engine.util.JRLoader; import javax.swing.*;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.URL; /**
* Created by 朱星翰 on 2017/9/14.
*/
public class JRPrinterApplet extends javax.swing.JApplet {
private URL url;
private JasperPrint jasperPrint; public JRPrinterApplet(){ }
public void init() {
String strUrl = getParameter("REPORT_URL");
if (strUrl != null) {
try {
url = new URL(getCodeBase(), strUrl);
} catch (Exception e) {
StringWriter swriter = new StringWriter();
PrintWriter pwriter = new PrintWriter(swriter);
e.printStackTrace(pwriter);
JOptionPane.showMessageDialog(this, swriter.toString());
}
}else {
JOptionPane.showMessageDialog(this, "REPORT_URL:不能为空!");
}
}
@Override
public void start() {
if (url != null) {
if (jasperPrint == null) {
try {
jasperPrint = (JasperPrint) JRLoader.loadObject(url);
} 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; Thread thread = new Thread(new Runnable()
{
public void run()
{
try {
JasperPrintManager.printReport(print, true);
} catch (Exception e) {
StringWriter swriter = new StringWriter();
PrintWriter pwriter = new PrintWriter(swriter);
e.printStackTrace(pwriter);
JOptionPane.showMessageDialog(null, swriter.toString());
}
}
});
thread.start();
} else {
JOptionPane.showMessageDialog(this, "报表是空的!");
}
}
else {
JOptionPane.showMessageDialog(this, "REPORT_URL:不能为空!");
}
}
}
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.util.JRLoader; import javax.swing.*;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.URL; /**
* Created by 朱星翰 on 2017/9/14.
*/
public class JRViewApplet extends javax.swing.JApplet { private URL url;
private JasperPrint jasperPrint; public JRViewApplet(){ }
public void init() {
String strUrl = getParameter("REPORT_URL");
if (strUrl != null) {
try {
url = new URL(getCodeBase(), strUrl);
} catch (Exception e) {
StringWriter swriter = new StringWriter();
PrintWriter pwriter = new PrintWriter(swriter);
e.printStackTrace(pwriter);
JOptionPane.showMessageDialog(this, swriter.toString());
}
}else {
JOptionPane.showMessageDialog(this, "REPORT_URL:不能为空!");
}
} @Override
public void start() {
// Add your handling code here:
if (url != null) {
try {
if (jasperPrint == null) {
jasperPrint = (JasperPrint) JRLoader.loadObject(url);
}
if (jasperPrint != null) {
ViewerFrame viewerFrame = new ViewerFrame(this.getAppletContext(), jasperPrint);
viewerFrame.show();
}else {
JOptionPane.showMessageDialog(this, "报表是空的!");
}
}catch (Exception e) {
StringWriter swriter = new StringWriter();
PrintWriter pwriter = new PrintWriter(swriter);
e.printStackTrace(pwriter);
JOptionPane.showMessageDialog(this, swriter.toString());
}
} else {
JOptionPane.showMessageDialog(this, "REPORT_URL:不能为空!");
}
}
}
/*
* 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 javax.swing.JButton;
import javax.swing.JOptionPane; import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.view.JRViewer; /**
* @author Teodor Danciu (teodord@users.sourceforge.net)
* @version 4.6.0
*/
public class JRViewerPlus extends JRViewer { /*protected JButton btnPlus = new javax.swing.JButton();*/ public JRViewerPlus(JasperPrint jrPrint) throws JRException {
super(jrPrint); tlbToolBar.remove(btnSave);
tlbToolBar.remove(btnReload); /*btnPlus = new javax.swing.JButton();
btnPlus.setToolTipText("Plus...");
btnPlus.setText("Plus...");
btnPlus.setPreferredSize(new java.awt.Dimension(80, 23));
btnPlus.setMaximumSize(new java.awt.Dimension(80, 23));
btnPlus.setMinimumSize(new java.awt.Dimension(80, 23));
btnPlus.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnPlusActionPerformed(evt);
}
});
tlbToolBar.add(btnPlus, 0);*/
} protected void setZooms() {
this.zooms = new int[]{33, 66, 100, 133, 166, 200, 233};
this.defaultZoomIndex = 2;
} /*protected void btnPlusActionPerformed(java.awt.event.ActionEvent evt) {
JOptionPane.showMessageDialog(this, "I just wanted to let you know that you can extend the JRViewer to customize it.\n The button you have pushed was added this way.");
}*/ }
/*
* 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 net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.view.JRViewer; /**
* @author Teodor Danciu (teodord@users.sourceforge.net)
* @version 4.6.0
*/
public class JRViewerSimple extends JRViewer { /**
*
*/
public JRViewerSimple(JasperPrint jrPrint) throws JRException {
super(jrPrint);
tlbToolBar.remove(btnSave);
tlbToolBar.remove(btnReload);
} }
/*
* 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.applet.AppletContext;
import java.awt.BorderLayout;
import java.net.MalformedURLException;
import java.net.URL; import javax.swing.JOptionPane; import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRPrintHyperlink;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.view.JRHyperlinkListener; /**
* @author Teodor Danciu (teodord@users.sourceforge.net)
* @version 4.6.0
*/
public class ViewerFrame extends javax.swing.JFrame implements JRHyperlinkListener { private AppletContext appletContext; public ViewerFrame(AppletContext appletContext, JasperPrint jasperPrint) throws JRException {
initComponents();
this.appletContext = appletContext;
JRViewerPlus viewer = new JRViewerPlus(jasperPrint);
viewer.addHyperlinkListener(this);
this.pnlMain.add(viewer, BorderLayout.CENTER);
} public void gotoHyperlink(JRPrintHyperlink hyperlink) {
switch(hyperlink.getHyperlinkTypeValue()) {
case REFERENCE : {
try {
this.appletContext.showDocument(new URL(hyperlink.getHyperlinkReference()), "_blank");
} catch (MalformedURLException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
break;
}
case LOCAL_ANCHOR :
case LOCAL_PAGE : {
break;
}
case REMOTE_ANCHOR :
case REMOTE_PAGE : {
JOptionPane.showMessageDialog(this, "Implement your own JRHyperlinkListener to manage this type of event.");
break;
}
case NONE :
default : {
break;
}
}
} /** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {//GEN-BEGIN:initComponents
pnlMain = new javax.swing.JPanel(); setTitle("预览");
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
pnlMain.setLayout(new java.awt.BorderLayout()); getContentPane().add(pnlMain, java.awt.BorderLayout.CENTER); pack();
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
int width=1000;
int height=800;
setSize(new java.awt.Dimension(width, height));
setLocation((screenSize.width-width)/2,(screenSize.height-height)/2);
}//GEN-END:initComponents // Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JPanel pnlMain;
// End of variables declaration//GEN-END:variables }

      (1).把这些代码放入src下,生成class代码

      (2).将class生成jar,我命名print.jar

      (3).jar -cvf print.jar *.class  (生成jar包),放入applets

     5.签名    

      1.创建一个证书
      keytool -genkey -validity 1800 -keystore applet.store -alias applet  
      2.导出证书文件
      keytool -export -keystore applet.store -alias applet -file applet.cer
      3.对jar包进行签名

      jarsigner -keystore applet.store jasperreports-applet-4.6.0.jar applet
      

      jarsigner -keystore applet.store print.jar applet (注意:必须签名这个jar包,要不然打印报错)

     6.页面,自己处理了很多js,

      

       <a title="打印" onclick="is_ie('JRPrinterApplet')"><img src="${ctx}/static/Images/print.jpg" alt=""></a>
&nbsp;&nbsp;
<a title="预览" onclick="is_ie('JRViewApplet')" ><img src="${ctx}/static/Images/look.png" alt=""></a></a>
&nbsp;&nbsp;
<a title="更方便的使用打印功能" style="font-size: 11px" href="${ctx}/applets/applet.cer">是否安装安全证书?</a>
function is_ie(str){
if ((navigator.userAgent.indexOf('MSIE') >= 0 && navigator.userAgent.indexOf('Opera') < 0)||(navigator.userAgent.indexOf("Trident") > -1 && navigator.userAgent.indexOf("rv") > -1)){
var archive='print.jar,jasperreports-applet-4.6.0.jar,commons-logging-1.1.1.jar,commons-collections-2.1.1.jar,commons-digester-2.1.jar';
var REPORT_URL='${ctx}/jasper/all_printJasper?';
var codebase='${ctx}/applets'
var file='${ctx}/applets/jre-6u45-windows-i586.exe';
var scriptable=false
var type='application/x-java-applet;version=1.6.0'; $.each(args,function(key,value){
REPORT_URL+=key+"="+value+"&";
});
REPORT_URL=change(REPORT_URL);
REPORT_URL=encodeURI(REPORT_URL); var write='正在处理'+(str=='JRPrinterApplet'?'打印':'预览')+'...' +
'<object classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" width = "0" height = "0" codebase="'+file+'" mayscript>' +
'<param name = "code" value = "'+str+'.class" >' +
'<param name = "codebase" value = "'+codebase+'" >' +
'<param name = "archive" value = "'+archive+'" >' +
'<param name="type" value="'+type+'">' +
'<param name="scriptable" value="'+scriptable+'">' +
'<param name = "REPORT_URL" value ="'+REPORT_URL+'">' +
'<comment>' +
'<embed type="'+type+'" code="'+str+'.class" java_codebase="'+codebase+'" archive="'+archive+'" scriptable='+scriptable+' pluginspage="'+file+'" >' +
'<noembed></noembed>' +
'</embed>' +
'</comment>' +
'</object>'
document.write(write);
}else {
alert('预览打印仅支持ie浏览器')
}
}

最后,终于完成了,总之,觉的jasperreport-applets-4.6官网给的好多错误地方,只有慢慢去找问题

关于jasperreport对应java打印机的解决方案的更多相关文章

  1. Java Scoket之java.io.EOFException解决方案

    Java Scoket之java.io.EOFException解决方案   Socket接收数据的时候,常常会抛出java.io.EOFException异常,也没有明确的原因和提示,在网上搜搜,很 ...

  2. Java 图片处理解决方案:ImageMagick 快速入门

    一.ImageMagick介绍 ImageMagick是一个免费的创建.编辑.合成图片的软件,可以实现图片切割.颜色替换.图片缩略图.图片水印等各种效果.ImageMagick是免费开源软件,支持大多 ...

  3. Java 图片处理解决方案:ImageMagick 快速入门教程

    文章首发于[博客园-陈树义],点击跳转到原文Java 图片处理解决方案:ImageMagick 快速入门教程. ImageMagick介绍 ImageMagick是一个免费的创建.编辑.合成图片的软件 ...

  4. Java中文乱码解决方案

    Java中文乱码解决方案   1.中文乱码解决方案,确保每个文件的默认编码是UTF-8         加入 URIEncoding="UTF-8" 代码中的设置 1>在se ...

  5. Atitit.java jar hell解决方案-----Djava.ext.dirs in ide envi..

    Atitit.java jar hell解决方案-----Djava.ext.dirs in ide envi.. Atitit.java class flect solu jar hell use  ...

  6. java 乱码问题解决方案

    java  乱码问题解决方案 一.tomcat: <Connector         port="8080"         maxThreads="150&qu ...

  7. java compiler level does not match the version of the installed java project facet 解决方案

    项目出现 java compiler level does not match the version of the installed java project facet 错误,一般是项目移植出现 ...

  8. idea生成JAVADOC 报java.lang.IllegalArgumentException解决方案[终极]

    idea生成javadoc文档,总是会报  java.lang.IllegalArgumentException     at sun.net.www.ParseUtil.decode(ParseUt ...

  9. 【转】Unable to execute dex: Java heap space 解决方案(如何为eclipse.int 添加内存)

    原文网址:http://blog.csdn.net/zengyangtech/article/details/7003379 欢迎转载,转载请注明 http://blog.csdn.net/zengy ...

随机推荐

  1. Mybatis3.4.0不支持mybatis-spring1.2.5及以下版本

    今天将工程的Mybatis的版本由3.3.0升级到3.4.0导致程序运行错误,使用的mybatis-spring版本是1.2.3,错误内容如下,最后发现是SpringManagedTransactio ...

  2. 01 Android修改新建虚拟机存放的位置

    创建的Android虚拟机(即AVD)的位置是在C盘的当前用户文件夹下(C:\Users\用户名\.android\avd) 这一默认设置不怎么好,特别是C盘空间吃紧的话. 这里提供2个解决办法(分2 ...

  3. 安卓AsyncTack详解

    我们知道安卓中的UI线程不是线程安全的,即不能在UI线程中进行耗时操作,所以我们通常的做法是开启一个子线程来进行耗时操作,然后将处理后的结果运用Handler机制传递给UI线程,在UI线程中根据处理后 ...

  4. 听说你又一次放弃了Dagger2,来听老夫说道说道

    关于Dagger2是啥,用法网上已经有很多了.不过听说你已经很多次Dagger2从入门到放弃了,来来,让我这个老中医给你治一治.老夫手法纯熟,不会让你有任何痛苦的,放心读下去吧. 本文同步自 博主的私 ...

  5. 论文系统Step1:从日志记录中提取特定信息

    论文系统Step1:从日志记录中提取特定信息 前言 论文数据需要,需要实现从服务器日志中提取出用户的特定交互行为信息.日志内容如下: 自己需要获取"请求数据包一行的信息"及&quo ...

  6. JAVA之旅(二十二)——Map概述,子类对象特点,共性方法,keySet,entrySet,Map小练习

    JAVA之旅(二十二)--Map概述,子类对象特点,共性方法,keySet,entrySet,Map小练习 继续坚持下去吧,各位骚年们! 事实上,我们的数据结构,只剩下这个Map的知识点了,平时开发中 ...

  7. linux shell编程语句if、case.

    shell学习笔记--if,case shell的控制流结构主要有if语句.for语句.case语句.while语句.until语句这五种,在shell中这些语句的用法有点类似C语言,很容易学会,但也 ...

  8. Css详解之(伪类选择器)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. 网站开发进阶(三十一)js如何将html表格导出为excel文件(后记)

    js如何将html表格导出为excel文件(后记) 前言 项目前期做了个导出Excel表格的功能,但是经过测试发现只有在IE上才可以正确实现,在Chrome等浏览器中无法实现导出效果.经过上网搜索,尝 ...

  10. Android BLE与终端通信(五)——Google API BLE4.0低功耗蓝牙文档解读之案例初探

    Android BLE与终端通信(五)--Google API BLE4.0低功耗蓝牙文档解读之案例初探 算下来很久没有写BLE的博文了,上家的技术都快忘记了,所以赶紧读了一遍Google的API顺便 ...