LoginSignup
1
3

More than 3 years have passed since last update.

OpenOfficeでドキュメント変換

Posted at

はじめに

ドキュメントを別のフォーマットのドキュメントに変換したい場合があります。
例えば、ExcelをPDFに変換したい。
PCの場合は、OfficeのExcelでは簡単にほかのフォーマットを保存するだけで変換されます。
image.png

ユーザー操作ではなく、プログラムで自動変換したい場合は、Openofficeを利用することで実現できます。

OpenOfficeインストール

image.png

コマンド:

sudo su -

wget https://sourceforge.net/projects/openofficeorg.mirror/files/4.1.6/binaries/ja/Apache_OpenOffice_4.1.6_Linux_x86-64_install-rpm_ja.tar.gz

tar -xvzf Apache_OpenOffice_4.1.6_Linux_x86-64_install-rpm_ja.tar.gz

cd ja/RPMS

rpm -Uvih *rpm

# インストール確認
ll /opt/openoffice4

# シンボルリンクを作成する
ln -s /opt/openoffice4/program/soffice /usr/local/bin/soffice

# 日本語フォントインストール
sudo yum install ipa-gothic-fonts ipa-pgothic-fonts
sudo fc-cache -fv

OpenOffice起動

# en_US.UTF-8だと日本語文字化けなので、日本語を設定
export LANG=ja_JP.UTF-8

# 起動
nohup soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard &

# 起動確認
ps aux | grep soffice

コマンドで変換

testfile.txtをPDFファイルに変換するサンプルです。
soffice --headless --convert-to pdf testfile.txt

プログラムで変換

ライブラリ

jodconverterというライブラリで変換
jodconverter:https://github.com/sbraconnier/jodconverter/

ライブラリ導入

// https://mvnrepository.com/artifact/org.jodconverter/jodconverter-local
compile group: 'org.jodconverter', name: 'jodconverter-local', version: '4.2.2'

変換サンプル

DocConverter.java
import java.io.File;

import org.jodconverter.DocumentConverter;
import org.jodconverter.LocalConverter;
import org.jodconverter.office.LocalOfficeManager;
import org.jodconverter.office.OfficeException;
import org.jodconverter.office.OfficeManager;

public class DocConverter {

    public static void main(String[] args) throws OfficeException {

        OfficeManager officeManager = LocalOfficeManager.make();
        DocumentConverter converter = LocalConverter.make(officeManager);
        try {
            officeManager.start();

            File inputFile = new File("/data/test.xlsx");
            File outputFile = new File("/data/test.pdf");
            // Convert...
            converter.convert(inputFile).to(outputFile).execute();
        } finally {
            officeManager.stop();
        }
    }
}

Liferayは設定だけでドキュメントの変換可能

コントロールパネル⇒設定⇒サーバ管理⇒外部サービス
image.png

URL:https://www.openoffice.org/

以上

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3