WebアプリでPDF文書を自動生成 on Ubuntu14.04 LTS

# 関連記事一覧

LibreOfficeでPDF文書を自動生成
https://qiita.com/nanbuwks/items/8ea939ad497fec29c069

LibreOffice で MySQL連動 PDF帳票を作る
https://qiita.com/nanbuwks/items/d4696542760cf4b8a24a

WebアプリでPDF文書を自動生成 on Ubuntu14.04 LTS (この記事)
https://qiita.com/nanbuwks/items/7dbd2a0a79b232f864b4

WebアプリでPDF文書を自動生成 on Ubuntu20.04 LTS
https://qiita.com/nanbuwks/items/f11e4e3aabac33f9a32b


# Webサーバ化

「LibreOfficeでPDF文書を自動生成」
http://qiita.com/nanbuwks/items/8ea939ad497fec29c069
で苦労しなくても

```

$ soffice --headless --convert-to pdf *.ods

```

だけでpdfコンバートできた件。

Webアプリにこのやりかたを適用してみる。


(2021/03/29追記)
Ubuntu20.04を使い別のやり方で実現した記事を以下に書きました
「WebアプリでPDF文書を自動生成 on Ubuntu20.04 LTS」
https://qiita.com/nanbuwks/items/f11e4e3aabac33f9a32b


# 環境

Webサーバ環境は、Ubuntu 14.04.3 LTS。
AWSのt2.small で動かしている。
X-windowは動作していない。

# LibreOfficeインストール

```

# apt-get update
# apt-get install LibreOffice
Reading package lists... Done
Building dependency tree       
Reading state information... Done
You might want to run 'apt-get -f install' to correct these:
The following packages have unmet dependencies:
 ca-certificates-java : Depends: openjdk-7-jre-headless (>= 7~u3-2.1.1~pre1-1) but it is not going to be installed or
                                 java6-runtime-headless
 libreoffice : Depends: fonts-sil-gentium-basic but it is not going to be installed
               Depends: libreoffice-base but it is not going to be installed
               Depends: libreoffice-calc but it is not going to be installed
               Depends: libreoffice-core (= 1:4.2.8-0ubuntu5.1) but it is not going to be installed
               Depends: libreoffice-draw but it is not going to be installed
               Depends: libreoffice-impress but it is not going to be installed
               Depends: libreoffice-math but it is not going to be installed
               Depends: libreoffice-report-builder-bin but it is not going to be installed
               Depends: libreoffice-writer but it is not going to be installed
               Depends: libreoffice-avmedia-backend-gstreamer but it is not going to be installed
               Depends: fonts-dejavu but it is not going to be installed
               Depends: libreoffice-java-common (>= 1:4.2.8~) but it is not going to be installed
               Depends: python3-uno (>= 4.0~) but it is not going to be installed
               Recommends: fonts-liberation but it is not going to be installed or
                           ttf-mscorefonts-installer but it is not installable
               Recommends: libreoffice-gnome but it is not going to be installed or
                           libreoffice-kde but it is not going to be installed
E: Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution).
u# apt-get -f install
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Correcting dependencies... Done
The following packages were automatically installed and are no longer required:
・
・
・
u# apt-get install LibreOffice
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are n
```
インストールできた

# Webサーバ実行ユーザでテスト

```

# su - www-data


```

実行してみる

```

$ soffice --headless --convert-to pdf *.ods
[Java framework] Error in function createSettingsDocument (elements.cxx).
javaldx failed!
Warning: failed to read path from javaldx
/usr/lib/libreoffice/program/soffice.bin: error while loading shared libraries: libjpeg.so.8: cannot open shared object file: No such file or directory
$ 
```
エラー出た。
これはwww-dataのホームディレクトリに、libreofficeの設定ファイルが書き込めないため。
専用のディレクトリを作ってみる。

```

html  index.html
root@www:/var/www# mkdir libreoffice
root@www:/var/www# chown -R www-data:www-data libreoffice
root@www:/var/www# su - www-data

```

改めてwww-dataユーザで

```


$ soffice --headless --convert-to pdf *.ods
/usr/lib/libreoffice/program/soffice.bin: error while loading shared libraries: libjpeg.so.8: cannot open shared object file: No such file or directory

```

別のエラー。libjpeg.so.8はどこにあるのかな。調べるためにはapt-fileコマンドが使えるらしい。

```

# apt-file search libjpeg.so.8
The program 'apt-file' is currently not installed. You can install it by typing:
apt-get install apt-file

```
インストールしていなかったのでインストール

