IReport study notes, Study Notes
Overview
This section describes how to generate pdf documents based on jasper reports and data, solutions to Chinese font problems, and formatted output of date and time.
IReport version: 5.2.0
Generate maven dependency for pdf documents
<dependency> <groupId>net.sf.jasperreports</groupId> <artifactId>jasperreports</artifactId> <version>5.2.0</version></dependency>
Complete dependency tree:
[INFO] +- net.sf.jasperreports:jasperreports:jar:5.2.0:compile
[INFO] | +- commons-beanutils:commons-beanutils:jar:1.8.0:compile
[INFO] | +- commons-digester:commons-digester:jar:2.1:compile
[INFO] | +- com.lowagie:itext:jar:2.1.7.js2:compile
[INFO] | | +- bouncycastle:bcmail-jdk14:jar:138:compile
[INFO] | | +- bouncycastle:bcprov-jdk14:jar:138:compile
[INFO] | | \- org.bouncycastle:bctsp-jdk14:jar:1.38:compile
[INFO] | | +- org.bouncycastle:bcprov-jdk14:jar:1.38:compile
[INFO] | | \- org.bouncycastle:bcmail-jdk14:jar:1.38:compile
[INFO] | +- jfree:jcommon:jar:1.0.15:compile
[INFO] | +- jfree:jfreechart:jar:1.0.12:compile
[INFO] | +- xml-apis:xml-apis:jar:1.3.02:compile
[INFO] | +- eclipse:jdtcore:jar:3.1.0:compile
[INFO] | +- org.codehaus.castor:castor:jar:1.2:compile
[INFO] | +- com.fasterxml.jackson.core:jackson-core:jar:2.0.5:compile
[INFO] | +- com.fasterxml.jackson.core:jackson-databind:jar:2.0.5:compile
[INFO] | \- com.fasterxml.jackson.core:jackson-annotations:jar:2.0.5:compile
[INFO] \- sinobest:font-song:jar:1.0:compile
Here, font-song jar is a self-made font jar package: http://pan.baidu.com/s/1nzmkm.
SQL-based data 1. Configure the data source
public void test() throws JRException, IOException, SQLException { InputStream is = Main.class.getClassLoader().getResourceAsStream("reports/XZFYSQS-query.jasper"); Map<String, Object> parameters = new HashMap<String, Object>(); parameters.put("SQD_SYSID", "4028ca9850d19ccb0150d19cd1ae0000"); Connection conn = ...; byte[] datas = JasperRunManager.runReportToPdf(is, parameters, conn); conn.close();}
Data Based on JavaBean 1. Create a JavaBean
package cn.sinobest.jzpt.fzywgz.reports.pojo;public class XZFYSQS_RY { private java.lang.String SQR; private java.lang.String XB; private java.sql.Timestamp CSNY; ......}
2. Add the compilation path to iReport.
public void test() throws JRException, IOException { InputStream is = Main.class.getClassLoader() .getResourceAsStream(reports/XZFYSQS-javabean.jasper); List<XZFYSQS_RY> ryList = new ArrayList<XZFYSQS_RY>(); ryList.add(...); JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource( ryList); byte[] datas = JasperRunManager.runReportToPdf(is, null, dataSource); ...}
Chinese font Solution
When generating pdf content based on the template during programming, Chinese characters may not be printed. The solution is as follows:
- Add the font jar package: font-song
Http://pan.baidu.com/s/1NzMkm
- Chinese font, which can be simplified to simplified Chinese characters, such as,, and .
- Set Properties of the Chinese component
Date and Time formatting outputRight-click TextField and choose Field pattern.
For the date type, MM always displays 2-digit month, M Displays 1 or 2-digit as needed, and other fields and so on.