SpringBoot集成itext導(dǎo)出PDF的過程
添加依賴
<!-- PDF導(dǎo)出 -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.11</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>準(zhǔn)備字體
因?yàn)檗D(zhuǎn)成pdf文件可能出現(xiàn)亂碼或者不展示中文,所以需要自定義字體
打開目錄 C:\Windows\Fonts

挑一個(gè)自己喜歡的字體,然后CV大法

代碼
controller新增方法
// 導(dǎo)出pdf
@GetMapping("/exportPdf")
public void exportPdf(HttpServletResponse response) throws DocumentException, IOException {
byte[] pdfBytes = userPdfExportService.exportUsersToPdf();
String filename = "用戶信息.pdf";
String encodedFilename = URLEncoder.encode(filename, StandardCharsets.UTF_8.toString()).replace("+", "%20");
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment; filename=" + encodedFilename);
response.setContentLength(pdfBytes.length);
response.getOutputStream().write(pdfBytes);
response.getOutputStream().flush();
response.getOutputStream().close();
}新增UserPdfExportService類
@Service
public class UserPdfExportService {
@Autowired
private ISysUserService sysUserService;
public byte[] exportUsersToPdf() throws DocumentException, IOException {
//查詢要導(dǎo)出的數(shù)據(jù)
List<SysUser> users = sysUserService.selectUserList(new SysUser());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Document document = new Document();
PdfWriter.getInstance(document, baos);
document.open();
// 添加標(biāo)題
document.add(new Paragraph("用戶列表"));
// 加載自定義字體
InputStream is = getClass().getResourceAsStream("/static/fonts/simfang.ttf");
if (is == null) {
throw new IOException("字體文件未找到");
}
byte[] fontBytes = toByteArray(is);
BaseFont baseFont = BaseFont.createFont("simfang.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED, true, fontBytes, null);
Font font = new Font(baseFont, 12);
// 創(chuàng)建表格
PdfPTable table = new PdfPTable(4);
table.setWidthPercentage(100);
// 在表格單元格中也應(yīng)使用相同的字體
table.addCell(new PdfPCell(new Phrase("用戶名", font)));
table.addCell(new PdfPCell(new Phrase("姓名", font)));
table.addCell(new PdfPCell(new Phrase("郵箱", font)));
table.addCell(new PdfPCell(new Phrase("手機(jī)號(hào)", font)));
for (SysUser user : users) {
table.addCell(new PdfPCell(new Phrase(user.getUserName(), font)));
table.addCell(new PdfPCell(new Phrase(user.getNickName(), font)));
table.addCell(new PdfPCell(new Phrase(user.getEmail(), font)));
table.addCell(new PdfPCell(new Phrase(user.getPhonenumber(), font)));
}
document.add(table);
document.close();
return baos.toByteArray();
}
/**
* 流轉(zhuǎn)byte字節(jié)
* @param is
* @return
* @throws IOException
*/
private byte[] toByteArray(InputStream is) throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[16384];
while ((nRead = is.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
buffer.flush();
return buffer.toByteArray();
}
}測(cè)試
記得添加請(qǐng)求頭


到此這篇關(guān)于SpringBoot集成itext導(dǎo)出PDF的文章就介紹到這了,更多相關(guān)SpringBoot導(dǎo)出PDF內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Springboot整合itext實(shí)現(xiàn)PDF文件合并
- SpringBoot3集成iText實(shí)現(xiàn)PDF導(dǎo)出功能
- SpringBoot集成iTextPDF的實(shí)例
- SpringBoot整合iText7導(dǎo)出PDF及性能優(yōu)化方式
- SpringBoot使用itext填充pdf表單及導(dǎo)出pdf的流程
- SpringBoot集成itext實(shí)現(xiàn)html轉(zhuǎn)PDF
- SpringBoot集成itextpdf實(shí)現(xiàn)根據(jù)模板動(dòng)態(tài)生成PDF
- SpringBoot使用iText7實(shí)現(xiàn)將HTML轉(zhuǎn)成PDF并添加頁眉頁腳水印
相關(guān)文章
詳解Java Web項(xiàng)目啟動(dòng)執(zhí)行順序
這篇文章主要介紹了詳解Java Web項(xiàng)目啟動(dòng)執(zhí)行順序,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-06-06
java HashMap,TreeMap與LinkedHashMap的詳解
這篇文章主要介紹了 java HashMap,TreeMap與LinkedHashMap的詳解的相關(guān)資料,這里提供實(shí)例代碼,幫助大家學(xué)習(xí)理解 這部分的內(nèi)容,需要的朋友可以參考下2016-11-11
PostMan如何傳參給@RequestBody(接受前端參數(shù))
這篇文章主要介紹了PostMan如何傳參給@RequestBody(接受前端參數(shù)),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
java識(shí)別一篇文章中某單詞出現(xiàn)個(gè)數(shù)的方法
這篇文章主要介紹了java識(shí)別一篇文章中某單詞出現(xiàn)個(gè)數(shù)的方法,涉及java字符解析操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
Spring boot + mybatis + Vue.js + ElementUI 實(shí)現(xiàn)數(shù)據(jù)的增刪改查實(shí)例代碼(一)
這篇文章主要介紹了Spring boot + mybatis + Vue.js + ElementUI 實(shí)現(xiàn)數(shù)據(jù)的增刪改查實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-05-05
Java生產(chǎn)1-100的隨機(jī)數(shù)簡(jiǎn)單實(shí)例(分享)
下面小編就為大家?guī)硪黄狫ava生產(chǎn)1-100的隨機(jī)數(shù)簡(jiǎn)單實(shí)例(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05
idea快捷鍵生成getter和setter,有構(gòu)造參數(shù),無構(gòu)造參數(shù),重寫toString方式
這篇文章主要介紹了java之idea快捷鍵生成getter和setter,有構(gòu)造參數(shù),無構(gòu)造參數(shù),重寫toString方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11

