在word转pdf时,在Windows上不乱码,在Linux上乱码,因此需在Linux上设置Windows字体。
import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
public class Doc2PdfUtil {
* 获取许可码
* @return
public static boolean getLicense() {
try {
InputStream is = Doc2PdfUtil.class.getClassLoader().getResourceAsStream("license.xml");
License aposeLic = new License();
aposeLic.setLicense(is);
return Boolean.TRUE;
} catch (Exception e) {
e.printStackTrace();
return Boolean.FALSE;
* doc转pdf
* @param sourcePath
* @param targetPath
public static void doc2pdf(String sourcePath, String targetPath) {
getLicense();
try {
File file = new File(targetPath);
FileOutputStream os = new FileOutputStream(file);
Document doc = new Document(sourcePath);
doc.save(os, SaveFormat.PDF);
} catch (Exception e) {
throw new RuntimeException("Word转PDF错误。", e);
在Linux上设置Windows字体
将Windows C:\Windows\Fonts路径下的字体(后缀为ttc,ttf)拷贝到Linux路径下的/usr/share/fonts/windows(若无此路径则建立)
修改字体文件权限
cd /usr/share/fonts/windows
chmod 755 *.ttf
chmod 755 *.ttc
建立字体缓存
mkfontscale (如果提示 mkfontscale: command not found,需自行安装 # yum install mkfontscale )
mkfontdir
fc-cache -fv (如果提示 fc-cache: command not found,则需要安装# yum install fontconfig )
source /etc/profile
IT_wtz
12.7k
粉丝