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

相关推荐
爱健身的小刘同学29 分钟前
Vue 3 + Leaflet 地图可视化
前端·javascript·vue.js
hashiqimiya39 分钟前
springboot事务触发滚动与不滚蛋
java·spring boot·后端
因我你好久不见42 分钟前
Windows部署springboot jar支持开机自启动
windows·spring boot·jar
PPPHUANG1 小时前
一次 CompletableFuture 误用,如何耗尽 IO 线程池并拖垮整个系统
java·后端·代码规范
恩创软件开发1 小时前
创业日常2026-1-8
java·经验分享·微信小程序·小程序
musashi2 小时前
用 Electron 写了一个 macOS 版本的 wallpaper(附源码、下载地址)
前端·vue.js·electron
徐徐子2 小时前
从vue3 watch开始理解Vue的响应式原理
前端·vue.js
无关86882 小时前
SpringBootApplication注解大解密
spring boot
想用offer打牌2 小时前
一站式了解Spring AI Alibaba的流式输出
java·人工智能·后端