- jaspersoft 設計pdf模闆遇到的坑
1.項目啟動的時候
.jrxml生成的檔案.jasper每次用完以後删除,下次再用.jfxml生成檔案
每次導入idea以後要重新編譯:
mvn clean install -DskipTests 進行編譯
編譯成功以後再進行對項目單機右鍵,選擇-》maven-》Reimport
最後再啟動項目
2.制作内容時候
JasperReport做報表
在畫框框圖的時候,parameters代表的是一個參數a,它可以通過parameters.set("a",xx)可以進行傳值
filed代表是一個字段,可以通過進行List的傳遞
param最好放在文本的Head中,field最好放在文本的Detail中,如果放放在一起,可能會存在Field出現多次的(觀察原檔案可看出來位置發生變化)
3.父子傳遞設計過程中
在父子傳遞的設定過程中,父首先要設定兩個parameters參數:一個是path(指定子的路徑),一個是list(指定子的資料源集合)
為了引入子在父中加入Palette中的Subreport :
在Subreport設定Expression:P{path}并且Data Source Expression
填寫以下内容
new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{list})
子的話就隻需要在Detail裡面寫好prameter等待父的參數即可
在設插入圖檔過程中
1.設定圖檔的Expression 為$P{imagesPaths}
2.Lazy 勾選上
3.Using Cache 為true
4在parameters中添加imagesPaths參數
解決中文亂碼問題:
首先在項目中resource下引入三個檔案 jasperreportes_extension.properties
stsong 檔案包含:fonts.xml 和 stsong.TFT
其次在設計表的時候每個字段都要設計為 華文宋體
執行過程中可能出現讀取失敗的問題,重複1裡面的内容啟動項目即可
4.可能再resource裡面檔案讀取的時候會出現被攔截的情況
對插件進行了修改
<plugins>
<plugin>
<artifactId>maven-resource-plugin</artifactId>
<inherited>false</inherited>
<configuration>
<nonfilteredFileExtension>jasper</nonfilteredFileExtension>
</configuration>
</plugin>
</plugins>
最後,上代碼
public String getReceipt(ReceiptBo receiptBo, List<ReceiptItemBo>receiptItemBos,List<MedisDescBo>medisDes,String documentNo,Integer type){
try(InputStream fis = new ClassPathResource("template/SellReceipt.jasper").getInputStream();
ByteArrayOutPutStream byteArrayOutPutStream = new ByteArrayOutPutStream();
//讀取模闆檔案
InputStream childTemplateFile = new CalssPathResource("template/MediaDesc.jasper").getInputStream();
InputStream childTemplateFile2 = new CalssPathResource("template/ReceiptItemBos.jasper").getInputStream();
InputStream pictureInputStream = new CalssPathResource("picture/logo.png").getInputStream()){
//通過ObjectMapper将對象轉為map
ObjectMapper oMapper = new ObjectMapper();
Map<String,Object>parameters = oMapper.converValue(receiptBo,Map.class);
parameters.put("imagesPaths",pictureInputStream);
parameters.put("isPrinted","");
parameters.put("mediaDesc",mediaDesc);
parameters.put("mediaDescPath",childTemplateFile);
Parameters.put("documentNo",documentNo);
parameters.put("receiptItemBos",receiptItemBos);
parameters.put("receiptItemPath",childTemplateFile2);
if(type!=null){
return getPngReceipt(fis,parameters);
}else{
JasperPrint print = JasperFillManager.filReport(fis,parameters,new JREmptyDataSource());
//轉換為流
JasperExportManager.exportReportToPdfStream()
}
}
}
資料長度的自适應
Strech,在設計器裡面有個屬性叫:Strech With Overflow
當資料長度超過元件的長度的時候,會自動折行
導出圖檔Base64字元串
private String getPngReceipt(InputStream fis,Map<String,Object>parameters){
try{
JasperPrint jasperPrint =
JasperFillManager.fillReport(
(JasperReport) JRLoader.loadObject(fis),//jasper對象 parameters,//參數清單 new JREmptyDataSource()//資料源資訊);
JRGraphics2DExporter exporter = new JRGraphics2DExporter();//建立graphics輸出器
//建立一個影像對象
BufferedImage bufferedImage = new BufferedImage(jasperPrint.getPageWidth() * 4, jasperPrint.getPageHeight() * 4, BufferedImage.TYPE_INT_RGB);
//取graphics
Graphics2D g = (Graphics2D) bufferedImage.getGraphics();
//設定相應參數資訊
exporter.setParameter(JRGraphics2DExporterParameter.GRAPHICS_2D, g);
exporter.setParameter(JRGraphics2DExporterParameter.ZOOM_RATIO, Float.valueOf(4));
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.exportReport();
g.dispose();//釋放資源資訊
//這裡的bufferedImage就是最終的影像圖像資訊,可以通過這個對象導入到cm中了.
ImageIO.write(bufferedImage, "JPEG", new File("d:/aa.jpg"));
ByteArrayOutStream os = new ByteArrayOutPutStream();
ImageIO.write(bufferedImage,"PNG",os);
retrun Base64.getEncoder.encodeToString(os.toByteArray()).trim();
}catch(JRException e){
log.error("ReceiptService:getPngReceipt",e);
}catch(IOException e){
log.error("ReceiptService:getPngReceipt",e)
}
return null;
}
}
生成條形碼需要的pom
<dependencuy>
<groupId>net.sourceforge.barbecue</groupId>
<artifactId>barbecue</artifactId>
<version>1.5-betal</version>
</dependencuy>
這種引入條形碼的方式可以使用jasper檔案,設計器中直接使用$P{parameter}即可
生成二維碼需要的pom
https://blog.csdn.net/dailuwen/article/details/52249128
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>2.2</version>
</dependency>
一維碼 ($F{WAYBILL_NO} 為條碼資料)
com.google.zxing.client.j2se.MatrixToImageWriter.toBufferedImage(new com.google.zxing.oned.Code128Writer().encode($F{WAYBILL_NO},com.google.zxing.BarcodeFormat.CODE_128,188,40))
二維碼 ($F{WAYBILL_NO} 為條碼資料)
com.google.zxing.client.j2se.MatrixToImageWriter.toBufferedImage(new com.google.zxing.qrcode.QRCodeWriter().encode($F{WAYBILL_NO},com.google.zxing.BarcodeFormat.QR_CODE,200,180))
這種方式導入的是圖檔直接 修改組建 屬性 Image Experssion
父子模闆的另一種書寫方式
https://www.cnblogs.com/penghongwei/p/9471801.html
我們在生産代碼中可以先将子報表的模闆預編譯好,然後直接以參數的形式傳遞給主報表(例如如下代碼将編譯好的 “checklist.jrxml” 子報表作為參數傳給主報表,然後在主報表中的引用表達式寫成$P{checklist})。
String subreport = "checklist";
JasperReport jasperReport = null;
try (InputStream inputStream = getResourceAsStream(subreport + ".jrxml")) {
jasperReport = JasperCompileManager.compileReport(inputStream);
} catch (IOException | JRException e) {
e.printStackTrace();
}
params.put(subreport, jasperReport);
JasperCompileManager.compilerReport(inputStream)
Image.read(inputStream)