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,并通过流的方式进行下载(前后端分离)

相关推荐
海兰7 分钟前
使用 Spring AI 打造企业级 RAG 知识库第二部分:AI 实战
java·人工智能·spring
历程里程碑25 分钟前
二叉树---二叉树的中序遍历
java·大数据·开发语言·elasticsearch·链表·搜索引擎·lua
阿丰资源36 分钟前
SpringBoot+MySQL+MyBatis-Plus+Vue前后端分离仓库管理系统 (附资料)
spring boot·mysql·mybatis
小信丶38 分钟前
Spring Cloud Stream EnableBinding注解详解:定义、应用场景与示例代码
java·spring boot·后端·spring
s1mple“”39 分钟前
互联网大厂Java面试实录:谢飞机的AIGC求职之旅 - JVM并发编程到Spring Cloud微服务
spring boot·aigc·微服务架构·java面试·分布式系统·rag技术·redis数据库
像素之间40 分钟前
为什么运行时要加set NODE_OPTIONS=--openssl-legacy-provider && vue-cli-service serve
前端·javascript·vue.js
无限进步_42 分钟前
【C++】验证回文字符串:高效算法详解与优化
java·开发语言·c++·git·算法·github·visual studio
M ? A42 分钟前
Vue转React实战:defineProps精准迁移实战
前端·javascript·vue.js·经验分享·react.js·开源·vureact
亚历克斯神43 分钟前
Spring Cloud 2026 架构演进
java·spring·微服务
七夜zippoe1 小时前
Spring Cloud与Dubbo架构哲学对决
java·spring cloud·架构·dubbo·配置中心