服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - JAVA教程 - iReport生成pdf打印的实例代码

iReport生成pdf打印的实例代码

2021-02-24 13:48浅安 JAVA教程

下面小编就为大家分享一篇iReport生成pdf打印的实例代码,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

先下载ireport版本 ,我下载的是 iReport-5.0.4  编辑模板  employees_identity_print_templet

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
@RequestMapping("printEmpPdf")
 @ResponseBody
 public Result printEmpPdf(HttpServletRequest request,HttpServletResponse response, Parameter parameter){
 Result result=Result.getInitializedResult();
 try{
  parameter = generateParameter(parameter, request);
  Map<String,Object> map= parameter.getParam();
  List<EmployeeVO> employeeVOList=employeeBiz.printPdfBarcode(map);
 
  // 报表数据源
  JRDataSource jrDataSource = new JRBeanCollectionDataSource(employeeVOList);
  //模板地址
  String jasperSource_adidas= CommonUtils.getAllMessage("jdbc","EMPLOYEES_IDENTITY_PRINT_PDF_TEMPLET")+"employees_identity_print_templet.jasper";
  File reportFile = new File(jasperSource_adidas);
  Map<String, Object> param_common=new HashMap<String, Object>();
  param_common.put("subReport",jasperSource_adidas);
  JasperPrint jasperPrint;
  jasperPrint = JasperFillManager.fillReport(reportFile.getPath(), param_common, jrDataSource);
  String fullPath = String.valueOf(System.currentTimeMillis()).concat("_员工列表.pdf"); //生成PDF文件保存地址
  String filePdfUrl= CommonUtils.getAllMessage("jdbc","IREPORT_FILE_URL");
  JRPdfExporter pdfExporter = new JRPdfExporter();
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  pdfExporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
  pdfExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
  pdfExporter.exportReport();
  byte[] bytes = baos.toByteArray();
  File file=new File(filePdfUrl+fullPath);
  WebUtil.writeByteArrayToFile(file,bytes);
  result.baseSucResult(fullPath);
 }catch (Exception e){
  result.baseFailResult("EmployeeController printEmpPdf error",parameter);
  logger.error("EmployeeController printEmpPdf error",e);
 }
 return result;
 }

 这是需要的maven 包

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<dependency>
 <groupId>net.sf.jasperreports</groupId>
 <artifactId>jasperreports</artifactId>
 <version>5.6.0</version>
</dependency>
<dependency>
 <groupId>org.codehaus.groovy</groupId>
 <artifactId>groovy-all</artifactId>
 <version>2.2.2</version>
</dependency>
 
<!-- iReport PDF中文支持 -->
<dependency>
 <groupId>com.lowagie</groupId>
 <artifactId>itextasian</artifactId>
 <version>2.1.7.js2</version>
</dependency>
<dependency>
 <groupId>com.lowagie</groupId>
 <artifactId>itext</artifactId>
 <version>2.1.7.js2</version>
</dependency>
<dependency>
 <groupId>net.sourceforge.barbecue</groupId>
 <artifactId>barbecue</artifactId>
 <version>1.5-beta1</version>
</dependency>
 
<dependency>
 <groupId>ireport</groupId>
 <artifactId>ireport-utils</artifactId>
 <version>2.0.1</version>
</dependency>
 
<dependency>
 <groupId>commons-collections</groupId>
 <artifactId>commons-collections</artifactId>
 <version>3.2</version>
</dependency>
<dependency>
 <groupId>commons-digester</groupId>
 <artifactId>commons-digester</artifactId>
 <version>1.8</version>
</dependency>

以上这篇iReport生成pdf打印的实例代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:http://www.cnblogs.com/duoduo264/p/7920081.html

延伸 · 阅读

精彩推荐