Java項(xiàng)目導(dǎo)出數(shù)據(jù)為 PDF 文件的操作代碼
Java項(xiàng)目如何導(dǎo)出數(shù)據(jù)為 PDF 文件?
一個(gè)小需求,需要將頁(yè)面上的數(shù)據(jù)導(dǎo)出為PDF,正常情況下這個(gè)需求需要讓前端來(lái)做,但是現(xiàn)在上面讓咱們后端來(lái)做,也沒(méi)問(wèn)題。
直接上代碼
一、代碼結(jié)構(gòu)如下

二、代碼說(shuō)明
1、添加依賴 pom.xml
<dependency>
<groupId>com.openhtmltopdf</groupId>
<artifactId>openhtmltopdf-core</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.openhtmltopdf</groupId>
<artifactId>openhtmltopdf-pdfbox</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.11.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>3.0.11.RELEASE</version>
</dependency>2、HTML模板文件 audit_order_record.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
<meta charset="UTF-8"/>
<title th:text="${pdfFileName}"></title>
<style>
@page {
size: A4;
@top-center {
content: element(header);
}
@bottom-center {
content: '';
font-family: 'stxihei', serif;
}
}
html, body {
font-family: 'stxihei', serif;
}
table {
-fs-table-paginate: paginate;
border-collapse: collapse;
}
#page-header {
position: running(header);
border-bottom: 1px solid #555;
padding: 5px;
}
#page-header-text {
font-size: 16px;
}
#page-header-num:after {
float: right;
text-align: right;
content: counter(page) '/' counter(pages);
}
table {
width: 100%;
margin: auto 0;
}
table, td, th {
border: 1px solid #555;
padding: 10px;
text-align: center;
}
th {
background-color: #eff3fa;
width: 200px;
}
</style>
</head>
<body>
<div id="page-header">
<span id="page-header-text">審批記錄</span>
<span id="page-header-num"></span>
</div>
<table>
<tr>
<th colspan="2">資源信息</th>
</tr>
<tr>
<th>目錄名稱</th>
<td>[[${resourceName}]]</td>
</tr>
<tr>
<th>資源名稱</th>
<td>[[${infoName}]]</td>
</tr>
<tr>
<th>資源類型</th>
<td>[[${resourceType}]]</td>
</tr>
<tr>
<th>數(shù)據(jù)提供方</th>
<td>[[${resOrgName}]]</td>
</tr>
<tr>
<th colspan="2">訂閱信息</th>
</tr>
<tr>
<th>資源需求方</th>
<td>[[${orgName}]]</td>
</tr>
<tr>
<th>需求方系統(tǒng)</th>
<td>[[${systemName}]]</td>
</tr>
<tr>
<th>聯(lián)系人</th>
<td>[[${linkMan}]]</td>
</tr>
<tr>
<th>聯(lián)系手機(jī)</th>
<td>[[${linkPhone}]]</td>
</tr>
<tr>
<th>申請(qǐng)日期</th>
<td>[[${orderDate}]]</td>
</tr>
<tr>
<th>申請(qǐng)理由</th>
<td>[[${orderReason}]]</td>
</tr>
<tr>
<th colspan="2">審核信息</th>
</tr>
<tr>
<th>審核人</th>
<td>[[${prejuUserName}]]</td>
</tr>
<tr>
<th>審核結(jié)果</th>
<td>[[${prejuResult}]]</td>
</tr>
<tr>
<th>審核日期</th>
<td>[[${prejuDate}]]</td>
</tr>
<tr>
<th>審核意見(jiàn)</th>
<td>[[${prejuSuggest}]]</td>
</tr>
</table>
</body>
</html>
3、添加字體
將字體文件 STXIHEI.TTF 放在 pdf/fonts 下面
4、PDF 導(dǎo)出工具類
package com.libin.pdf.demo.util;
import com.openhtmltopdf.pdfboxout.PdfRendererBuilder;
import org.springframework.util.ResourceUtils;
import org.thymeleaf.context.Context;
import org.thymeleaf.spring5.SpringTemplateEngine;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
import org.thymeleaf.templateresolver.FileTemplateResolver;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.OutputStream;
import java.util.Map;
public class PdfUtil {
/**
* @param response http請(qǐng)求后的相應(yīng)
* @param pdfFileName pdf文件名稱(不包含pdf后綴)
* @param templateName 模板名稱
* @param variables 模板變量
*/
public static void exportPdf(HttpServletResponse response, String pdfFileName, String templateName, Map<String, Object> variables) throws Exception {
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment;filename=" + new String((pdfFileName + ".pdf").getBytes(), "iso8859-1"));
OutputStream os = response.getOutputStream();
//構(gòu)造模板引擎
ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver();
resolver.setPrefix("pdf/templates/"); //模板所在目錄,相對(duì)于當(dāng)前classloader的classpath。
resolver.setSuffix(".html"); //模板文件后綴
// FileTemplateResolver resolver = new FileTemplateResolver();
// resolver.setPrefix(ServletActionContext.getServletContext().getRealPath("pdf/templates") + File.separator); // 如果放在web下
// resolver.setSuffix(".html");
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(resolver);
//構(gòu)造上下文(Model)
Context context = new Context();
context.setVariable("pdfFileName", pdfFileName);
context.setVariables(variables);
//渲染模板
String example = templateEngine.process(templateName, context);
PdfRendererBuilder builder = new PdfRendererBuilder();
//設(shè)置字體文件
builder.useFont(ResourceUtils.getFile("classpath:pdf/fonts/SIMHEI.TTF"), "stxihei");
// builder.useFont(new File(ServletActionContext.getServletContext().getRealPath("/pdf/fonts/SIMHEI.TTF")), "stxihei"); // 如果是放在web下面
builder.useFastMode();
builder.withHtmlContent(example, null);
builder.toStream(os);
builder.run();
}
}
5、導(dǎo)出接口
package com.libin.pdf.demo.controller;
import com.libin.pdf.demo.util.PdfUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
@RestController
public class DemoController {
@GetMapping("pdf")
public void test(HttpServletResponse response) throws Exception {
Map<String, Object> variables = new HashMap<>();
// 資源信息
// 資源名稱
variables.put("resourceName", "XXXXXXXXXX");
// 目錄名稱
variables.put("infoName", "XXXXXXXXXX");
//資源類型
variables.put("resourceType", "XXXXXXXXXX");
// 數(shù)據(jù)提供方
variables.put("resOrgName", "XXXXXXXXXX");
// 資源需求方
variables.put("orgName", "XXXXXXXXXX");
// 需求方系統(tǒng)
variables.put("systemName", "XXXXXXXXXX");
// 聯(lián)系人
variables.put("linkMan", "XXXXXXXXXX");
// 聯(lián)系手機(jī)
variables.put("linkPhone", "XXXXXXXXXX");
// 申請(qǐng)日期
variables.put("orderDate", "XXXXXXXXXX");
// 申請(qǐng)理由
variables.put("orderReason", "XXXXXXXXXX");
// 審核人
variables.put("prejuUserName", "XXXXXXXXXX");
// 審核結(jié)果
variables.put("prejuResult", "XXXXXXXXXX");
// 審核日期
variables.put("prejuDate", "XXXXXXXXXX");
// 審核意見(jiàn)
variables.put("prejuSuggest", "XXXXXXXXXX");
PdfUtil.exportPdf(response, "審批記錄", "audit_order_record.html", variables);
}
}
6、打開(kāi)瀏覽器測(cè)試
瀏覽器訪問(wèn) localhost:8080/pdf 進(jìn)行測(cè)試
三、效果圖

