Aspose对Word文档转PDF与PNG——章节1
部署Linux遭遇中文乱码——章节2
一. Aspose使用Maven管理及工具类
百度网盘分享
提取码:qy25
建议下载之后放桌面
1.jar包导入本地maven仓库
检查本地maven是否安装与配置系统环境变量:
C:\Users\w00434>mvn -v
Apache Maven 3.6.0 (97c98ec64a1fdfee7767ce5ffb20918da4f719f3; 2018-10-25T02:41:47+08:00)
Maven home: C:\Users\w00434\JavaSoft\apache-maven-3.6.0\bin\..
Java version: 1.8.0_281, vendor: Oracle Corporation, runtime: C:\Program Files\Java\jdk1.8.0_281\jre
Default locale: zh_CN, platform encoding: GBK
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"
若你也像以上配置了系统变量,则可以直接执行以下命令:
mvn install:install-file -DgroupId=com.iyanwu -DartifactId=aspose-word -Dversion=15.8.0 -Dpackaging=jar -Dfile= 本地asposejar包路径(我是C:\Users\w00434\Desktop\aspose-word-15.8.0.jar)
命令与pom.xml对应关系如下
2. 代码部分
将license2word.xml放于项目resource目录下
工具类代码
package com.cloudnine.personnel.utils;
import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import lombok.SneakyThrows;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
/**
* @author IyanWu
* @description 用于将word文档进行格式转换
* @date 2021/10/27 13:51
*/
public class AsposeWord2Pdf {
private static final Logger logger = LoggerFactory.getLogger(AsposeWord2Pdf.class);
/**
* 转换PDF方法
*
* @param filePath 原文件路径(包括文件名)
* @param toFilePah 目标文件路径
* @param toFileName 目标文件文件名
* @param cover 是否覆盖目标文件
* @return 转换文件大小
*/
public synchronized static String fileToPDF(String filePath, String toFilePah, String toFileName, boolean cover) {
long time = fileToPDFFun(filePath, toFilePah, toFileName, cover);
return String.valueOf(time);
}
/**
* 转换PDF方法
*
* @param filePath 原文件路径(包括文件名)
* @param toFilePah 目标文件路径(包括文件名)
* @param cover 是否覆盖目标文件
* @return 转换文件的大小
*/
@SneakyThrows
public static Long fileToPDFFun(String filePath, String toFilePah, String toFileName, boolean cover) {
File target = new File(filePath);
if (!target.isFile()) return (long) -7;
if (!target.exists()) return (long) -6;
target = new File(toFilePah);
if (!target.exists()) {
target.mkdirs();
}
target = new File(toFilePah + File.separator + toFileName);
if (target.exists()) {
if (cover) {
target.delete();
} else {
return (long) -5;
}
}
long convert2pdf = doc2pdf(Files.newInputStream(Paths.get(filePath)), toFilePah + File.separator + toFileName);
if (convert2pdf < 0) {
return convert2pdf;
} else {
return target.length();
}
}
/**
* word转pdf
*
* @param inputStream word文档的输入流(也可以为word文档保存路径)
* @param pdfPath 转换后pdf文件保存的路径
* @author IyanWu
* @date 2021/10/27 14:35
*/
@SneakyThrows
public static Long doc2pdf(InputStream inputStream, String pdfPath) {
long ret;
getLicense();
// 新建一个空白pdf文档
File file = new File(pdfPath);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
file.createNewFile();
long old = System.currentTimeMillis();
FileOutputStream os = new FileOutputStream(file);
Document doc = new Document(inputStream); // Address是将要被转化的word文档
DocumentBuilder documentBuilder = new DocumentBuilder();
documentBuilder.getFont().clearFormatting();
doc.save(os, SaveFormat.PNG);
doc.setTrackRevisions(true);
doc.acceptAllRevisions();
doc.save(os, SaveFormat.PDF);
ret = file.length();
long now = System.currentTimeMillis();
String append = "word转PDF," + "目标文件:" + pdfPath +
"共耗时:" + ((now - old) / 1000.0) + "秒";
logger.info(append);
return ret;
}
/**
* word转pdf后写入到响应输出流
*
* @param inputStream word文档的输入流(也可以为word文档保存路径)
* @author IyanWu
* @date 2021/10/27 14:35
*/
@SneakyThrows
public static void resopnseDoc2pdf(InputStream inputStream, OutputStream outputStream) {
//获取许可
getLicense();
long old = System.currentTimeMillis();
Document doc = new Document(inputStream);
DocumentBuilder documentBuilder = new DocumentBuilder();
documentBuilder.getFont().clearFormatting();
doc.setTrackRevisions(true);
doc.acceptAllRevisions();
doc.save(outputStream, SaveFormat.PDF);
long now = System.currentTimeMillis();
String append = "word转PDF," + "共耗时:" + ((now - old) / 1000.0) + "秒";
logger.info(append);
}
/**
* 加载license.xml,不生成水印
*
* @author IyanWu
* @date 2021/10/19 15:32
*/
@SneakyThrows
private static void getLicense() {
try (InputStream is = AsposeWord2Pdf.class.getClassLoader().getResourceAsStream("license2word.xml")) {
License license = new License();
license.setLicense(is);
}
}
}
使用示例
仅业务层示例代码
public void appendixPreview(Long id, HttpServletResponse httpServletResponse) {
if (Objects.isNull(id)) {
return;
}
ContractManageDO contractManageDO = super.get(id);
assert contractManageDO != null;
String remoteFileAddr = contractManageDO.getContractAppendix();
String name = contractManageDO.getName();
byte[] download;
OutputStream os = new ByteArrayOutputStream();
try {
os = httpServletResponse.getOutputStream();
download = FastDFSUtil.download(FileServiceConstant.DEFAULT_GROUP, remoteFileAddr);
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(download);
httpServletResponse.setHeader("Content-Disposition", "attachment;fileName=" + name + "合同.pdf");
AsposeWord2Pdf.resopnseDoc2pdf(byteArrayInputStream, os);
} catch (MyException | IOException e) {
throw new ApiException(String.format("预览%s合同失败!", name));
} finally {
IOUtils.closeQuietly(os);
}
}
二、解决Linux服务器中文乱码
1. 查看已安装的中文字体: fc-list :lang=zh
[root@cmdblog java-project]# fc-list :lang=zh
/usr/share/fonts/wqy-microhei/wqy-microhei.ttc: WenQuanYi Micro Hei,文泉驛微米黑:style=Regular
/usr/share/fonts/wqy-zenhei/wqy-zenhei.ttc: WenQuanYi Zen Hei:style=Regular
/usr/share/fonts/wqy-zenhei/wqy-zenhei.ttc: WenQuanYi Zen Hei Sharp,文泉驛點陣正黑:style=Regular
/usr/share/fonts/cjkuni-uming/uming.ttc: AR PL UMing TW MBE:style=Light
/usr/share/fonts/wqy-microhei/wqy-microhei.ttc: WenQuanYi Micro Hei Mono,文泉驛等寬微米黑:style=Regular
/usr/share/fonts/wqy-zenhei/wqy-zenhei.ttc: WenQuanYi Zen Hei Mono:style=Regular
/usr/share/fonts/cjkuni-uming/uming.ttc: AR PL UMing TW:style=Light
/usr/share/fonts/cjkuni-uming/uming.ttc: AR PL UMing HK:style=Light
/usr/share/fonts/cjkuni-uming/uming.ttc: AR PL UMing CN:style=Light
问题1:若没有fc命令,则安装字体命令:yum -y install fontconfig
2. 如果执行查看中文字体命令后也如图,那么需要在linux安装windows中文字体
在 /usr/share/fonts目录下面创建中文字体文件夹:mkdir chinese
windows中文字体目录:C:\Windows\Fonts
拷贝如下字体使用Xftp工具类上传到创建的中文字体目录(usr/share/fonts/chinese)
让中文字体生效
mkfontscale
mkfontdir
fc-cache -fv
如果mkfontscale无效,则yum安装一下命令
yum -y install mkfontscale
再检测中文字体是否已安装 fc-list :lang=zh
[root@cmdblog chinese]# fc-list :lang=zh
/usr/share/fonts/chinese/STXINGKA.TTF: STXingkai:style=Regular
/usr/share/fonts/chinese/MSYH.TTC: Microsoft YaHei:style=Normal
/usr/share/fonts/chinese/STXIHEI.TTF: STXihei:style=Regular
/usr/share/fonts/chinese/SIMSUN.TTC: 宋体,SimSun:style=常规,Regular
/usr/share/fonts/wqy-microhei/wqy-microhei.ttc: WenQuanYi Micro Hei,文泉驛微米黑:style=Regular
/usr/share/fonts/wqy-zenhei/wqy-zenhei.ttc: WenQuanYi Zen Hei:style=Regular
/usr/share/fonts/chinese/MSYHBD.TTC: Microsoft YaHei:style=Έντονα
/usr/share/fonts/chinese/STKAITI.TTF: STKaiti:style=Regular
/usr/share/fonts/chinese/DENGL.TTF: DengXian,DengXian Light:style=Light,Regular
/usr/share/fonts/wqy-zenhei/wqy-zenhei.ttc: WenQuanYi Zen Hei Sharp,文泉驛點陣正黑:style=Regular
/usr/share/fonts/chinese/DENG.TTF: DengXian:style=Regular
/usr/share/fonts/chinese/MSYH.TTC: Microsoft YaHei UI:style=Normal
/usr/share/fonts/chinese/FZSTK.TTF: FZShuTi:style=Regular
/usr/share/fonts/chinese/MSYHBD.TTC: Microsoft YaHei UI:style=Έντονα
/usr/share/fonts/chinese/STFANGSO.TTF: STFangsong:style=Regular
/usr/share/fonts/cjkuni-uming/uming.ttc: AR PL UMing TW MBE:style=Light
/usr/share/fonts/chinese/DENGB.TTF: DengXian:style=Bold
/usr/share/fonts/chinese/STCAIYUN.TTF: STCaiyun:style=Regular
/usr/share/fonts/chinese/SIMSUN.TTC: 新宋体,NSimSun:style=常规,Regular
/usr/share/fonts/chinese/MSYHL.TTC: Microsoft YaHei UI,Microsoft YaHei UI Light:style=Light,Regular
/usr/share/fonts/wqy-microhei/wqy-microhei.ttc: WenQuanYi Micro Hei Mono,文泉驛等寬微米黑:style=Regular
/usr/share/fonts/chinese/SIMKAI.TTF: KaiTi:style=Regular,Normaali
/usr/share/fonts/chinese/FZYTK.TTF: FZYaoTi:style=Regular
/usr/share/fonts/wqy-zenhei/wqy-zenhei.ttc: WenQuanYi Zen Hei Mono:style=Regular
/usr/share/fonts/chinese/STSONG.TTF: STSong:style=Regular
/usr/share/fonts/chinese/STHUPO.TTF: STHupo:style=Regular
/usr/share/fonts/chinese/SIMLI.TTF: LiSu:style=Regular
/usr/share/fonts/chinese/STXINWEI.TTF: STXinwei:style=Regular
/usr/share/fonts/chinese/STLITI.TTF: STLiti:style=Regular
/usr/share/fonts/win/simhei.ttf: SimHei:style=Normal
/usr/share/fonts/chinese/STZHONGS.TTF: STZhongsong:style=Regular
/usr/share/fonts/cjkuni-uming/uming.ttc: AR PL UMing TW:style=Light
/usr/share/fonts/cjkuni-uming/uming.ttc: AR PL UMing HK:style=Light
/usr/share/fonts/cjkuni-uming/uming.ttc: AR PL UMing CN:style=Light
/usr/share/fonts/chinese/SIMHEI.TTF: SimHei:style=Normal
/usr/share/fonts/chinese/SIMYOU.TTF: YouYuan:style=Regular
/usr/share/fonts/chinese/SIMFANG.TTF: FangSong:style=Regular,Normaali
/usr/share/fonts/chinese/MSYHL.TTC: Microsoft YaHei,Microsoft YaHei Light:style=Light,Regular
到这儿已完成大部分工作,小坑是需要重启服务器!
shutdown -r now