SpringBoot(十六)_springboot整合JasperRe

renke發表於2021-09-09

現在專案上要求實現套打,結果公司裡有個人建議用JaperReport進行實現,就進入這個東西的坑中。好歹經過掙扎現在已經脫離此坑中。現在我也是僅能實現讀取資料庫資料轉成pdf進行展示,包括中文的展示。於是記錄下整個過程。

1.下載 安裝 Jaspersoft Studio

下載地址:

圖片描述

我下載的就是6.6.0這個版本,Jasper Report 分為專業版(收費)和社群版(免費),這裡下載的社群版本。

2.設計模板

對這個模板設計我也不是很熟悉,這裡我就不展開說明了。大家自行設計吧

2.1 匯入並設定字型

這裡需要注意一點就是,這個設計出的不顯示中文,需要匯入字型。

點選window->Preferences->jaspersoft Studio->font->add

圖片描述

設定完成後,點選Finish.

2.2 設計模板選擇設定的myfont字型

檢視jrxml檔案後,設定後會多出font標籤

                <font fontName="myfont" size="26" pdfFontName="" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
                </textElement>
                <text><![CDATA[三年二班學生資訊]]></text>

2.3 匯出myfont字型jar包

點選window->Preferences->jaspersoft Studio->font

選擇myfont 點選export 匯出。這裡我儲存為kevin.jar

2.4 將kevin.jar 安裝到本地maven庫

mvn install:install-file -Dfile=kevin.jar -DgroupId=com.kevin -DartifactId=myfont -Dversion=1.0.0 -Dpackaging=jar

至此,前期的準備工作都已經完成。

3.構建springboot專案

這裡我使用的版本是 2.1.1.RELEASE

3.1 pom檔案

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>net.sf.jasperreports</groupId>
            <artifactId>jasperreports</artifactId>
            <version>6.6.0</version>
        </dependency>
        <dependency>
            <groupId>com.kevin</groupId>
            <artifactId>myfont</artifactId>
            <version>1.0.0</version>
        </dependency>
    </dependencies>

注意 ,我這裡引用的是我設定的com.kevin.myfont版本,個人根據自己步驟2.4設定的情況進行修改

3.2 application.yml

# Server settingsserver:  port: 8080# SPRING PROFILESspring:  http:
    encoding.charset: UTF-8
    encoding.enable: true
    encoding.force: true  datasource:    url: jdbc:mysql://127.0.0.1:3306/kevin?characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8    username: root    password: 123456    driver-class-name: com.mysql.jdbc.Driver

3.3 ReportController 程式碼

@RestControllerpublic class ReportController {

    @Resource
    private DataSource dataSource;    /**
     * 轉換為pdf展示
     *
     * @param reportName
     * @param parameters
     * @param response
     * @throws SQLException
     * @throws ClassNotFoundException
     * @throws JRException
     * @throws IOException
     */
    @GetMapping("/{reportName}")    public void getReportByParam(            @PathVariable("reportName") final String reportName,            @RequestParam(required = false) Map<String, Object> parameters,
            HttpServletResponse response) throws SQLException, ClassNotFoundException, JRException, IOException {

        parameters = parameters == null ? new HashMap<>() : parameters;        //獲取檔案流
        ClassPathResource resource = new ClassPathResource("jaspers" + File.separator + reportName + ".jasper");
        InputStream jasperStream = resource.getInputStream();

        JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperStream);
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, dataSource.getConnection());        // JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, new JREmptyDataSource());
        response.setContentType("application/pdf");
        response.setHeader("Content-Disposition", "inline;");        final OutputStream outputStream = response.getOutputStream();
        JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);
    }
}

3.4 完整目錄結構

圖片描述

4.執行效果

啟動專案,執行

圖片描述

完整程式碼

github:

原文出處:https://www.cnblogs.com/zhenghengbin/p/10268495.html 

作者: 

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/430/viewspace-2820171/,如需轉載,請註明出處,否則將追究法律責任。

相關文章