LoginSignup
5
5

More than 5 years have passed since last update.

PHPExcel使ったあとLibreOfficeからコマンドでPDF出力をする時の文字化け対策

Posted at

タイトル通りの対策メモ

ケースとしては

  • 良い感じのフォントがインストールされていない
  • PDFで出力される文字が期待してるのと違う

ときに有効なフォント変更方法。

環境

  • RHEL6系
  • LibreOffice 4.2.8.2
  • 出力元ファイル:xls形式のExcelファイルや、PDFファイル
  • Excelファイル作成/編集にはPHPExcelを使用。
  • PDF変換にはLibreOfficeをCommandで呼び出して変換している。

現状確認

1.インスコされているフォント確認

$ fc-list
Liberation Mono:style=Regular
DejaVu Serif,DejaVu Serif Condensed:style=Condensed,Book
DejaVu Serif,DejaVu Serif Condensed:style=Condensed Bold Italic,Bold Italic
DejaVu Serif,DejaVu Serif Condensed:style=Condensed Bold,Bold
Liberation Sans:style=Regular
DejaVu Sans,DejaVu Sans Condensed:style=Condensed Oblique,Oblique
DejaVu Sans Mono:style=Bold Oblique
Carlito:style=Italic
Liberation Serif:style=Bold Italic
Caladea:style=Bold
DejaVu Serif:style=Bold Italic
OpenSymbol:style=Regular
Caladea:style=Bold Italic,Italic
DejaVu Sans:style=Bold Oblique
Carlito:style=Regular
Liberation Serif:style=Bold
Liberation Mono:style=Bold Italic
DejaVu Sans:style=Oblique
Liberation Sans:style=Bold
DejaVu Sans Mono:style=Oblique
Carlito:style=Bold Italic
DejaVu Sans,DejaVu Sans Condensed:style=Condensed Bold Oblique,Bold Oblique
Caladea:style=Italic
Carlito:style=Bold
Liberation Serif:style=Regular
VL Pゴシック,VL PGothic:style=regular
DejaVu Sans,DejaVu Sans Light:style=ExtraLight
Liberation Sans:style=Bold Italic
IPAゴシック,IPAGothic:style=Regular
DejaVu Sans,DejaVu Sans Condensed:style=Condensed,Book
DejaVu Sans,DejaVu Sans Condensed:style=Condensed Bold,Bold
Liberation Mono:style=Bold
IPA Pゴシック,IPAPGothic:style=Regular
DejaVu Serif,DejaVu Serif Condensed:style=Condensed Italic,Italic
Caladea:style=Regular
DejaVu Sans:style=Bold
DejaVu Sans:style=Book
DejaVu Serif:style=Italic
DejaVu Sans Mono:style=Book
IPA明朝,IPAMincho:style=Regular
DejaVu Sans Mono:style=Bold
Liberation Serif:style=Italic
Liberation Mono:style=Italic
IPA P明朝,IPAPMincho:style=Regular
Liberation Sans:style=Italic
VL ゴシック,VL Gothic:style=regular
DejaVu Serif:style=Book
DejaVu Serif:style=Bold

2.PDF出力されたフォント確認

Adobe Acrobatとかだと「Command+d」の文章プロパティ内「フォント」タブで確認できる。
スクリーンショット 2016-02-08 17.17.58.png

このとき、DejaVuSans系かIPAGothic、VL何とかが原因で文字化けをしていると判断できる。
IPAGothicとかは文字化けしない可能性が高いと思われるが、簡単にうまくいかなかったのでIPAexGothicを使用することにする。

フォントインスコ

1.IPAGothicをダウンロード

こちらから諸々にきちんと読んで問題無ければ同意してダウンロード。

2.解凍&Upload

省略。適当にサーバーにあげます。
(ここではIPAexゴシックを使います)

3.フォントファイルを配置

root権限つかえるユーザーで

$ mkdir /usr/share/fonts/ipa-ex-gothic

