word轉pdf 亂碼 aspose

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. 亂碼原因和解決方案

  1. From Windows

Windows下字體庫的位置爲C:\Windows\fonts,這裏麪包含所有windows下可用的字體。複製粘貼出來

  1. 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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章