JasperReport生成PDF文件
前言
最近公司项目中需要使用JasperReport报表生成工具,使用6.2版本。用Jaspersoft Studio工具画jasper模板,模板就不附上了。
所需JAR包
<dependency> <groupId>net.sf.jasperreports</groupId> <artifactId>jasperreports</artifactId> <version>6.2.0</version> </dependency> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itext-asian</artifactId> <version>1.5.2</version> </dependency>
生成PDF
public static void main(String[] args) throws Exception { String fileName = "D:\\test.jasper"; String outFileName = "D:\\test.pdf"; HashMap map = new HashMap(); JasperPrint print = JasperFillManager.fillReport(fileName, map, new JREmptyDataSource()); JRExporter exporter = new net.sf.jasperreports.engine.export.JRPdfExporter(); exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, outFileName); exporter.setParameter(JRExporterParameter.JASPER_PRINT, print); exporter.exportReport(); System.out.println("Created file: " + outFileName); }
常见错误
1.net.sf.jasperreports.engine.JRRuntimeException: Could not load the following font :
pdfFontName : STSong-Light
pdfEncoding : UniGB-UCS2-H
isPdfEmbedded : false
出现上述错误提示原因:缺少iTextAsian.jar引用