```

# apt-get install apt-file
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  gir1.2-gudev-1.0 libfile-copy-recursive-perl libgudev-1.0-0 ttf-dejavu-core
  update-inetd
Use 'apt-get autoremove' to remove them.
The following extra packages will be installed:
  libapt-pkg-perl libconfig-file-perl liblist-moreutils-perl
  libregexp-assemble-perl
The following NEW packages will be installed:
  apt-file libapt-pkg-perl libconfig-file-perl liblist-moreutils-perl
  libregexp-assemble-perl
0 upgraded, 5 newly installed, 0 to remove and 415 not upgraded.
Need to get 253 kB of archives.
After this operation, 870 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
・
・
・
```
改めて実行すると最初にapt-file update しろと出るのでやっておく

```
# apt-file search libjpeg.so.8
E: The cache is empty. You need to run 'apt-file update' first.
# apt-file update
Downloading complete file http://us-east-1.ec2.archive.ubuntu.com/ubuntu/dists/trusty/Contents-amd64.gz
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 28.0M  100 28.0M    0     0  23.7M      0  0:00:01  0:00:01 --:--:-- 23.7M
Downloading complete file http://us-east-1.ec2.archive.ubuntu.com/ubuntu/dists/trusty-updates/Contents-amd64.gz
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 45.3M  100 45.3M    0     0  22.2M      0  0:00:02  0:00:02 --:--:-- 22.2M
Downloading Index http://us-east-1.ec2.archive.ubuntu.com/ubuntu/dists/trusty/Contents-amd64.diff/Index:
No Index available.
Downloading complete file http://us-east-1.ec2.archive.ubuntu.com/ubuntu/dists/trusty/Contents-amd64.gz
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0 28.0M    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
File is up-to-date.
Downloading Index http://us-east-1.ec2.archive.ubuntu.com/ubuntu/dists/trusty-updates/Contents-amd64.diff/Index:
No Index available.
Downloading complete file http://us-east-1.ec2.archive.ubuntu.com/ubuntu/dists/trusty-updates/Contents-amd64.gz
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0 45.3M    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
File is up-to-date.
Downloading complete file http://security.ubuntu.com/ubuntu/dists/trusty-security/Contents-amd64.gz
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 42.1M  100 42.1M    0     0  5316k      0  0:00:08  0:00:08 --:--:-- 5736k
Downloading Index http://security.ubuntu.com/ubuntu/dists/trusty-security/Contents-amd64.diff/Index:
No Index available.
Downloading complete file http://security.ubuntu.com/ubuntu/dists/trusty-security/Contents-amd64.gz
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0 42.1M    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
File is up-to-date.

```
やっと apt-file が実行できる

```

# apt-file search libjpeg.so.8
libjpeg-turbo8: /usr/lib/x86_64-linux-gnu/libjpeg.so.8
libjpeg-turbo8: /usr/lib/x86_64-linux-gnu/libjpeg.so.8.0.2

```
なるほど。libjpeg-turbo8をインストール。

```

# apt-get install libjpeg-turbo8
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  gir1.2-gudev-1.0 libfile-copy-recursive-perl libgudev-1.0-0 ttf-dejavu-core
  update-inetd
Use 'apt-get autoremove' to remove them.
The following packages will be upgraded:
  libjpeg-turbo8
1 upgraded, 0 newly installed, 0 to remove and 414 not upgraded.
Need to get 104 kB of archives.
After this operation, 23.6 kB of additional disk space will be used.
Get:1 http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ trusty/main libjpeg-turbo8 amd64 1.3.0-0ubuntu2 [104 kB]
Fetched 104 kB in 0s (6,563 kB/s)  
(Reading database ... 43600 files and directories currently installed.)
Preparing to unpack .../libjpeg-turbo8_1.3.0-0ubuntu2_amd64.deb ...
Unpacking libjpeg-turbo8:amd64 (1.3.0-0ubuntu2) over (1.1.90+svn733-0ubuntu4.4) ...
Setting up libjpeg-turbo8:amd64 (1.3.0-0ubuntu2) ...
Processing triggers for libc-bin (2.15-0ubuntu10.7) ...
ldconfig deferred processing now taking place

```

今度はrootで実行してみた

```

# soffice --headless --convert-to pdf *.ods
Fontconfig error: Cannot load default config file
Fontconfig error: Cannot load default config file
convert /hogehoge/1502089893000000002/1502089893000000002.ods -> /hogehoge/1502089893000000002/1502089893000000002.pdf using calc_pdf_Export
# ls
1502089893000000002.ods  1502089893000000002.pdf
```
pdfができた。しかしながら日本語が表示されていない。

