springboot+vue项目实现Word转PDF

依赖的话就自求多福吧,因为我这个代码也写了很久了,具体我也不太清楚用了哪些依赖(嘤嘤嘤)~

接下来直接看核心代码吧:

java 复制代码
public R wordToPdf(MultipartFile file, HttpServletRequest request) throws IOException, DocumentException {

        // 获取文件名,不带扩展名
        String filename = file.getOriginalFilename().substring(0, file.getOriginalFilename().lastIndexOf("."));
        try {
            // 获取文件输入流
            InputStream docxInputStream = file.getInputStream();

            // 构建输出流,保存PDF文件到E盘下
            String outputPath = "E:/pictureTool/"+filename+"_converted.pdf";
            FilePath=outputPath;
            FileName=filename+"_converted.pdf";
            FileOutputStream outputStream = new FileOutputStream(outputPath);

            // 构建转换器,将docx转换为PDF
            IConverter converter = LocalConverter.builder().build();
            converter.convert(docxInputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute();

            // 添加用户操作记录
            boolean success = addUserOperation(file, "pdf", 1, request);
            System.out.println("Word转PDF成功,操作结果为:" + success);

            return R.Success("Word转PDF成功"+outputPath);
        } catch (Exception e) {
            e.printStackTrace();
            // 添加用户操作记录
            boolean success = addUserOperation(file, "pdf", 0, request);
            System.out.println("Word转PDF失败,操作结果为:" + success);

            return R.Failed("Word转PDF失败");
        }
    }

代码中均做有注释,可以实现word转pdf,若要看具体的其它配置,如静态资源的放行以及yml的配置,请移步到这篇博文(主要是真的不想再写一次了~(我知道,我很懒~))

java实现图片转pdf,并通过流的方式进行下载(前后端分离)

相关推荐
me8321 分钟前
【Java】解决Maven多模块父POM加载失败+IDEA无法新建Java类问题
java·maven·intellij-idea
亚马逊云开发者10 分钟前
RAG 向量存储月费 800 刀?S3 Vectors 直接砍到 100 出头
java
2401_8955213420 分钟前
springboot集成onlyoffice(部署+开发)
java·spring boot·后端
zlpzlpzyd21 分钟前
groovy学习
java·jvm·学习
程序员小假30 分钟前
你分得清 Prompt、Agent、Function Call、Skill、MCP 吗?
java·后端
xuboyok233 分钟前
【Spring Boot】统一数据返回
java·spring boot·后端
亚马逊云开发者36 分钟前
你的 AI Agent 只有鱼的记忆?聊聊 Agent 记忆管理的正确姿势
java
吃西瓜的年年1 小时前
前端面试题(vue)
前端·javascript·vue.js
wuhen_n1 小时前
结构化Prompt——让AI说“人话”
前端·vue.js·ai编程
燕山罗成1 小时前
JAVA多线程基础
java·开发语言