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

相关推荐
千寻技术帮15 小时前
10361_基于Springboot的哈尔滨旅游管理系统
数据库·spring boot·mysql·毕业设计·旅游
步菲16 小时前
springboot canche 无法避免Null key错误, Null key returned for cache operation
java·开发语言·spring boot
毕设源码-朱学姐16 小时前
【开题答辩全过程】以 基于SpringBoot的中医理疗就诊系统为例,包含答辩的问题和答案
java·spring boot·后端
2201_7578308720 小时前
全局异常处理器
java
小徐Chao努力21 小时前
【Langchain4j-Java AI开发】09-Agent智能体工作流
java·开发语言·人工智能
Coder_Boy_21 小时前
SpringAI与LangChain4j的智能应用-(理论篇2)
人工智能·spring boot·langchain·springai
Coder_Boy_21 小时前
SpringAI与LangChain4j的智能应用-(理论篇3)
java·人工智能·spring boot·langchain
Coder_Boy_1 天前
基于SpringAI的智能平台基座开发-(六)
java·数据库·人工智能·spring·langchain·langchain4j
武昌库里写JAVA1 天前
iview-CRUD模板
vue.js·spring boot·sql·layui·课程设计
伯明翰java1 天前
Java数据类型与变量
java·开发语言