当前位置:网站首页>Jasperreport generate PDF report section

Jasperreport generate PDF report section

2022-06-09 07:25:00 woshihedayu

1、 Use Jaspersoft Studio Make PDF Templates , Generate a jrxml file .
 Insert picture description here

2、 compile jrxml file , Generate jasper file , Then copy it to springboot In the project resources Under the table of contents .

3、 Introduce the required dependencies

<dependency>
   <groupId>net.sf.jasperreports</groupId>
    <artifactId>jasperreports</artifactId>
    <version>6.19.0</version>
</dependency>

4、 establish jasper Input stream object for file , Query the database , Get the data required by the report , Fill in PDF Inside , And then PDF The file is exported to the browser in the form of byte stream , Realize file download , The code is as follows

@SneakyThrows
@GetMapping("/{id}/pdf")
public void pdf(@PathVariable String id, HttpServletResponse response){
    
    ClassPathResource resource = new ClassPathResource("templates/profile.jasper");
    FileInputStream fis = new FileInputStream(resource.getFile());
    ServletOutputStream sos=null;
    try {
    
        response.addHeader("Content-Disposition","attachment;filename=employee.pdf");
        sos= response.getOutputStream();
        // Employee details 
        UserCompanyPersonal personal = userCompanyPersonalService.getById(id);
        // Employee position information 
        UserCompanyJobs jobs = userCompanyJobsService.getById(id);
        // Employee Avatar 
        String staffPhoto = systemFeignClient.getPhoto(id);
        Map<String,Object> params=new HashMap<>();
        Map<String, Object> personalMap = BeanMapUtils.beanToMap(personal);
        Map<String, Object> jobsMap = BeanMapUtils.beanToMap(jobs);
        params.putAll(personalMap);
        params.putAll(jobsMap);
        params.put("staffPhoto",staffPhoto);
        JasperPrint jasperPrint = JasperFillManager.fillReport(fis, params,new JREmptyDataSource());
        JasperExportManager.exportReportToPdfStream(jasperPrint,sos);
    }finally {
    
        sos.flush();
        sos.close();
    }
}

among ,params It's a map object , Used to direct to PDF Fill in the data , If you execute first

params.put("staffPhoto",staffPhoto);

After execution

params.putAll(personalMap);
params.putAll(jobsMap);

It can lead to params Medium staffPhoto Value is empty , Eventually lead to PDF Export is empty , The code runs with an error , and personalMap and jobsMap It doesn't contain staffPhoto This key . This phenomenon is very strange , Because of this error , Stuck me for a long time , Later adopted debug Only at the breaking point can we find the problem .

5、JasperReport Chinese is not supported by default , So it will lead to PDF Chinese in cannot be displayed , The solution is in resources Add fonts.xml、stsong.TTF and jasperreports_extension.properties Three files , As shown below
 Insert picture description here among ,stsong.TTF It is a font file in Chinese Song typeface ,fonts.xml The code is as follows

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

<fontFamilies>

    <!--<fontFamily name="Lobster Two">-->
    <!--<normal>lobstertwo/LobsterTwo-Regular.otf</normal>-->
    <!--<bold>lobstertwo/LobsterTwo-Bold.otf</bold>-->
    <!--<italic>lobstertwo/LobsterTwo-Italic.otf</italic>-->
    <!--<boldItalic>lobstertwo/LobsterTwo-BoldItalic.otf</boldItalic>-->
    <!--<pdfEncoding>Identity-H</pdfEncoding>-->
    <!--<pdfEmbedded>true</pdfEmbedded>-->
    <!--<!–-->
    <!--<exportFonts>-->
    <!--<export key="net.sf.jasperreports.html">'Lobster Two', 'Times New Roman', Times, serif</export>-->
    <!--</exportFonts>-->
    <!--–>-->
    <!--</fontFamily>-->
    <fontFamily name=" Chinese Songti ">
        <normal>stsong/stsong.TTF</normal>
        <bold>stsong/stsong.TTF</bold>
        <italic>stsong/stsong.TTF</italic>
        <boldItalic>stsong/stsong.TTF</boldItalic>
        <pdfEncoding>Identity-H</pdfEncoding>
        <pdfEmbedded>true</pdfEmbedded>
        <exportFonts>
            <export key="net.sf.jasperreports.html">' Chinese Songti ', Arial, Helvetica, sans-serif</export>
            <export key="net.sf.jasperreports.xhtml">' Chinese Songti ', Arial, Helvetica, sans-serif</export>
        </exportFonts>
        <!-- <locales> <locale>en_US</locale> <locale>de_DE</locale> </locales> -->
    </fontFamily>
</fontFamilies>

jasperreports_extension.properties The code is as follows

net.sf.jasperreports.extension.registry.factory.simple.font.families=net.sf.jasperreports.engine.fonts.SimpleFontExtensionsRegistryFactory
net.sf.jasperreports.extension.simple.font.families.dejavu=stsong/fonts.xml
原网站

版权声明
本文为[woshihedayu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203021422311309.html