在开发过程中,对于word、Excel的不同格式转换也是一大难题,本片文章就针对如何使用java将word转为pdf
使用springboot进行具体java代码实现。
具体目录结构如下:
配置文件pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="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>3.2.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.soft</groupId>
<artifactId>spring-boot-word-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-word-test</name>
<description>spring-boot-word-test</description>
<properties>
<java.version>17</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>
工具类WordToPdfConverter:
package com.soft.utils;
import org.apache.poi.xwpf.converter.pdf.PdfConverter;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import java.io.*;
public class WordToPdfConverter {
public void convertToPdf(File wordFile, File pdfFile) throws IOException {
InputStream inputStream = null;
OutputStream outputStream = null;
XWPFDocument document = null;
try {
// 文件输入流
inputStream = new FileInputStream(wordFile);
// 文件输出流
outputStream = new FileOutputStream(pdfFile);
document = new XWPFDocument(inputStream);
PdfConverter.getInstance().convert(document, outputStream, null);
} catch (IOException e){
e.printStackTrace();
} finally {
// 释放资源
document.close();
outputStream.close();
inputStream.close();
}
}
}
控制层类FileConversionController:
package com.soft.controller;
import com.soft.utils.WordToPdfConverter;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
/**
*需求:使用Springboot将word转换为pdf
*/
@Controller
@RequestMapping("/w2p")
public class FileConversionController {
/**
* 跳转 w2p 页面,提交文件
* @return
*/
@GetMapping
public String w2p(){
return "/w2p/wordtest";
}
/**
* 文件转换:word 装换为 PDF
*
* @param file 源 word 文件
* @return
*/
@PostMapping("/convert")
public ResponseEntity<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 = new WordToPdfConverter();
// 转换 PDF
converter.convertToPdf(wordFile, pdfFile);
/* PDF 文件下载 */
FileInputStream fis = new FileInputStream(pdfFile);
byte[] bytes = new byte[fis.available()];
fis.read(bytes);
// 删除 word 临时文件
wordFile.delete();
fis.close();
pdfFile.delete();
// 设置下载的响应头信息
HttpHeaders hh = new HttpHeaders();
hh.setContentDispositionFormData("attachement", pdfFile.getName());
return new ResponseEntity<byte[]>(bytes, hh, HttpStatus.OK);
/* PDF 文件下载 */
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
前端页面wordtest.html:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/w2p/convert" method="post" enctype="multipart/form-data">
<input type="file" name="file"><input type="submit" value="转换"/>
</form>
[[${result}]]
</body>
</html>
页面展示如下:
点击转换之后:
转换完成。