从服务器获取报表模板到html页面显示报表pdf

本文介绍如何利用JasperReports 3.5.3及iReport工具生成PDF报告。主要步骤包括:从数据库获取数据并转换为List,加载报表模板,填充数据源,并通过JRPdfExporter导出为PDF格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1、我用到的jar包为jasperreports-3.5.3jar包。制作报表模板用到的工具为:iReport-nb-3.5.0

2、首先将从数据库中获取到的数据转成list.

3、public static <T extends com.pwyec.utils.report.javabean.BasePrintEntity> JasperPrint getPrintDataSource(String path,List<T> dataSource) throws ClientProtocolException, IOException, JRException{
/*通过文件名,获得模板文件*/
HttpClient httpclient = new DefaultHttpClient(); 
HttpGet httpgets = new HttpGet(path); 
/*将模板文件转成流*/
    HttpResponse response = httpclient.execute(httpgets);    
    HttpEntity entity = response.getEntity();  
    InputStream instreams = null;
    if (entity != null) {    
    instreams  = entity.getContent(); 
     
    }   
    JasperReport jasperReport = JasperCompileManager.compileReport(instreams);     
    JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(dataSource);
    Map parameters = new HashMap();
    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, beanColDataSource);
    return jasperPrint;
}

其中 path为模板配置文件的路径。dataSource为数据源。

用该方法将之转成JasperPrint

4、在控制器 我们拿到JasperPrint

@Token(save = true)
@RequestMapping(value = "printPdf", method = RequestMethod.GET)
public void printPdf(String id,
HttpServletRequest request, HttpServletResponse response) {
JasperPrint jasperPrint = null;
StoreJumpSession jump = (StoreJumpSession) WebUtil.getPrincipal();
IStoreSessionUser user = jump.getStoreUser();
try {
jasperPrint =getPrintDataSource(String path,List<T> dataSource);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JRException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


JRPdfExporter pdfExporter = new JRPdfExporter();
pdfExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
OutputStream stream = null;
ByteArrayOutputStream exportStream = new ByteArrayOutputStream();
try {
response.setCharacterEncoding("UTF-8");
response.setContentType("application/pdf");
pdfExporter.setParameter(JRExporterParameter.OUTPUT_STREAM,
exportStream);
pdfExporter.setParameter(JRExporterParameter.CHARACTER_ENCODING,
"UTF-8");
try {
pdfExporter.exportReport();
} catch (JRException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
PdfReader reader = new PdfReader(new ByteArrayInputStream(
exportStream.toByteArray()));
stream = response.getOutputStream();
PdfStamper pdfStamper = new PdfStamper(reader, stream);
pdfStamper.setViewerPreferences(PdfWriter.HideMenubar
| PdfWriter.HideToolbar | PdfWriter.HideWindowUI);
pdfStamper.addJavaScript(PrintHelper.getPrintScript());
pdfStamper.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (exportStream != null) {
try {
exportStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

public static String getPrintScript(){

StringBuffer script = new StringBuffer(); 
script.append("this.print({bUI: false,bSilent: true,bShrinkToFit: false});")
.append("\r\nthis.closeDoc();");

return script.toString();

然后在js写一个调用该控制器的方法就可以了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值