SpringBoot集成itextpdf實現(xiàn)根據(jù)模板動態(tài)生成PDF
需求說明
根據(jù)合同模板,將動態(tài)的合同標簽,合同方以及合同簽約時間等動態(tài)的生成PDF,供用戶下載打印。
前期準備
安裝 Adobe Acrobat DC
鏈接:https://pan.baidu.com/s/1T8wTCkxn0YcHjxZQ8G6WeQ
提取碼:j6b9
Spring Boot 集成
添加依賴
<dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.9</version> <scope>compile</scope> </dependency> <!--中文問題解決--> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itext-asian</artifactId> <version>5.2.0</version> </dependency>
構建工具類
public class PDFUtils { private static final Logger log = LoggerFactory.getLogger(PDFUtils.class); /** * 根據(jù)pdf模板輸出流 * @param templateFileName 模板文件名 * @param resultMap 包含文件字段名和值的map * @return 生成的文件字節(jié)流 */ public static ByteArrayOutputStream createPdfStream(String templateFileName, Map<String, String> resultMap){ ByteArrayOutputStream ba = new ByteArrayOutputStream(); PdfStamper stamp =null; PdfReader reader = null; try { reader = new PdfReader(templateFileName); stamp = new PdfStamper(reader, ba); //使用字體 BaseFont bf = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); /* 獲取模版中的字段 */ AcroFields form = stamp.getAcroFields(); //填充表單 if (resultMap != null) { for (Map.Entry<String, String> entry : resultMap.entrySet()) { form.setFieldProperty(entry.getKey(), "textfont", bf, null); form.setField(entry.getKey(), entry.getValue()!=null?entry.getValue():""); } } //不能編輯 stamp.setFormFlattening(true); } catch (IOException e) { log.error("文檔構建I/O異常",e); } catch (DocumentException e) { log.error("文檔構建異常",e); } finally { if(stamp!=null){ try { stamp.close(); } catch (DocumentException e) { log.error("流關閉錯誤",e); } catch (IOException e) { log.error("流關閉錯誤",e); } } if(reader!=null){ reader.close(); } } return ba; } }
構建MultipartFile
方便之后上傳OSS返回url
public UploadFileModel createUrl(String filePath, ByteArrayOutputStream byteArrayOutputStream) throws URISyntaxException { try{ byte[] pdfBytes = byteArrayOutputStream.toByteArray(); MultipartFile multipartFile = new MockMultipartFile( "file", filePath, "application/pdf", pdfBytes ); return uploadFileUtil.upload(multipartFile); } catch (Exception e) { log.error("創(chuàng)建Url時出錯:" + e.getMessage()); } return null; }
編輯PDF模板
Java代碼設置對應form的key-value
pdf模板放在springboot 項目目錄resources/static 目錄下
public String createContract(CreateContractRequest request) { HashMap<String, String> map = new HashMap<>(); map.put("companyName",request.getCompanyName()); map.put("phone",request.getPhone()); UploadFileModel url = null; ByteArrayOutputStream pdfStream = PDFUtils.createPdfStream(UserApplication.class.getResource("/").getPath() + "static/contract.pdf", map); try { url = createUrl("合同.pdf", pdfStream); } catch (URISyntaxException e) { throw new RuntimeException(e); } return url.getUrl(); }
到此這篇關于SpringBoot集成itextpdf實現(xiàn)根據(jù)模板動態(tài)生成PDF的文章就介紹到這了,更多相關SpringBoot itextpdf生成PDF內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java多線程基礎 線程的等待與喚醒(wait、notify、notifyAll)
這篇文章主要介紹了Java多線程基礎 線程的等待與喚醒,需要的朋友可以參考下2017-05-05Java getResource()如何獲取class文件目錄位置
這篇文章主要介紹了Java getResource()如何獲取class文件目錄位置,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12java WebSocket的實現(xiàn)以及Spring WebSocket示例代碼
本篇文章主要介紹了java WebSocket的實現(xiàn)以及Spring WebSocket,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-01-01maven中profile動態(tài)打包不同環(huán)境配置文件的實現(xiàn)
開發(fā)項目時會遇到這個問題:開發(fā)環(huán)境,測試環(huán)境,生產(chǎn)環(huán)境的配置文件不同, 打包時經(jīng)常要手動更改配置文件,本文就來介紹一下maven中profile動態(tài)打包不同環(huán)境配置文件的實現(xiàn),感興趣的可以了解一下2023-10-10Java如何根據(jù)前端返回的字段名進行查詢數(shù)據(jù)
這篇文章主要為大家詳細介紹了Java如何根據(jù)前端返回的字段名進行查詢數(shù)據(jù),文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下2024-11-11