• 【技术】Spring Boot 将 Word 转换为 PDF 2.0 版本


    之前写过一篇 Spring Boot 将 Word 转换为 PDF 的文章,但是有评论说导入依赖有问题,还存在依赖冲突的问题。索性再来一个完整版的代码,之前的完整版代码找不到了,又重新整理了一下,依赖导入和之前不太一样,代码写法类似。

    1、导入依赖
    核心依赖:

    
    <dependency>
        <groupId>org.apache.poigroupId>
        <artifactId>poi-ooxmlartifactId>
        <version>3.15version>
    dependency>
    
    
    <dependency>
        <groupId>fr.opensagres.xdocreportgroupId>
        <artifactId>org.apache.poi.xwpf.converter.pdfartifactId>
        <version>1.0.6version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    完整 pom.xml 文件

    
    <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.0modelVersion>
        <parent>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-parentartifactId>
            <version>2.7.18version>
            <relativePath/> 
        parent>
        <groupId>com.riugroupId>
        <artifactId>spring-boot-demoartifactId>
        <version>0.0.1-SNAPSHOTversion>
        <name>spring-boot-demoname>
        <description>spring-boot-demodescription>
        <properties>
            <java.version>8java.version>
        properties>
        <dependencies>
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
    
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-thymeleafartifactId>
            dependency>
    
            
            <dependency>
                <groupId>org.apache.poigroupId>
                <artifactId>poi-ooxmlartifactId>
                <version>3.15version>
            dependency>
    
            
            <dependency>
                <groupId>fr.opensagres.xdocreportgroupId>
                <artifactId>org.apache.poi.xwpf.converter.pdfartifactId>
                <version>1.0.6version>
            dependency>
        dependencies>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-maven-pluginartifactId>
                plugin>
            plugins>
        build>
    project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54

    2、用于上传文件的页面

    DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Titletitle>
    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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    3、控制层代码

    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;
    
    @Controller
    @RequestMapping("/w2p")
    public class FileConversionController {
    
        /**
         * 跳转 w2p 页面,提交文件
         * @return
         */
        @GetMapping
        public String w2p(){
            return "/w2p/w2p";
        }
    
        /**
         * 文件转换: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;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73

    4、PDF 转换类

    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();
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
  • 相关阅读:
    适合tiktok运营的云手机需要满足什么条件?
    数据库SQL优化总结
    webpack类似的工具还有哪些?
    QPE更换Logo和相关信息
    Rust 流程控制和循环控制
    vue2中怎么使用css样式实现给元素穿衣的功能
    146. LRU 缓存
    ZooKeeper 核心知识总结
    力扣--动态规划1027.最长等差数列
    ISIS与OSPF区别
  • 原文地址:https://blog.csdn.net/m0_43453853/article/details/138039518