Jasperreport configuration and troubleshooting!

Source: Internet
Author: User

Jasperreport PDF Chinese

Source: http://blog.sina.com.cn/s/blog_4ffe1dc90100bnt8.html

In-depth code analysis of the cause of the problem, I soon found that jasperreports was calling itext. jar (another open-source project) encountered a problem when creating the font. After reading the use of itext, there were two other packages itextasian when using the Asian Font. jar and itextasiancmaps. jar, and associate them to the Project. The problem is solved!

 

When ireport and jasperreport are used as report generation tools, Chinese support is frequently encountered. I recently developed a J2EE WEB Project to use this tool to develop reports, and encountered similar problems, this article summarizes the content related to Chinese support when ireport and jasperreport are used to develop reports, hoping to help readers.
1. Running Environment
1.1 jasperreport 1.2.8
Jasperreports is the core content of ireport. It is a powerful open-source report generation tool that can output content to the screen, printer or generate PDF, HTML, xls, CSV and XML files. It is fully written in Java and can be used to generate dynamic report content in a variety of Java applications (including J2EE or Web applications. The jasperreports organization uses JDBC to access data in a relational database based on the report design defined in an XML file. Before filling the report data, the report design must be compiled into a Jasper file.
: Http://jasperreports.sourceforge.net/
1.2 ireport 1.2 8
Ireport is an excellent visual development tool for creating jasperreport XML files. It allows you to easily design report templates. The current maximum version is ireport 1.3.0.
: Http://ireport.sourceforge.net
1.3 itext 1.3.1
Itext is an open source Java class library used to generate PDF files.
: Http://itext.sourceforge.net
1.4 itextasian. Jar
To display Chinese and other Asian characters in the generated PDF file, you must also download the itext Asian character package.
: Http://itextdocs.lowagie.com/downloads/iTextAsian.jar
The above four packages must be added to the build path of the application or to the classpath, if the web application is placed in the corresponding WEB-INF/lib.
2. Set Chinese support
To display Chinese characters in a report, it is not enough to add the above four packages (because the above four packages only support report generation and Chinese display ), you must set the relevant attributes of each Display object on the report. The attribute settings are described as follows:
Font Name: (Chinese font)
PDF Font Name: stsong-light
UniGB-UCS2-H (Chinese siplified)
PDF embeded: √
As shown in (Display object property settings ):
 
Note: Make sure that the itextasian. Jar Asian Language Pack is already included in the project. Otherwise, a compilation error occurs.
If you think it is too difficult to set each object, you can use ireport's "format "?" Set the fonts menu and set it to the default report font. As shown in:
 
3. Possible Problems
After the above settings, the Chinese report display should be okay. I encountered several problems during the report development process, which are summarized as follows:
3.1 Problem Description
Run ireport1.2.8 in WINXP. When designing a report, set the font of the data field to a Chinese font, adjust the format, compile and run the report, and use jrviewer to preview the report. Everything is normal (the Chinese display is normal ), however, the following error occurs when previewing in PDF or Excel:
Cocould not load the following Font:
Pdffontname: stsong-light
Pdfencoding: UniGB-UCS2-H
Ispdfembedded: True
Cause: the Asian language pack into PDF is not included in the project
Solution: Include the Asian Language Pack itextasian. jar into the item. The iport port may be used for "options "?" Classpath "sets itextasian. jar to add classpath or directly add itextasian. copy jar to Lib in the ireport installation directory. if it is a web project, itextasian should be added. jar to the WEB-INF/lib/of this project.
3.2 Problem Description
Run ireport1.2.8 in WINXP. When designing a report, set the font of the data field to a Chinese font, adjust the format, compile and run the report, and use jrviewer to preview the report. Everything is normal (the Chinese display is normal ), PDF and Excel previews are displayed without any errors, but Chinese characters cannot be displayed (only blank ).
Cause: the PDF Font attribute of the report Display object is incorrectly set.
Solution: Set the report object's font attributes as follows:
Font Name: (Chinese font)
PDF Font Name: stsong-light
UniGB-UCS2-H (Chinese siplified)
PDF embeded: True
As shown in the following figure:
 
3.3 Problem Description
When a PDF or Excel file is generated, all Chinese characters are displayed normally, but garbled characters are displayed when an HTML file is generated.
Cause: Mainly webpage Encoding Problems
Solution
When HTML is generated on the JSP page, set <% @ page contenttype = "text/html; charset = gb2312" %>
Response. setcontenttype ("text/html; charset = gb2312 ");
If you have set and filtered the project encoding, you need to consider it separately, but it is mainly focused on coding. for example, the most recently developed project is based on Struts. The encoding is unified and filtered. When an HTML page is generated, the HTML page can be correctly generated without coding, there will be no garbled characters. If it is set to charset = gb2312, garbled characters will appear. in short, it is annoying to unify and process the encoding.
In addition, we can view the jrxml generated by ireport (<? XML version = "1.0" encoding = "UTF-8"?>) Files can be found that the iport port saves the Chinese characters are UTF-8 encoding. Understanding its encoding can help us unify the encoding method.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.