3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【議事録】Laravel 5.4 Artisanコンソールにtcpdf_addfont.phpコマンドを追加

3
Posted at

背景

下記の記事で日本語のPDFが使えるようになりましたが、vendor/tecnick.com/tcpdf の中にフォントが追加されてしまうので、gitコミットができません。
https://qiita.com/edisonthk/items/01c84ed6d0fceee19a8c

せっかくローカルでは日本語が使えるようになったのに、コミットされないと、awsやherokuなどにデプロイされても日本語が使えません。

それで、デプロイ時にtcpdf_addfontが実行されるようにartisanコマンドに追加しました。

tcpdf_addfontのArtisanコマンドを作成

Artisan コマンドを作成

php artisan make:command TcpdfAddFont

app/Console/Kernel.php にコマンドを登録 (Register)

app/Console/Kernel.php
protected $commands = [
    ...
    Commands\TcpdfAddFont::class,  // これを追加
        ...
];
app/Console/Commands/TcpdfAddFont.php
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use TCPDF_FONTS;

class TcpdfAddFont extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'tcpdf:add-font {font-file} {font-type}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'TCPDFに新しいフォントを登録、追加するコマンド';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {

        $fontFile = $this->argument('font-file');
        $fontType = $this->argument('font-type');
        $enc = '';
        $flags = 32;
        $outpath = '';
        $platid = 3;
        $encid = 1;
        $addcbbox = true;

        // 関数の詳細はvendor/tecnick.com/tcpdf/include/tcpdf_fonts.php を参照してください。
        // 
        $fontname = TCPDF_FONTS::addTTFfont(base_path('resources/fonts/'.$fontFile), $fontType, $enc, $flags, $outpath, $platid, $encid, $addcbbox);

        if($fontname === false) {
            $this->comment('フォント登録失敗しました');
        } else {
            $this->comment($fontname.'を登録しました');
        }
    }
}

<project_root>/resources/fonts/フォルダにipaexg.ttfを配置。

最後にcomposer.jsonにTcpdfAddFontコマンドが実行されるように追加

composer.json
{
...
    "scripts": {
        ...
        "post-install-cmd": [
            ...
            "php artisan tcpdf:add-font ipaexg.ttf TrueTypeUnicode"
        ],
        "post-update-cmd": [
            ...
            "php artisan tcpdf:add-font ipaexg.ttf TrueTypeUnicode"
        ]
        ...
    },
...
}
3
4
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
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?