例: 外字ファイルが eudc.tte の場合
「eudc.tte」→「eudc.ttf」
git clone https://github.com/tecnickcom/tcpdf
cd tcpdf/tools
php ./tcpdf_addfont.php -b -t eudc -f 32 -i eudc.ttf
これでフォルダ tcpdf/fonts にファイル
eudc.ctg.z
eudc.php
eudc.z
が生成されます。
これらの3つのファイルをtcpdfを動作させるサーバの好きなディレクトリにコピーします。
$pdf = new Fpdi\TcpdfFpdi();
$font_2 = $pdf->AddFont( "eudc", "", "/YOUR/SERVER/PATH/fonts/eudc.php", true );
これで
<span style="font-family:eudc">◆◆◆</span>
(◆ が外字 )
で表示されます。 外字文字のところだけ、このCSSをあててあげます。
Laravel の モデルにアクセサを登録します。
/**
* アクセサー : PDF生成時に外字文字のみCSSを追加します。
*/
public function getPdfGaijiNameAttribute()
{
if ( $this->attributes['name'] != null ){
$text = $this->attributes['name'];
$encoding = "UTF-8";
$textLength = mb_strlen($text, $encoding);
for($i = 0; $i < $textLength; $i++) {
$m = mb_substr($text, $i, 1, $encoding);
if (preg_match('/^(\xEE[\x80-\xBF])|(\xEF[\x80-\xA3])|(\xF3[\xB0-\xBF])|(\xF4[\x80-\x8F])/', $m)){
echo '<span style="font-family:eudc">' . $m ."</span>";
}
else {
echo $m;
}
}
} else {
return 'ERROR: getPdfGaijiNameAttribute()';
}
}
使い方
{{$user->name}}
↓ の代わりに次のように記述します。
{{$user->pdf_gaiji_name}}