って感じで適当にフォルダを作る。
そして中に「1.」でダウンロード/解凍したフォントファイルをいれればいい。
Permissionを0644にしておく。

$ cp /home/ykeisuke/fonts/ipaexg.ttf /usr/share/fonts/ipa-ex-gothic/
$ chmod 0644 /usr/share/fonts/ipa-ex-gothic/ipaexg.ttf

4.フォントキャッシュ削除

$ fc-cache -fv
/usr/share/fonts: caching, new cache contents: 0 fonts, 11 dirs
/usr/share/fonts/dejavu: caching, new cache contents: 21 fonts, 0 dirs
/usr/share/fonts/google-crosextra-caladea: caching, new cache contents: 4 fonts, 0 dirs
/usr/share/fonts/google-crosextra-carlito: caching, new cache contents: 4 fonts, 0 dirs
/usr/share/fonts/ipa-ex-gochic: caching, new cache contents: 1 fonts, 0 dirs
/usr/share/fonts/ipa-gothic: caching, new cache contents: 1 fonts, 0 dirs
/usr/share/fonts/ipa-mincho: caching, new cache contents: 1 fonts, 0 dirs
/usr/share/fonts/ipa-pgothic: caching, new cache contents: 1 fonts, 0 dirs
/usr/share/fonts/ipa-pmincho: caching, new cache contents: 1 fonts, 0 dirs
/usr/share/fonts/liberation: caching, new cache contents: 12 fonts, 0 dirs
/usr/share/fonts/opensymbol: caching, new cache contents: 1 fonts, 0 dirs
/usr/share/fonts/vlgothic: caching, new cache contents: 2 fonts, 0 dirs
/usr/share/X11/fonts/Type1: skipping, no such directory
/usr/share/X11/fonts/TTF: skipping, no such directory
/usr/local/share/fonts: skipping, no such directory
/root/.fonts: skipping, no such directory
/var/cache/fontconfig: cleaning cache directory
/root/.fontconfig: not cleaning non-existent cache directory
fc-cache: succeeded

5.フォント一覧

「3.」で配置したフォントがインスコされてリストに入ってるのがわかる

$ fc-list
(略)
IPAexゴシック,IPAexGothic:style=Regular
(略)

PHPExcelを読んでるところに手を加える

フォントをインスコした段階で治ればよかったんだが、治らなかった。
MacのMS-Excel側でフォントを指定したがPHPExcelで出力されるExcelファイルのフォントは先ほどインスコしたものになってるのにPDFでは変わってないとかあった。

MacのLibreOfficeで保存しなおしてとかPHPexcelなのかLibreOfficeなのか問題の切り分けをしてーとか色々あるが、PHPExcel+LibreOffice4の組み合わせは当分かわらないという判断のもと特定やめた。

        $reader = PHPExcel_IOFactory::createReader('Excel5');
        $excel = $reader->load("/INPUT/YOUR/EXCEL/PATH");
        $sheet = $excel->getSheetByName("input_your_sheet_name");
        $sheet->getDefaultStyle()->getFont()->setName('IPAexGothic');

な感じで明確に指定する。
このときIPAexGothicはfc-listで出てきた名称となるよ。

最後に

RHEL6系のOSで、PHPで綺麗なPDFを吐き出す方法といったらなんだろう?

LibreOffice4をコマンドラインで呼び出すと、一番綺麗(元Excelに近い形)で吐き出せた。
LibreOfficeの元のOpenOfficeには触っていない。

fPDFとかftPDFとかは、少し触りましたが簡単なHTMLを作って呼び出す分には問題無いと思うんだけど、色々とコードを書いたりしなきゃならない感じがしたので見送った。
ExcelファイルPDF変換ライブラリとかやりたくない。

以上により何も考えないでExcelファイルがそれなりに良い感じに出力されるLibreOfficeが最強と結論にいたった

5
5
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
5
5