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

相关推荐
洋洋技术笔记6 分钟前
Spring Boot启动流程解析
spring boot·后端
Seven972 小时前
Condition底层机制剖析:多线程等待与通知机制
java
怒放吧德德11 小时前
Spring Boot 实战:RSA+AES 接口全链路加解密(防篡改 / 防重放)
java·spring boot·后端
郑州光合科技余经理14 小时前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
大大水瓶15 小时前
Tomcat
java·tomcat
dustcell.15 小时前
haproxy七层代理
java·开发语言·前端
游离态指针15 小时前
以为发消息=下单成功?RabbitMQ从0到秒杀实战的完整踩坑笔记
java
李慕婉学姐15 小时前
Springboot智慧社区系统设计与开发6n99s526(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库·spring boot·后端
Xin_z_16 小时前
Vue3 + Sticky 锚点跳转被遮挡问题解决方案
前端·javascript·vue.js
BD_Marathon16 小时前
工厂方法模式
android·java·工厂方法模式