之前写过一篇 Spring Boot 将 Word 转换为 PDF 的文章,但是有评论说导入依赖有问题,还存在依赖冲突的问题。索性再来一个完整版的代码,之前的完整版代码找不到了,又重新整理了一下,依赖导入和之前不太一样,代码写法类似。
1、导入依赖
核心依赖:
<!-- Apache POI --><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.15</version></dependency><!-- PdfConverter --><dependency><groupId>fr.opensagres.xdocreport</groupId><artifactId>org.apache.poi.xwpf.converter.pdf</artifactId><version>1.0.6</version></dependency>
完整 pom.xml 文件
<?xml version="1.0" encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.18</version><relativePath/><!-- lookup parent from repository --></parent><groupId>com.riu</groupId><artifactId>spring-boot-demo</artifactId><version>0.0.1-SNAPSHOT</version><name>spring-boot-demo</name><description>spring-boot-demo</description><properties><java.version>8</java.version></properties><dependencies><!-- Web 启动器 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- Thymeleaf 启动器 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><!-- Apache POI --><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.15</version></dependency><!-- PdfConverter --><dependency><groupId>fr.opensagres.xdocreport</groupId><artifactId>org.apache.poi.xwpf.converter.pdf</artifactId><version>1.0.6</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
2、用于上传文件的页面
<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>Title</title></head><body><formaction="/w2p/convert"method="post"enctype="multipart/form-data"><inputtype="file"name="file"><inputtype="submit"value="转换"/></form>
[[${result}]]
</body></html>
3、控制层代码
importorg.springframework.http.HttpHeaders;importorg.springframework.http.HttpStatus;importorg.springframework.http.ResponseEntity;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.PostMapping;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestParam;importorg.springframework.web.multipart.MultipartFile;importjava.io.File;importjava.io.FileInputStream;importjava.io.IOException;@Controller@RequestMapping("/w2p")publicclassFileConversionController{/**
* 跳转 w2p 页面,提交文件
* @return
*/@GetMappingpublicStringw2p(){return"/w2p/w2p";}/**
* 文件转换:word 装换为 PDF
*
* @param file 源 word 文件
* @return
*/@PostMapping("/convert")publicResponseEntity<byte[]>convertWordToPdf(@RequestParam("file")MultipartFile file){try{// 创建 word 临时文件对象File wordFile =File.createTempFile("word",".docx");// 临时 word 文件写入磁盘
file.transferTo(wordFile);// 建 pdf 临时文件对象File pdfFile =File.createTempFile("pdf",".pdf");// 调用转换工具类WordToPdfConverter converter =newWordToPdfConverter();// 转换 PDF
converter.convertToPdf(wordFile, pdfFile);/* PDF 文件下载 */FileInputStream fis =newFileInputStream(pdfFile);byte[] bytes =newbyte[fis.available()];
fis.read(bytes);// 删除 word 临时文件
wordFile.delete();
fis.close();
pdfFile.delete();// 设置下载的响应头信息HttpHeaders hh =newHttpHeaders();
hh.setContentDispositionFormData("attachement", pdfFile.getName());returnnewResponseEntity<byte[]>(bytes, hh,HttpStatus.OK);/* PDF 文件下载 */}catch(IOException e){
e.printStackTrace();}returnnull;}}
4、PDF 转换类
importorg.apache.poi.xwpf.converter.pdf.PdfConverter;importorg.apache.poi.xwpf.usermodel.XWPFDocument;importjava.io.*;publicclassWordToPdfConverter{publicvoidconvertToPdf(File wordFile,File pdfFile)throwsIOException{InputStream inputStream =null;OutputStream outputStream =null;XWPFDocument document =null;try{// 文件输入流
inputStream =newFileInputStream(wordFile);// 文件输出流
outputStream =newFileOutputStream(pdfFile);
document =newXWPFDocument(inputStream);PdfConverter.getInstance().convert(document, outputStream,null);}catch(IOException e){
e.printStackTrace();}finally{// 释放资源
document.close();
outputStream.close();
inputStream.close();}}}
版权归原作者 Riu_Peter 所有, 如有侵权,请联系我们删除。