Spring MVC+Jasper Report and the Chinese problem of generating PDF

Let me talk about the choice of reporting framework first. JasperReport and BIRT are both good JAVA reporting tools, but the integration of BIRT and Spring is more troublesome. There is a good article in the Spring official website forum, but I think it is still quite troublesome. Jasper is simpler.

 

There are many integration methods between SpringMVC and JasperReport. Here is the official method in the Spring documentation. This method is relatively simple and requires very little code. The disadvantage is that it is not automatic. Every time a report is added, the configuration file needs to be modified and the service restarted. It is not suitable for WEB services that specialize in reporting.

 

1. Add in the configuration file:

 

 

Xml code   Favorite code
  1. <beans:beanid="viewResolver"class="org.springframework.web.servlet.view.ResourceBundleViewResolver">    
  2.     <beans:propertyname="basename"value="views"/>    
  3. </beans:bean>  
 

Note the order of resolvers.

 

2. Add views.properties to the classpath. My files are placed under src/main/resources.

The content of views is:

 

 

Properties code   Favorite code
  1. #Set report output format  
  2. simpleReport.(class)=org.springframework.web.servlet.view.jasperreports.JasperReportsPdfView  
  3. #Set report file path  
  4. simpleReport.url=/WEB-INF/reports/report1.jasper  
  5. #Data key value  
  6. simpleReport.reportDataKey=datasource  
 

 

Each report requires a set of the above configuration. Spring supports automatic selection of report input formats at runtime. For details, you can check the official documentation of spring.

 

3. Edit the report with iReport. iReport version is 4.5.1

The pdf of iReport has a traditional Chinese problem, and there are many introduction articles on the Internet. There are itext-2.1.7.jar, iTextAsian-2.1.7.jar and iText-AsianCmaps-2.1.7 packages under lib of ireport. (downloadable in attachment)

I thought iReport4.5.1 could be more advanced, but I didn't expect to use iText-2.1.7 and the other two supporting packages, don't use the latest version, the latest version is invalid.

1) Add the above 3 packages to iReport Tools-->Options-->Classpath

2) In iReport Tools-->Options-->Fonts, tick the checkboxes in front of these 3 packages.

3) When editing the Text box of the report, edit its properties as follows

 

 Pdf Font  name:STSong-Light;

 Pdf Embedded: Check it

 Pdf Enccoding:UniGB-UCS2-H (Chinese Simplified)

4) Add these three jar packages to the ClassPath of the Eclipse project.

In this way, the pdf can display Chinese normally.

 

Copy the edited .jasper file to the Eclipse project.

 

 

4. Write Controller.

 

 

 

Java code   Favorite code
  1. import java.util.ArrayList;  
  2. import  java.util.List;  
  3.   
  4. import javax.servlet.http.HttpServletRequest;  
  5.   
  6. import org.slf4j.Logger;  
  7. import org.slf4j.LoggerFactory;  
  8. import org.springframework.stereotype.Controller;  
  9. import org.springframework.ui.ModelMap;  
  10. import org.springframework.web.bind.annotation.RequestMapping;  
  11. import org.springframework.web.servlet.ModelAndView;  
  12.   
  13. @Controller  
  14. @RequestMapping("/report.html")  
  15. publicclass ReportCtrl {   
  16.     privatestaticfinal Logger logger = LoggerFactory.getLogger(ReportCtrl.class);    
  17.       
  18.     @RequestMapping()  
  19.     public ModelAndView showReport(ModelMap model, HttpServletRequest request) {  
  20.         logger.info("ReportCtrl-showReport start!");  
  21.           
  22.         DataSource map = new DataSource();  
  23.         map.setPlanno("APP1-1039-0990");  
  24.         map.setPlanname( "Shanghai Taxi" );  
  25.         List<DataSource> beanData = new ArrayList<DataSource>();  
  26.         beanData.add (map);  
  27.         model.addAttribute( "datasource" , beanData);  //datasource and views.properties are configured the same  
  28.           
  29.         logger.info("ReportCtrl-showReport end!");  
  30.         return new  ModelAndView( "simpleReport" , model);  //simpleReport is the same as the one configured in views.properties   
  31.     }  
  32.   
  33.     //bean definition  
  34.     publicclass DataSource {   
  35.         private String planno = "";  
  36.         private String planname = "";  
  37.         public String getPlanno() {  
  38.             return planno;  
  39.         }  
  40.         publicvoid setPlanno(String planno) {   
  41.             this.planno = planno;  
  42.         }  
  43.         public String getPlanname() {  
  44.             return planname;  
  45.         }  
  46.         publicvoid setPlanname(String planname) {   
  47.             this.planname = planname;  
  48.         }  
  49.     }  
  50. }  
 

 

Run to display the report.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326962134&siteId=291194637