顯示具有 Libre Office 標籤的文章。 顯示所有文章
顯示具有 Libre Office 標籤的文章。 顯示所有文章

2012年4月24日 星期二

使用 JODConverter 將 Microsoft Office 文件轉換成 PDF

使用版本:
JODConverter v2.2.2,
LibreOffice v3.5
JODConverter 需要在本機電腦中安裝 LibreOffice (OpenOffice 也可),本處以 LibreOffice 為範例


可轉換類型:
*.doc,
*.ppt,
*.xls,
*.docx,
*.pptx,
*.xlsx


使用教學:

Step1: 安裝 LibreOffice

Step2: 啟動 LibreOffice Service
   1. cd C:\Program Files\LibreOffice 3.5\program
   2. soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard

Step3: 將 JODConverter library 裡的 jar 檔放進專案

完成以上動作後,即可開始測試下面範例程式

使用方式,可傳入檔案完整路徑,或檔案的 File 物件,如

OfficePDFUtil officePDFUtil = new OfficePDFUtil();
officePDFUtil.convert("test.doc""doc.pdf");


<<範例程式>>

import java.io.File;
import java.net.ConnectException;

import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;


public class OfficePDFUtil {

    /**
     * Convert office document to PDF
     *
     * @param inputFile - File path of original office document (*.doc, *.xls, *.ppt)
     * @param outputFile - File path of target pdf document
     * @throws ConnectException
     */
    public void convert(String inputFilePath, String outputFilePath) throws Exception {
        File inputFile = new File(inputFilePath);
        File outputFile = new File(outputFilePath);
        this.convert(inputFile, outputFile);
    }
   
    /**
     * Convert office document to PDF
     *
     * @param inputFile - File of original office document (*.doc, *.xls, *.ppt)
     * @param outputFile - File of target pdf document
     * @throws ConnectException
     */
    public void convert(File inputFile, File outputFile) throws Exception {
      //connect to an OpenOffice.org instance running on port 8100
        OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);

        try {
            connection.connect();

            // convert
            DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
            converter.convert(inputFile, outputFile);
        } catch (Exception e) {
            throw e;
        } finally {
            //close the connection
            try {
                if (connection != null) {
                    connection.disconnect();
                    connection = null;
                }
            } catch (Exception e) {
                throw e;
            }
        }
    }
}

2012年4月20日 星期五

RedHat Linux 上安裝 RPM 版本 LibreOffice 3.5


1. 解壓縮
tar zxvf [安裝檔名]
例如:
tar zxvf LibO_3.5.2_Linux_x86-64_install-rpm_en-US.tar.gz

2. 解壓縮的目錄會位於壓縮檔的相同路徑下
例如:
LibO_3.5.2_Linux_x86-64_install-rpm_en-US.tar.gz ← /user/home
LibO_3.5.2rc2_Linux_x86-64_install-rpm_en-US ← 也會在 /user/home

進入解完壓縮後的目錄
cd LibO_3.5.2rc2_Linux_x86-64_install-rpm_en-US/RPMS

並執行下列命令 (必須為 root 帳號)
rpm -Uvh *.rpm

3. 安裝完畢後的路徑
/opt/libreoffice3.5/program


4. 啟動 Service 指令(背景執行)
/usr/bin/nohup /opt/libreoffice3.5/program/soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard 0</dev/null 1>/dev/null 2>/dev/null &



5. shell 檔案如下 (在 server 上啟動 Libre Office 的服務)

<startup_soffice.sh>
#!/bin/bash
# Update source files from Subversion and build them
#
# History
# 2012.04.20 GUC-Kaohung
# create this file

LibreOffice_HOME=/opt/libreoffice3.5/program/soffice.bin

# find tomcat PID
tomcatpid=`ps -ef | grep "$LibreOffice_HOME" | grep -v grep | awk '{print $2}'`
if [ -n "$tomcatpid" ]; then
kill -9 $tomcatpid
echo "LibreOffice3.5 service is stopped."
fi

/usr/bin/nohup /opt/libreoffice3.5/program/soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard 0</dev/null 1>/dev/null 2>/dev/null &
echo "LibreOffice3.5 service is started."

<shutdown_soffice.sh>
#!/bin/bash
# Update source files from Subversion and build them
#
# History
# 2012.04.20 GUC-Kaohung
# create this file

LibreOffice_HOME=/opt/libreoffice3.5/program/soffice.bin

# find tomcat PID
tomcatpid=`ps -ef | grep "$LibreOffice_HOME" | grep -v grep | awk '{print $2}'`
if [ -n "$tomcatpid" ]; then
kill -9 $tomcatpid
echo "LibreOffice3.5 service is stopped."
fi