一、使用aspose工具转换,需要用到的jar包 aspectjweaver-1.9.1.jar,aspose-words-14.9.0-jdk16.jar
可去https://download.csdn.net/download/u012758488/11038290下载
二、编写工具类Word2PdfUtil,用法在main函数里面
public class Word2PdfUtil {
public static void main(String[] args) {
doc2pdf("C:/Users/Administrator/Desktop/equipmentYSTemplete.doc",
"C:/Users/Administrator/Desktop/test.pdf");
}
public static void doc2pdf(String inPath, String outPath) {
FileOutputStream os = null;
try {
// 新建一个空白pdf文档
File file = new File(outPath);
os = new FileOutputStream(file);
Document doc = new Document(inPath);
doc.save(os, SaveFormat.PDF);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
三、需要注意的是此方法在windows服务器上没有问题,但放到linux服务器上中文可能会出现乱码,原因是linux服务器上没有安装windows上的字体,安装上即可:
(1)Windows下字体库的位置为C:\Windows\fonts,这里面包含所有windows下可用的字体。
(2)linux的字体库是 /usr/share/Fonts 。
在该目录下新建一个目录,比如目录名叫 windows(根据个人的喜好,自己理解就行,当然这里是有权限要求的,你可以用sudo来执行)。
然后将 windows 字体库中你要的字体文件复制到新建的目录下(只需要复制*.ttc,和*.ttf的文件).
复制所有字体:
sudo cp *.ttc /usr/share/fonts/windows/
sudo cp *.ttf /usr/share/fonts/windows/
更改这些字体库的权限:
sudo chmod 755 /usr/share/fonts/windows/*
然后进入linux字体库:
cd /usr/share/fonts/windows/
接着根据当前目录下的字体建立scale文件
sudo mkfontscale
接着建立dir文件
sudo mkfontdir
然后运行
sudo fc-cache
重启 Linux 操作系统就可以使用这些字体了。