高端品牌型网站建设顶尖网站设计公司
web/
2025/9/27 3:24:39/
文章来源:
高端品牌型网站建设,顶尖网站设计公司,滁州市建设工程协会网站,食药监局网站建设方案java springboot word文档转pdf 1、环境2、依赖3、代码 1、环境
1、java、springboot 2、maven或者gradle 3、办公软件#xff08;自己电脑上的wps或者office等#xff0c;如果部署到服务器上也要安装#xff0c;linux、Mac 都有#xff0c;自己安装#xff09; 可能会遇… java springboot word文档转pdf 1、环境2、依赖3、代码 1、环境
1、java、springboot 2、maven或者gradle 3、办公软件自己电脑上的wps或者office等如果部署到服务器上也要安装linux、Mac 都有自己安装 可能会遇到的问题 java springboot maven项目当然gradle 也可以特别注意的是这个需要依赖外部软件就是我们熟知的wps或者office 都可以应该说是有办公软件且办公软件有word转pdf的功能这个它应该是获取到我们电脑本地的环境来转的如果有时候不行的话可以考虑一下是不是自己的办公软件的问题我之前就遇到这个问题我本地已经安装wps的也可以正常使用但是代码里面就是不行一直报流已经关闭意思就是获取不到我本地相关的流。最后我重新安装一下我的办公软件wps就好了还有一种就可能版本的问题也一定要注意
2、依赖
dependencygroupIdcom.documents4j/groupIdartifactIddocuments4j-local/artifactIdversion1.0.3/version/dependencydependencygroupIdcom.documents4j/groupIdartifactIddocuments4j-transformer-msoffice-word/artifactIdversion1.0.3/version/dependency这是maven的依赖其他的自己找
3、代码
package org.jeecg.modules.xunshu.utils;import com.documents4j.api.DocumentType;
import com.documents4j.api.IConverter;
import com.documents4j.job.LocalConverter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;import java.io.*;/*** author weisn*/
Slf4j
Component
public class WordConvertPdfUtil {private static final IConverter CONVERTER LocalConverter.builder().build();/*** 通过documents4j 实现word转pdf** param sourcePath 源文件地址 如 /root/example.doc* param targetPath 目标文件地址 如 /root/example.pdf*/public static void documents4jWordToPdf(String sourcePath, String targetPath) throws IOException {InputStream docxInputStream null;OutputStream outputStream null;try {// 原word地址docxInputStream new FileInputStream(sourcePath);// 转换后pdf生成地址outputStream new FileOutputStream(targetPath);IConverter converter LocalConverter.builder().build();converter.convert(docxInputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute();// 关闭converter.shutDown();// 关闭outputStream.close();// 关闭docxInputStream.close();} catch (Exception e) {System.out.println([documents4J] word转pdf失败: e.toString());} finally {if (outputStream ! null) {outputStream.close();}if (docxInputStream ! null) {docxInputStream.close();}}}}
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/82514.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!