```

# export FONTCONFIG_PATH=/etc/fonts
```

としたが、同じだった。


日本語環境ってこれかな？

```
#  apt-get install language-pack-ja-base language-pack-ja
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  gir1.2-gudev-1.0 libfile-copy-recursive-perl libgudev-1.0-0 ttf-dejavu-core
  update-inetd
Use 'apt-get autoremove' to remove them.
The following NEW packages will be installed:
  language-pack-ja language-pack-ja-base
0 upgraded, 2 newly installed, 0 to remove and 414 not upgraded.
Need to get 1,993 kB of archives.
After this operation, 9,234 kB of additional disk space will be used.
Get:1 http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ trusty-updates/main language-pack-ja-base all 1:14.04+20160720 [1,992 kB]
Get:2 http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ trusty-updates/main language-pack-ja all 1:14.04+20160720 [1,818 B]
Fetched 1,993 kB in 0s (7,500 kB/s)     
Selecting previously unselected package language-pack-ja-base.
(Reading database ... 43600 files and directories currently installed.)
Preparing to unpack .../language-pack-ja-base_1%3a14.04+20160720_all.deb ...
Unpacking language-pack-ja-base (1:14.04+20160720) ...
Selecting previously unselected package language-pack-ja.
Preparing to unpack .../language-pack-ja_1%3a14.04+20160720_all.deb ...
Unpacking language-pack-ja (1:14.04+20160720) ...
Setting up language-pack-ja (1:14.04+20160720) ...
Setting up language-pack-ja-base (1:14.04+20160720) ...
Generating locales...
  ja_JP.UTF-8... done
Generation complete.
```

フォントがインストールされているようには見えないし、改めて作成したPDFを見ても日本語化けは治らない。使用しているODSファイルはIPAGothic/IPAMinchoを使っているのだけれど、Ubuntuの日本語フォントにIPAGothic/IPAMinchoってヒョウジュンではいってなかったっけ？
今インストールされているフォントを確かめてみる。


```

# fc-list
Fontconfig error: Cannot load default config file
/usr/share/fonts/truetype/dejavu/DejaVuSerif-Bold.ttf: DejaVu Serif:style=Bold
/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf: DejaVu Sans Mono:style=Book
/usr/share/fonts/truetype/dejavu/DejaVuSansCondensed-Oblique.ttf: DejaVu Sans,DejaVu Sans Condensed:style=Condensed Oblique,Oblique
・
・
・
```
それっぽいのがないので明示的にインストール。

```

# apt-get install fonts-ipafont-gothic fonts-ipafont-mincho
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  gir1.2-gudev-1.0 libfile-copy-recursive-perl libgudev-1.0-0 ttf-dejavu-core
  update-inetd
Use 'apt-get autoremove' to remove them.
The following NEW packages will be installed:
  fonts-ipafont-gothic fonts-ipafont-mincho
0 upgraded, 2 newly installed, 0 to remove and 414 not upgraded.
Need to get 8,245 kB of archives.
After this operation, 28.8 MB of additional disk space will be used.
Get:1 http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ trusty/main fonts-ipafont-gothic all 00303-12ubuntu1 [3,519 kB]
Get:2 http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ trusty/main fonts-ipafont-mincho all 00303-12ubuntu1 [4,726 kB]
Fetched 8,245 kB in 0s (28.2 MB/s)          
Selecting previously unselected package fonts-ipafont-gothic.
(Reading database ... 43812 files and directories currently installed.)
Preparing to unpack .../fonts-ipafont-gothic_00303-12ubuntu1_all.deb ...
Unpacking fonts-ipafont-gothic (00303-12ubuntu1) ...
Selecting previously unselected package fonts-ipafont-mincho.
Preparing to unpack .../fonts-ipafont-mincho_00303-12ubuntu1_all.deb ...
Unpacking fonts-ipafont-mincho (00303-12ubuntu1) ...
Processing triggers for fontconfig (2.11.0-0ubuntu4.2) ...
Setting up fonts-ipafont-gothic (00303-12ubuntu1) ...
update-alternatives: using /usr/share/fonts/opentype/ipafont-gothic/ipag.ttf to provide /usr/share/fonts/truetype/fonts-japanese-gothic.ttf (fonts-japanese-gothic.ttf) in auto mode
Setting up fonts-ipafont-mincho (00303-12ubuntu1) ...
update-alternatives: using /usr/share/fonts/opentype/ipafont-mincho/ipam.ttf to provide /usr/share/fonts/truetype/fonts-japanese-mincho.ttf (fonts-japanese-mincho.ttf) in auto mode
```

これでうまくいきました。