1、jasperreport 生成pdf下载功能必须的jar包:
1、jasperreports-5.0.4.jar
2、commons-digester-2.1.jar
3、groovy-all-2.0.1.jar
4、itextpdf-5.4.0
5、itext-pdfa-5.4.0
6、itext-xtra-5.4.0
7、iText-2.1.7
8、iTextAsian
9、xbean.jar
2、用iReport软件编译生成下载所需的jaspr文件:
(1) 首先创建一张空白的Blank A4 页面,给它取相应的名称,然后设置页面属性,必须将"When No Data" 这个属性设置为All Sections, No Detail
(2)制作pdf的jrxml模板时,需要注意的几点:--重中之中
jrxml模板以下属性应设置为如图,其余默认,否则PDF导出为空白
对应摸一个元素块,比如title,以下属性应设置为如图,其余默认,否则PDF汉字不显示
设置字体时,由于工具默认的字体不支持汉字,应设置为“宋体”
(3)配置参数变量
说明:name为变量 名称,class为变量 的数据类型,当类型为java.util.Date时,在该变量的属性面板中的"Pattern"属性中设置日期的格式:如“yyyy-MM-dd”
每一个变量都要在''Blank When Null"属性前打钩,意思是当该变量的值为null时,显示为空白,否则会在打印的pdf页面中显示null
3、单页pdf组装
后台代码:
//模板路径
String filePath = request.getSession().getServletContext().getRealPath("/static/template/pdf/userInfo.jasper");
/属性集合
HashMap parameters = new HashMap();
.... //向parameters放入键值对,
注意:键名必须与jasper文件中的属性名保持一致。
new PdfUtil().exportPdfReport(filePath, "用户信息_"+System.currentTimeMillis(), null, parameters, response);
4、多页pdf组装
后台代码:
//多页PDF,实例化PDF集合
List jasperPrintList = new ArrayList();
//模板路径1
String filePath1 = request.getSession().getServletContext().getRealPath("/static/template/pdf/userInfo1.jasper");
//属性集合1
HashMap parameters1 = new HashMap();
JasperPrint jasperPrint1 = JasperFillManager.fillReport(filePath1, parameters1, new JREmptyDataSource());
jasperPrintList.add(jasperPrint1); //加入集合
//模板路径2
String filePath2 = request.getSession().getServletContext().getRealPath("/static/template/pdf/userInfo2.jasper");
//属性集合2
HashMap parameters2 = new HashMap();
JasperPrint jasperPrint2 = JasperFillManager.fillReport(filePath2, parameters2, new JREmptyDataSource());
jasperPrintList.add(jasperPrint2); //加入集合
//模板路径3
String filePath3 = request.getSession().getServletContext().getRealPath("/static/template/pdf/userInfo3.jasper");
//属性集合3 验收时企业年度基本情况
HashMap parameters3 = new HashMap();
JasperPrint jasperPrint3 = JasperFillManager.fillReport(filePath3, parameters3, new JREmptyDataSource());
jasperPrintList.add(jasperPrint3); //加入集合
new PdfUtil().exportMultiPdfReport(jasperPrintList, "用户信息_"+System.currentTimeMillis(),request,response);
5、下载pdf的工具类
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JExcelApiExporterParameter;
import net.sf.jasperreports.engine.export.JRPdfExporter;
/**
* pdf导出工具
* 支持多个PDF合并导出
* @author Administrator
*
*/
public class PdfUtil {
/**
* 导出pdf通用方法
* @param filePath
* @param filename
* @param list
* @param parameters
* @param response
* @throws Exception
*/
public void exportPdfReport(String filePath, String filename,
List list, HashMap parameters,
HttpServletResponse response) throws Exception {
OutputStream outputStream = null;
if (list == null) {
list = new ArrayList();
}
if (parameters == null) {
parameters = new HashMap();
}
try {
outputStream = response.getOutputStream();
// 循环list数据
JRDataSource source = new JRBeanCollectionDataSource(list, false);
// JasperFillManager.
// 给PDF填充数据
JasperPrint jasperPrint = JasperFillManager.fillReport(filePath,
parameters, source);
JRExporter pdf = new JRPdfExporter();
response.setContentType("application/pdf");
response.setHeader("Content-disposition", "attachment; filename="
+ new String(filename.getBytes("GB18030"), "iso8859-1")
+ ".pdf");
pdf.setParameter(JExcelApiExporterParameter.CHARACTER_ENCODING,
"utf-8");
pdf.setParameter(JExcelApiExporterParameter.JASPER_PRINT,
jasperPrint);
pdf.setParameter(JExcelApiExporterParameter.OUTPUT_STREAM,
outputStream);
pdf.exportReport();
outputStream.flush();
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (Exception ex) {
throw ex;
}
}
}
}
/**
* 多个PDF合并导出,解决ireport一次只能导一页的问题
* @param jasperPrintList
* @param filename
* @param request
* @param response
* @throws Exception
*/
public void exportMultiPdfReport(List jasperPrintList, String filename,
HttpServletRequest request,HttpServletResponse response) throws Exception {
OutputStream outputStream = null;
try {
outputStream = response.getOutputStream();
response.setContentType("application/pdf");
response.setHeader("Content-disposition", "attachment; filename="+ new String(filename.getBytes("GB18030"), "ISO-8859-1")+ ".pdf");
JRExporter jrExporter = new JRPdfExporter();
jrExporter.setParameter(JRExporterParameter.CHARACTER_ENCODING,"UTF-8");
jrExporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST,jasperPrintList);
jrExporter.setParameter(JRExporterParameter.OUTPUT_STREAM,outputStream);
jrExporter.exportReport();
outputStream.flush();
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (Exception ex) {
throw ex;
}
}
}
}
}