到此這篇關(guān)于Java項(xiàng)目導(dǎo)出數(shù)據(jù)為 PDF 文件的操作代碼的文章就介紹到這了,更多相關(guān)java導(dǎo)出PDF文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot3集成ElasticSearch的方法詳解
Elasticsearch是一個(gè)分布式、RESTful風(fēng)格的搜索和數(shù)據(jù)分析引擎,適用于各種數(shù)據(jù)類型,數(shù)字、文本、地理位置、結(jié)構(gòu)化數(shù)據(jù)、非結(jié)構(gòu)化數(shù)據(jù),本文給大家詳解介紹了SpringBoot3集成ElasticSearch的方法,需要的朋友可以參考下2023-08-08
Spring?AOP實(shí)現(xiàn)用戶登錄統(tǒng)一驗(yàn)證功能
這篇文章主要為大家詳細(xì)介紹了Spring?AOP如何實(shí)現(xiàn)用戶登錄統(tǒng)一驗(yàn)證功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)具有一定的借鑒價(jià)值,需要的可以參考一下2023-01-01
Java 實(shí)戰(zhàn)項(xiàng)目之畢業(yè)設(shè)計(jì)管理系統(tǒng)的實(shí)現(xiàn)流程
讀萬(wàn)卷書(shū)不如行萬(wàn)里路,只學(xué)書(shū)上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+SSM+jsp+mysql+maven實(shí)現(xiàn)畢業(yè)設(shè)計(jì)管理系統(tǒng),大家可以在過(guò)程中查缺補(bǔ)漏,提升水平2021-11-11
java:提示程序包org.junit不存在時(shí)的解決方案
這篇文章主要介紹了java:提示程序包org.junit不存在時(shí)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
Java實(shí)現(xiàn)迅雷地址轉(zhuǎn)成普通地址實(shí)例代碼
本篇文章主要介紹了Java實(shí)現(xiàn)迅雷地址轉(zhuǎn)成普通地址實(shí)例代碼,非常具有實(shí)用價(jià)值,有興趣的可以了解一下。2017-03-03
解決SpringBoot配置文件項(xiàng)目重啟出現(xiàn)亂碼的問(wèn)題
最近在創(chuàng)建了SpringBoot項(xiàng)目后往配置文件中寫(xiě)了相關(guān)的系統(tǒng)配置,并且在上面加了中文注釋,但是在重啟項(xiàng)目或開(kāi)機(jī)重啟后遇到了注釋亂碼的情況,下面這篇文章主要給大家介紹一下如何解決SpringBoot配置文件項(xiàng)目重啟出現(xiàn)亂碼的問(wèn)題,需要的朋友可以參考下2023-06-06
基于Spring Batch向Elasticsearch批量導(dǎo)入數(shù)據(jù)示例
本文介紹了基于Spring Batch向Elasticsearch批量導(dǎo)入數(shù)據(jù)示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-02-02
Spring Boot 2.0 設(shè)置網(wǎng)站默認(rèn)首頁(yè)的實(shí)現(xiàn)代碼
這篇文章主要介紹了Spring Boot 2.0 設(shè)置網(wǎng)站默認(rèn)首頁(yè)的實(shí)現(xiàn)代碼,需要的朋友可以參考下2018-04-04

