Spring Boot 整合 JasperReports 下载 PDF 报表文件

主要功能点:

Spring Boot Web 整合 JasperReports,在浏览器地址栏输入访问地址会直接下载 PDF 报表文件;整合过程中遇到的两个比较费时间的问题:

  1. Maven 依赖包下载失败:Cannot resolve com.lowagie:itext:2.1.7.js8 ;
  2. 生成的 PDF 文件中中文不显示。

1、Maven 依赖


    org.springframework.boot
    spring-boot-starter-web


    net.sf.jasperreports
    jasperreports
    6.19.1
    
        
            com.lowagie
            itext
        
    


    net.sf.jasperreports
    jasperreports-fonts
    6.19.1


    com.lowagie
    itext
    2.1.7

2、报表内容

报表文件名称:employees-details.jrxml

报表文件路径:src\main\resources\employees-details.jrxml

<?xml version="1.0" encoding="UTF-8"?>


    
    
    
    
        
        
            
        
        
        
        
        
        
        
            
        
    
    
    
    
        
    
    
    
    
    
    
        
    
    
        <band height="86" splitType="Stretch">
            <frame>
                <reportElement mode="Opaque" x="0" y="0" width="555" height="86" backcolor="#3BE3E3" uuid="57f80b95-2c82-4855-8b4f-797057ea6a99"/>
                <image scaleImage="FillFrame">
                    <reportElement x="82" y="0" width="410" height="40" uuid="c21b5d52-4d05-448d-a001-a0cd19a9aeb2"/>
                    <imageExpression><![CDATA["E:/1.png"]]></imageExpression>
                </image>
                <textField>
                    <reportElement style="Table_TH" mode="Transparent" x="90" y="50" width="400" height="30" backcolor="#FFFFFF" uuid="15957312-bfcc-43e7-ad7c-8cdb97c4197b">
                        <property name="com.jaspersoft.studio.unit.width" value="px"/>
                    </reportElement>
                    <box padding="0">
                        <pen lineWidth="1.0" lineStyle="Solid"/>
                        <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                        <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                        <bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                        <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    </box>
                    <textElement>
                        <font fontName="华文宋体" size="20" isBold="true" isItalic="true" isUnderline="true"/>
                    </textElement>
                    <textFieldExpression><![CDATA[$P{CompanyName}]]></textFieldExpression>
                </textField>
            </frame>
        </band>
    
    
        
            
                
                    
                
                
                    
                
            
        
    
    
        
            
            
                
                    
                    
                    
                    
                
                
                    
                        
                    
                    
                        
                        
                            
                                
                                
                                
                            
                        
                        
                            
                                
                                
                                    
                                
                                
                            
                        
                        
                        
                            
                                
                                
                                    
                                
                                
                            
                        
                    
                    
                        
                        
                            
                                
                                
                                
                            
                        
                        
                            
                                
                                
                                    
                                
                                
                            
                        
                        
                        
                            
                                
                                
                                    
                                
                                
                            
                        
                    
                    
                        
                        
                        
                            
                                
                                
                                    
                                
                                
                            
                        
                        
                        
                            
                                
                                
                                    
                                
                                
                            
                        
                    
                    
                        
                        
                        
                            
                                
                                
                                    
                                
                                
                            
                        
                        
                            
                        
                        
                            
                                
                                
                                    
                                
                                
                            
                        
                    
                
            
        
    
    
        
            
                
                
                
            
            
                
                
            
        
    

3、字体配置

字体文件位置:src\main\resources\static\font\chinese.stsong.ttf

字体配置文件位置:src\main\resources\static\font\fonts.xml

配置文件内容

<?xml version="1.0" encoding="UTF-8"?>

    
        static/font/chinese.stsong.ttf
        static/font/chinese.stsong.ttf
        static/font/chinese.stsong.ttf
        static/font/chinese.stsong.ttf
        Identity-H
        true
        
            '华文宋体', Arial, Helvetica, sans-serif
            '华文宋体', Arial, Helvetica, sans-serif
        
    

在 classpath 路径下(application.properties 同级)创建 Jasper 拓展文件 jasperreports_extension.properties

net.sf.jasperreports.extension.registry.factory.simple.font.families=net.sf.jasperreports.engine.fonts.SimpleFontExtensionsRegistryFactory
net.sf.jasperreports.extension.simple.font.families.lobstertwo=static/font/fonts.xml

4、实体对象 —— Employee.java

@Data
@NoArgsConstructor
public class Employee {

    private int id;
    private String name;
    private String role;
    private String address;

    public Employee(int id, String name, String role, String address) {
        this.id = id;
        this.name = name;
        this.role = role;
        this.address = address;
    }
}

5、控制器 —— EmployeeController.java

@RestController
public class EmployeeController {

    @GetMapping("/employee/records/report")
    public ResponseEntity getEmployeeRecordReport() {
        try {
            // 测试数据
            List empLst = createTestData();

            // 报表需要的动态参数
            Map empParams = new HashMap();
            empParams.put("CompanyName", "Spring Boot 整合 JasperReports");
            empParams.put("employeeData", new JRBeanCollectionDataSource(empLst));

            // 编译
            JasperReport jasperReport = JasperCompileManager.compileReport(
                    ResourceUtils.getFile("classpath:employees-details.jrxml").getAbsolutePath()
            );

            // 填充数据
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, empParams, new JREmptyDataSource());

            // 导出报表
            HttpHeaders headers = new HttpHeaders();
            // 设置响应格式:PDF
            headers.setContentType(MediaType.APPLICATION_PDF);
            // 设置文件名称
            headers.setContentDispositionFormData("filename", "employees-details.pdf");
            return new ResponseEntity(JasperExportManager.exportReportToPdf(jasperPrint), headers, HttpStatus.OK);

        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    private List createTestData() {
        List resultList = new ArrayList<>();
        resultList.add(new Employee(1, "汪小成", "程序员", "山东省济宁市任城区"));
        resultList.add(new Employee(2, "孙小顺", "部门经理", "安徽省合肥市蜀山区"));
        return resultList;
    }
}

6、查看效果

在谷歌浏览器地址栏输入 http://localhost:9000/employee/records/report 会直接弹出保存文件的对话框,实际效果如下:

Spring Boot 整合 JasperReports 下载 PDF 报表文件

附录

itext-2.1.7.js8.jar 可以在如下地址下载:

https://jaspersoft.jfrog.io/ui/native/third-party-ce-artifacts/com/lowagie/itext/2.1.7.js8/

发表评论
留言与评论(共有 0 条评论) “”
   
验证码:

相关文章

推荐文章