JasperReport生成PDF文件

前言

最近公司项目中需要使用JasperReport报表生成工具,使用6.2版本。用Jaspersoft Studio工具画jasper模板,模板就不附上了。

所需JAR包

<dependency>
    <groupId>net.sf.jasperreportsgroupId>
    <artifactId>jasperreportsartifactId>
    <version>6.2.0version>
dependency>
<dependency>
    <groupId>com.itextpdfgroupId>
    <artifactId>itext-asianartifactId>
    <version>1.5.2version>
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引用

转载于:https://www.cnblogs.com/zhuqianchang/p/7274180.html

你可能感兴趣的:(JasperReport生成PDF文件)