网站后台导航随意添加,wordpress首页优化缩略图,网站名称注册,数据型网站建设文章目录 概要整体介绍具体实现官网pom文件增加依赖 遇到的问题本地运行OK#xff0c;发布到Linux报错还是本地OK#xff0c;但是Linux能运行的#xff0c;但是中文乱码 小结 概要
Springboot word 转 pdf
整体介绍
搜了一下#xff0c;发现了能实现功能的方法有四种
U… 文章目录 概要整体介绍具体实现官网pom文件增加依赖 遇到的问题本地运行OK发布到Linux报错还是本地OK但是Linux能运行的但是中文乱码 小结 概要
Springboot word 转 pdf
整体介绍
搜了一下发现了能实现功能的方法有四种
Using itext and opensagres and apache poiUsing Documents4jUsing openoffice nuoilUsing Aspose Word(not free)
具体实现
这里只提供Aspose的实现因为这个不区分windows还是Linux操作系统。因为试过了Doc4j需要依赖是Windows的所以暂时不去研究了
官网
aspose官网
由于是收费的所以这里的就按照官网的来百度一搜随便都是jar包。我这里不贴了毕竟不是免费的可以自行搜索破解版。 不废话了上代码 代码很简单的这里写一个直接写文件的例子
创建一个springboot项目然后把下载下来的jar包放到resource的lib下 然后右键这个jar包然后点击Add as library点击完了才是截图这样的否则就只是一个jar包点击了Add as library目的是为了让项目引入这个jar包。
pom文件增加依赖
dependencygroupIdcom.aspose.words/groupIdartifactIdaspose-words/artifactIdversion19.3/versionscopesystem/scopesystemPath${project.basedir}/src/main/resources/lib/aspose-words-19.3.jar/systemPath
/dependencypackage com.word2pdf.springbootword2pdf.controller;import com.aspose.words.Document;
import com.aspose.words.ParagraphFormat;
import com.aspose.words.SaveFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;/*** word2pdf** author Rex* since 2024/4/12 11:34*/
RestController
RequestMapping(/pangolin/test)
public class TestController {GetMapping(/word2pdf)public String test() throws Exception {InputStream inputStream Thread.currentThread().getContextClassLoader().getResourceAsStream(word/response.docx);Document doc new Document(inputStream);ParagraphFormat pfdoc.getStyles().getDefaultParagraphFormat();pf.clearFormatting();File tempPdfFile new File(./temp.pdf);FileOutputStream os new FileOutputStream(tempPdfFile);doc.save(os, SaveFormat.PDF);return 1;}}遇到的问题
本地运行OK发布到Linux报错
第一个坎儿报错没有找到类
java.lang.ClassNotFoundException: com.aspose.words.Document这个是因为不是走的maven自己放在项目的resources的lib下本地运行可以的但是打包需要在pom里增加配置
buildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactIdconfiguration!--打包的时候包含资源文件夹下自己的jar包--includeSystemScopetrue/includeSystemScopeexcludesexcludegroupIdorg.projectlombok/groupIdartifactIdlombok/artifactId/exclude/excludes/configuration/plugin/plugins
/build这个为了打包的时候能给这个jar包包含进来。
还是本地OK但是Linux能运行的但是中文乱码
好一个中文乱码这个问题是因为毕竟doc是基于windows的所以这里是少字体包。 解决方式是简单来说就是把windows的字体包拷贝到Linux中然后安装即可
# 首先windows 字体库的路径 : C:\Windows\Fonts
# 在/usr/share/fonts/下新建文件夹 winFonts
cd /usr/share/fonts/winFonts
sudo mkfontscale
sudo mkfontdir //这两条命令是生成字体的索引信息
sudo fc-cache -fv //更新字体缓存到此问题解决还是需要有耐心分析问题解决问题当然了也离不开运维同事的配合。
小结
遇到问题了先不要慌冷静分析相信自己能行。
附例子代码