关于Java项目用aspose word转pdf在Linux环境乱码问题

jar包之前使用的是 aspose-words-18.6.0-jdk16.jar
这是pdf在Linux上显示的问题,都是方格,开始以为是乱码问题,后来查了一下,说是在Linux上没加载到中文字体,然后把用到的字体包从windows的C:\Windows\Fonts里找出来,复制到linux的/usr/share/fonts/windows-fonts下然后就是其他博客的那些,刷新字体# fc-cache。不生效重启服务器 # reboot。,刷新了一下服务器,感觉还是没气效果,于是,又开始查找,终于找到一个,说可能是没有读到字体,于是:
关于Java项目用aspose word转pdf在Linux环境乱码问题_第1张图片package com.shineyoo.common.utils.wordtopdf;

import com.aspose.words.Document;
import com.aspose.words.FontSettings;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;

public class Doc2PdfUtil {
private static Logger logger = LoggerFactory.getLogger(Doc2PdfUtil.class);

/**
 * doc转pdf
 *
 * @param docPath doc文件路径,包含.doc
 * @param pdfPath pdf文件路径,包含.pdf
 * @return
 */
public static File doc2Pdf(String docPath, String pdfPath) {
    System.out.println("pdfPath = "+pdfPath);
    System.out.println("pdfPath = "+pdfPath);
    File pdfFile = new File(pdfPath);
    //判断是否windows系统,Linux要读取字体,否则pdf字体为方格
    if(!OSinfo.isWindows()){
    //这是重点
        **FontSettings.getDefaultInstance().setFontsFolder(File.separator + "usr"
                + File.separator + "share" + File.separator + "fonts" +File.separator + "Fonts", true);**
    }
    try {
        String s = "Aspose.Total for JavaAspose.Words for JavaEnterprise20991231209912318bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=";
        ByteArrayInputStream is = new ByteArrayInputStream(s.getBytes());
        License license = new License();
        license.setLicense(is);
        Document document = new Document(docPath);
        FileOutputStream fileOutputStream = new FileOutputStream(pdfFile);
        System.out.println("pdf文件:"+fileOutputStream);
        document.save(fileOutputStream, SaveFormat.PDF);
    } catch (Exception e) {
        logger.info("****aspose doc转pdf异常");
        e.printStackTrace();
    }
    return pdfFile;
}

}

将FontSettings.getDefaultInstance().setFontsFolder(File.separator + “usr”
+ File.separator + “share” + File.separator + “fonts” +File.separator + “Fonts”, true);这段放到读取文件之前,就可以了,怎么样,很简单吧!

你可能感兴趣的:(word做预览功能,aspose,word转pdf,乱码)