内容
TCPDFでPDFを日本語で出力しようとしたところ文字化け?が治らない
試したこと①
①日本語フォントをTCPDFへ登録するプログラムを実行
参考サイト
https://office-obata.com/report/memorandum/post-5373/
PHP
1require_once('tcpdf/tcpdf.php'); 2 3/** PDFクラス取得 */ 4$font = new TCPDF_FONTS(); 5 6//ttfフォントをTCPDF用に変換 7$myFont = $font->addTTFfont("ipag.ttf");
fontフォルダにipag.ctg.z/ipag.php/ipag.z 3種類のファイルが作成されたことを確認しそれらのファイルをアップロード
PHP
1include dirname(__FILE__)."/../tcpdf_main/tcpdf.php"; 2//レビュー出力プログラム 3$tcpdf = new TCPDF("L", "mm", "A4", true, "UTF-8" ); 4$tcpdf->AddPage(); 5$tcpdf->SetFont("ipag", "", 10); 6$html = <<< EOF 7<style> 8table {width: 500px;} 9 10table th {text-align: center; font-weight: bold;} 11table th.no {width: 50px;} 12table th.name {width: 150px;} 13table th.age {width: 50px;} 14table th.address {width: 250px;} 15 16table td.no {text-align: right;} 17table td.name {text-align: left;} 18table td.age {text-align: right;} 19table td.address {text-align: left;} 20</style> 21<h1>ユーザ一覧</h1> 22<table border="1"> 23 <tr> 24 <th class="no">No</th> 25 <th class="name">名前</th> 26 <th class="age">年齢</th> 27 <th class="address">住所</th> 28 </tr> 29 <tr> 30 <td class="no">1</td> 31 <td class="name">山田</td> 32 <td class="age">25</td> 33 <td class="address">東京都港区</td> 34 </tr> 35 <tr> 36 <td class="no">2</td> 37 <td class="name">佐藤</td> 38 <td class="age">32</td> 39 <td class="address">埼玉県さいたま市</td> 40 </tr> 41 <tr> 42 <td class="no">3</td> 43 <td class="name">鈴木</td> 44 <td class="age">23</td> 45 <td class="address">大阪府大阪市</td> 46 </tr> 47</table> 48EOF; 49 50$tcpdf->writeHTML($html); 51$tcpdf->Output("user.pdf", "I");
【結果】上記を実行すると「?」だけが表示されたPDFが出来上がる。
試したこと②
ヒアドキュメント内の日本語部分を全て英語に変更
【結果】テーブルも文字も表示されます。
試したこと③
PHP
1$tcpdf->SetFont('kozminproregular', '', 24);
フォント指定を上記デフォルトの日本語対応フォントを指定
【結果】先ほど同様「?」のみです。
試したこと④
下記のようにヒアドキュメント内のテーブル内を全て英語にしてタイトルのみ日本語にしても「?」だけの表示となります。
$html = <<< EOF <style> table {width: 500px;} table th {text-align: center; font-weight: bold;} table th.no {width: 50px;} table th.name {width: 150px;} table th.age {width: 50px;} table th.address {width: 250px;} table td.no {text-align: right;} table td.name {text-align: left;} table td.age {text-align: right;} table td.address {text-align: left;} </style> <h1>ユーザ一覧</h1> <table border="1"> <tr> <th class="no">No</th> <th class="name">aaa</th> <th class="age">bbb</th> <th class="address">ccc</th> </tr> <tr> <th class="no">No</th> <th class="name">aaa</th> <th class="age">bbb</th> <th class="address">ccc</th> </tr> <tr> <th class="no">No</th> <th class="name">aaa</th> <th class="age">bbb</th> <th class="address">ccc</th> </tr> <tr> <th class="no">No</th> <th class="name">aaa</th> <th class="age">bbb</th> <th class="address">ccc</th> </tr> </table> EOF;
見解
日本語が出現すると全ての表示が「?」になっているようだと思ったのですが、どのように検証すればいいかわかりませんでした。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。