iReport生成pdf打印的实例代码

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

@RequestMapping("printEmpPdf")
 @ResponseBody
 public Result printEmpPdf(HttpServletRequest request,HttpServletResponse response, Parameter parameter){
 Result result=Result.getInitializedResult();
 try{
  parameter = generateParameter(parameter, request);
  Map map= parameter.getParam();
  List 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 param_common=new HashMap();
  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 包


 net.sf.jasperreports
 jasperreports
 5.6.0


 org.codehaus.groovy
 groovy-all
 2.2.2




 com.lowagie
 itextasian
 2.1.7.js2


 com.lowagie
 itext
 2.1.7.js2


 net.sourceforge.barbecue
 barbecue
 1.5-beta1



 ireport
 ireport-utils
 2.0.1



 commons-collections
 commons-collections
 3.2


 commons-digester
 commons-digester
 1.8

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

你可能感兴趣的:(iReport生成pdf打印的实例代码)