ireport导出pdf记录
項(xiàng)目中有個(gè)需求需要導(dǎo)出pdf,折騰了一周,記錄一下。
模板是這樣的,前面一段文字,中間一個(gè)動(dòng)態(tài)表格,動(dòng)態(tài)表格后面接著又是一大段靜態(tài)文本。
模板大概如下:
$P{參數(shù)key} 的參數(shù)需要定義在Parameter里面
$F{參數(shù)key}需要定義在Filed里面,右邊也有這個(gè)字段的一些屬性。
設(shè)計(jì)的時(shí)候想得挺美好,但結(jié)果卻不是你想的那樣。動(dòng)態(tài)表格跟底部的合計(jì)斷開了,中間空白了一大片,很納悶。
測(cè)試了一下,當(dāng)動(dòng)態(tài)表格后面的內(nèi)容不多,加上動(dòng)態(tài)表格一頁能夠放得下的時(shí)候才會(huì)鏈接在一起。
當(dāng)動(dòng)態(tài)表格后面的內(nèi)容很多,產(chǎn)生了分頁的話,表格就會(huì)斷開。
Table表格組件
于是,就想著換一種做法,用了一個(gè)Tabel表格組件,模板如下:
2.這里可以選擇顏色,表頭啥的,根據(jù)自己需要選擇了。因?yàn)槲疫@個(gè)表格是動(dòng)態(tài)的,動(dòng)態(tài)頭,動(dòng)態(tài)屬性,所以我這里就不需要表頭了。
?3.選中表格控件,右鍵,選擇Edit table datasource
?4.創(chuàng)建表格時(shí),會(huì)默認(rèn)給你創(chuàng)建一個(gè)Tabel Dataset 1,表格的Sub dataset就是指向了這個(gè)數(shù)據(jù)集。數(shù)據(jù)是JSON傳進(jìn)來的,表達(dá)式如下:detailList是從代碼中傳進(jìn)來的
new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{detailList})?
這個(gè)table表格我是單獨(dú)放了一個(gè)detail band 里面的,合計(jì)放在了第二個(gè)detail band里面,后面再繼續(xù)接其他內(nèi)容。
?代碼
@PostMapping("/print2")@ApiOperation("打印2")public void print2(@RequestBody PurchaseOrderDTO dto, HttpServletResponse response) {try {PurchaseOrderPrintVO printVO = productionPurchaseOrderService.findPrintData(dto.getPk(), dto.getOrderCode());Map<String, Object> stringObjectMap = BeanUtil.beanToMap(printVO); // List<Map<String, Object>> detailList = printVO.getDetailList();List<Object> list = new ArrayList<>();list.add(stringObjectMap);/*** 這里傳入的list長度有多少,模板中的detail塊就遍歷幾遍。* 因?yàn)槲沂褂玫谋砀竦臄?shù)據(jù)來源是detailList,在我的對(duì)象中有這個(gè)屬性* 所以這里傳入的list就隨便給了一個(gè)*/JRBeanCollectionDataSource jrBeanCollectionDataSource = new JRBeanCollectionDataSource(list);Resource resource = new ClassPathResource("jasper/report2.jasper");InputStream inputStream = resource.getInputStream();JasperPrint jasperPrint = JasperFillManager.fillReport(inputStream, stringObjectMap, jrBeanCollectionDataSource);response.setCharacterEncoding("utf-8");response.setContentType("application/pdf");response.setHeader("content-disposition", "attachment;filename=XXXXX合同.pdf");JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream());}catch (JRException e) {log.error("打印失敗,", e);} catch (Exception e) {log.error("打印失敗,", e);}}?依賴
<dependency><groupId>net.sf.jasperreports</groupId><artifactId>jasperreports</artifactId><version>6.10.0</version><exclusions><exclusion><groupId>com.lowagie</groupId><artifactId>itext</artifactId></exclusion></exclusions></dependency><dependency><groupId>com.lowagie</groupId><artifactId>itext</artifactId><version>2.1.7</version></dependency><dependency><groupId>org.codehaus.groovy</groupId><artifactId>groovy-all</artifactId><version>2.4.11</version></dependency><!-- 解決中文字體顯示問題 --><dependency><groupId>cn.lesper</groupId><artifactId>iTextAsian</artifactId><version>3.0</version></dependency>?最終導(dǎo)出效果如下:
總結(jié)
以上是生活随笔為你收集整理的ireport导出pdf记录的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ruby on rails中的分页插件K
- 下一篇: MySQL year函数