SpringBoot(十六)_springboot整合JasperReport6.6.0


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

1.下載 安裝 Jaspersoft Studio

下載地址:https://community.jaspersoft.com/community-download

我下載的就是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 settings
server:
  port: 8080

# SPRING PROFILES
spring:
  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 代碼

@RestController
public 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.運行效果

啟動項目,運行http://localhost:8080/demo

完整代碼

github:https://github.com/runzhenghengbin/SpringBoot

玩的開心!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM