LoginSignup
6
2

More than 5 years have passed since last update.

JavaからDockerを起動してOfficeドキュメントをPDF変換する

Last updated at Posted at 2017-12-15

1. はじめに

  • 動作環境はCentOS7上のdockerを対象とする。 ※ Windows上のdockerではうまく動作しないことも確認済み
  • javaのバージョンは1.8で確認済み

2. Dockerのインストール

CentOS上にDockerをインストールします。
詳細は公式のページ参照
https://docs.docker.com/engine/installation/linux/docker-ce/centos/

yum install -y yum-utils \
  device-mapper-persistent-data \
  lvm2
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum makecache fast
yum install docker-ce
systemctl enable docker
systemctl start docker

3. Dockerの設定

(1). CentOS7のイメージを構築します

docker pull centos:centos7
docker run -it centos:centos7 /bin/bash

(2). LibreOfficeとIPAフォントを導入します

yum update -y
yum install libreoffice* ipa-*-fonts

(3). 作業用フォルダを作成します

mkdir /tmp/
exit

(4). DockerのCONTAINER IDを確認します

docker ps -a

(5). 「office」の名前でコミットします
${CONTAINER ID}には先ほど確認したCONTAINER IDを入れます。

docker commit ${CONTAINER ID} office

(6). 正しく動作するか確認します
CentOS上に「/var/tmp/test.xlsx」ファイルが存在するものとします。
正常に実行されると「/var/tmp/test.pdf」が生成されます。

docker run --rm=true -it -v /var/tmp:/tmp office libreoffice --headless --nologo --nofirststartwizard --convert-to pdf --outdir /tmp /tmp/test.xlsx

4. Javaのコーディング

javaのprocessクラスでdockerを起動するサンプルです。


    /**
     * Docker経由でPDF変換を行う
     * @param excelFileName
     * @param pdfFileName
     * @return
     * @throws InterruptedException
     */
    public boolean convertPDF(String excelFileName) throws InterruptedException {

        //起動コマンド指定
        String[] command = {
                "docker", "run", "--rm=true", "-v", "/var/tmp:/tmp", "office", "libreoffice", "--headless"
                , "--nologo", "--nofirststartwizard", "--convert-to", "pdf", "--outdir"
                ,"/tmp", "/tmp/"+excelFileName };

        ProcessBuilder pb = new ProcessBuilder(command);

        try {
            //コマンド実行
            Process process = pb.start();

            //コマンド実行の結果を待機
            int ret = process.waitFor();

            //標準出力
            InputStream is = process.getInputStream();  
            printInputStream(is);

            //標準エラー
            InputStream es = process.getErrorStream();  
            printInputStream(es);

        }
        catch (Exception e) {
            return false;

        }

        return true;
    }

    /**
     * 標準・エラー出力を表示する
     * @param is
     * @throws IOException
     */
    public static void printInputStream(InputStream is) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        try {
            for (;;) {
                String line = br.readLine();
                if (line == null) break;
                System.out.println(line);
            }
        } finally {
            br.close();
        }
    }
6
2
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
6
2