1. 最近做了个小需求,
word转pdf,随便找了个类库转了一下,结果win正常,linux乱码了,看了一些文章,说是字体问题,这里记录一下
2. 需要依赖
<dependency>
<groupId>com.aspose.words</groupId>
<artifactId>aspose-words</artifactId>
<version>19.5jdk</version>
</dependency>
<dependency>
<groupId>com.aspose.words</groupId>
<artifactId>aspose-cells</artifactId>
<version>8.5.2</version>
</dependency>
3. 转换工具
import com.aspose.cells.License;
import com.aspose.words.Document;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
/**
* word文档 转换 PDF
*/
public class WordToPdf {
/**
* 获取license许可凭证
*
* @return
*/
private static boolean getLicense() {
boolean result = false;
try {
String licenseStr = "<License>\n"
+ " <Data>\n"
+ " <Products>\n"
+ " <Product>Aspose.Total for Java</Product>\n"
+ " <Product>Aspose.Words for Java</Product>\n"
+ " </Products>\n"
+ " <EditionType>Enterprise</EditionType>\n"
+ " <SubscriptionExpiry>20991231</SubscriptionExpiry>\n"
+ " <LicenseExpiry>20991231</LicenseExpiry>\n"
+ " <SerialNumber>23dcc79f-44ec-4a23-be3a-03c1632404e9</SerialNumber>\n"
+ " </Data>\n"
+ " <Signature>0nRuwNEddXwLfXB7pw66G71MS93gW8mNzJ7vuh3Sf4VAEOBfpxtHLCotymv1PoeukxYe31K441Ivq0Pkvx1yZZG4O1KCv3Omdbs7uqzUB4xXHlOub4VsTODzDJ5MWHqlRCB1HHcGjlyT2sVGiovLt0Grvqw5+QXBuinoBY0suX0=</Signature>\n"
+ "</License>";
InputStream license = new ByteArrayInputStream(licenseStr.getBytes("UTF-8"));
License asposeLic = new License();
asposeLic.setLicense(license);
System.out.println("zxu");
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* word文档 转换为 PDF
*
* @param inPath 源文件
* @param outPath 目标文件
*/
public static void doc2pdf(String inPath, String outPath) {
//验证License,获取许可凭证
if (!getLicense()) {
return;
}
try {
//新建一个PDF文档
File file = new File(outPath);
//新建一个IO输出流
FileOutputStream os = new FileOutputStream(file);
//获取将要被转化的word文档
Document doc = new Document(inPath);
// 全面支持DOC, DOCX,OOXML, RTF HTML,OpenDocument,PDF, EPUB, XPS,SWF 相互转换
doc.save(os, com.aspose.words.SaveFormat.PDF);
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
doc2pdf("D:\\说明.docx", "D:\\说明.pdf");
}
}
4. 乱码原因和解决方案
- From Windows
Windows下字体库的位置为C:\Windows\fonts,这里面包含所有windows下可用的字体。复制粘贴出来
- To Linux
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/
# 使mkfontscale和mkfontdir命令正常运行
yum install ttf-mscorefonts-installer
# 使fc-cache命令正常运行
yum install fontconfig
#安装字库。
#将win机器的C:\Windows\Fonts目录下的全部文件拷贝到生产服务器字体安装目录下
#然后执行以下命令更新字体缓存
sudo mkfontscale
sudo mkfontdir
sudo fc-cache -fv
#执行命令让字体生效
source /etc/profile
#如果安装失败可以考虑修改权限
chmod 755 *.ttf
5. 参考:
·1. https://blog.csdn.net/hanchuang213/article/details/64905214
2. https://blog.csdn.net/qq_26975307/article/details/84306156