/**
* @Description Word转PDF
* @param inPath 源文件(Word)
* @param outPath 目标文件(PDF)
* @param watermark 水印字符(为空则不加)
* @author han
* @date 2021年3月26日 上午10:46:00
*/
public synchronized static void doc2pdf(String inPath, String outPath,String watermark) throws Exception {
if (!getLicense()) {//验证去除头部的水印
return;
}
FileOutputStream os =null;
try {
File file = new File(outPath); // 新建一个空白pdf文档
log.info("getPathgetPathgetPath:::::::::::::" + file.getPath());
os = new FileOutputStream(file);
//注意Linux环境需要这一行,不然乱码 ?
FontSettings.setFontsFolder("/usr/share/fonts",true);
Document doc = new Document(inPath); // Address是将要被转化的word文档
if(StringUtils.isNotEmpty(watermark)) {
insertWatermarkText(doc,watermark);
}
log.info("getPathgetPathgetPath2222:::::::::::::" + file.getPath());
doc.save(os, SaveFormat.PDF);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,// EPUB, XPS, SWF 相互转换
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("Word转PDF出错:"+e.getMessage());
}finally {
os.close();
}
}
//验证License 若不验证则转化出的pdf文档会有水印产生
public static boolean getLicense() throws Exception {
boolean result = false;
try {
// InputStream is = Test.class.getClassLoader().getResourceAsStream("license.xml"); // license.xml应放在..\WebRoot\WEB-INF\classes路径下
ClassPathResource classPathResource = new ClassPathResource("license.xml");
InputStream is =classPathResource.getInputStream();
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("验证License出错:"+e.getMessage());
}
return result;
}
特别注意:FontSettings.setFontsFolder("/usr/share/fonts",true);
1获取权限
sudo chmod -R 755 /usr/share/fonts
2.将整个目录C:\Windows\Fonts\上传到linux下的/usr/shared/fonts/2.
文件夹全部上传到/home/monitor/Fonts然后复制到目录
sudo cp -r /home/monitor/Fonts/. /usr/share/fonts
3.安装ttmkfdir
sudo yum -y install ttmkfdir
4.执行ttmkfdir命令
ttmkfdir -e /usr/share/X11/fonts/encodings/encoding.dir
5刷新内存中的字体缓存.
fc-cache
6.查看fc-list :lang=zh
不需要重启。