linux 下配置 openOffice ,jodconverter,解決字體亂碼

環境:linux

 這裏使用的docker 的contos7 系統。由於缺少很多依賴包,所以下面安裝過程會解決一些問題,如果你沒有碰到,就不用執行。

安裝包下載:

鏈接:https://pan.baidu.com/s/1YbGPBFZyarJ8A4Qr9ioOeQ 
提取碼:ysr8 

內有java8,jodconverter2.2 , openOffice4.1.6

0.準備環境

這裏由於使用了docker,拉完鏡像後,再使用Dockerfile 生成一個鏡像默認支持UTF-8,Dockerfile:

ROM centos:7.2.1511
ENV LANG en_US.utf8

不安裝中文包了,中文包安裝中有問題,反正是utf-8就行. 

1.安裝java

配置環境變量

export JAVA_HOME=/root/jdk1.8.0_201
export PATH=$JAVA_HOME/bin:$PATH 
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar 

2.安裝 openoffice

解壓之後會在 /opt/softwares 中生成 zh-CN 文件夾,進入RPMS文件夾中,並執行安裝。

cd /opt/softwares/zh-CN/RPMS
yum localinstall *.rpm

成功之後會在當前目錄生成 desktop-integration 文件夾,進入文件夾並執行安裝:

yum localinstall openoffice4.1.6-redhat-menus-4.1.6-9790.noarch.rpm

安裝成功會在 /opt 目錄下生成  openoffice4 文件夾。

嘗試運行:

nohup /opt/openoffice4/program/soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard &

報錯1  /opt/openoffice4/program/soffice.bin: error while loading shared libraries: libXext.so.6: cannot open shared object file: No such file or directory

安裝並拷貝文件

yum install libXext.x86_64
cp -a /usr/lib64/libXext.so.6 /opt/openoffice4/program/

報錯2  /opt/openoffice4/program/soffice.bin: error while loading shared libraries: libfreetype.so.6: cannot open shared object file: No such file or directory
 

 

yum install freetype
  
cp -a /usr/lib64/libfreetype.so.6 /opt/openoffice4/program/

報錯3 no suitable windowing system found, exiting

yum groupinstall "X Window System"

再次運行

nohup /opt/openoffice4/program/soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard &

成功。

解決 生成的文件亂碼:

把windows 上的字體 C:\Windows\Fonts 需要的字體上傳到 linux 上
  
  在centos的/usr/java/jdk1.8.0_91/jre/lib/fonts下新建路徑:fallback。
  將字體:simhei.ttf 黑體、simsun.ttc 宋體 等文件 上傳至/usr/java/jdk1.8.0_91/jre/lib/fonts/fallback路徑下。  

複製文件:
 

cp /root/jdk1.8.0_201/jre/lib/fonts/fallback/* /usr/share/fonts/


  
  # 更新字體
 

fc-cache   


  
  啓動服務,運行:
  
nohup /opt/openoffice4/program/soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard &
編寫代碼 調用服務。

4 把jar包放工程中,並編寫demo:

工程下載(包含jar):

鏈接:https://pan.baidu.com/s/1AAzl4ricIT7KYCVhu2yDJw 
提取碼:oaja 
 

代碼:

// 將word格式的文件轉換爲pdf格式
    public static void Word2Pdf(String srcPath, String desPath) throws IOException {
        // 源文件目錄
        File inputFile = new File(srcPath);
        if (!inputFile.exists()) {
            System.out.println("源文件不存在!");
            return;
        }
        // 輸出文件目錄
        File outputFile = new File(desPath);
        if (!outputFile.getParentFile().exists()) {
            outputFile.getParentFile().exists();
        }
        System.out.println(outputFile);
//        // window 使用  調用openoffice服務線程     本機C盤!!!
//        String command = "C:\\Program Files (x86)\\OpenOffice 4\\program\\soffice -headless -accept=\"socket,host=127.0.0.1,port=8100;urp; -nofirststartwizard\"";
//        //Linux使用 本地環境已經啓動就不需要這個了
//        //   String command = "/opt/openoffice4/program/soffice -headless -accept=\"socket,host=127.0.0.1,port=8100;urp; -nofirststartwizard\"";
//        Process p = Runtime.getRuntime().exec(command);

        // 連接openoffice服務
        OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);
//        OpenOfficeConnection connection = new SocketOpenOfficeConnection("119.80.41.140", 8100);
        connection.connect();

        // 轉換word到pdf
        DocumentConverter converter = new OpenOfficeDocumentConverter(
                connection);
        converter.convert(inputFile, outputFile);

        // 關閉連接
        connection.disconnect();

        // 關閉進程
//        p.destroy();

        System.out.println("轉換完成!");
    }
    public static void main(String[] args) throws IOException {
        if(args.length == 2){
            String srcPath = args[0];
            String desPath = args[1];
            Demo.Word2Pdf(srcPath, desPath);
        }else{
            System.out.println("usage: srcpath desPath");
        }

    }



參考:

1.https://blog.csdn.net/Lucky_boy_gilr/article/details/52996198 

2. https://blog.csdn.net/mufeng633/article/details/83343191
3.https://blog.csdn.net/laoyang360/article/details/73555598/

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