ireport导出pdf记录
生活随笔
收集整理的這篇文章主要介紹了
ireport导出pdf记录
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
項目中有個需求需要導出pdf,折騰了一周,記錄一下。
模板是這樣的,前面一段文字,中間一個動態表格,動態表格后面接著又是一大段靜態文本。
模板大概如下:
$P{參數key} 的參數需要定義在Parameter里面
$F{參數key}需要定義在Filed里面,右邊也有這個字段的一些屬性。
設計的時候想得挺美好,但結果卻不是你想的那樣。動態表格跟底部的合計斷開了,中間空白了一大片,很納悶。
測試了一下,當動態表格后面的內容不多,加上動態表格一頁能夠放得下的時候才會鏈接在一起。
當動態表格后面的內容很多,產生了分頁的話,表格就會斷開。
Table表格組件
于是,就想著換一種做法,用了一個Tabel表格組件,模板如下:
2.這里可以選擇顏色,表頭啥的,根據自己需要選擇了。因為我這個表格是動態的,動態頭,動態屬性,所以我這里就不需要表頭了。
?3.選中表格控件,右鍵,選擇Edit table datasource
?4.創建表格時,會默認給你創建一個Tabel Dataset 1,表格的Sub dataset就是指向了這個數據集。數據是JSON傳進來的,表達式如下:detailList是從代碼中傳進來的
new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{detailList})?
這個table表格我是單獨放了一個detail band 里面的,合計放在了第二個detail band里面,后面再繼續接其他內容。
?代碼
@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塊就遍歷幾遍。* 因為我使用的表格的數據來源是detailList,在我的對象中有這個屬性* 所以這里傳入的list就隨便給了一個*/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>?最終導出效果如下:
總結
以上是生活随笔為你收集整理的ireport导出pdf记录的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ruby on rails中的分页插件K
- 下一篇: 使用Photon PUN创建简单对战游戏