18
19

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 3 years have passed since last update.

Laravel5.8環境で日本語PDF出力(Laravel-dompdf)

Last updated at Posted at 2019-10-16

Laravel5.8環境で日本語PDF出力(Laravel-dompdf)

laravel-dompdfパッケージのインストール

composer.jsonファイルに
"barryvdh/laravel-dompdf": "^0.8.5", を追加して
プロジェクトディレクトリ(/home/public_html)に移動し下記コマンドを実行する。

  1. composer updateを実行
  2. composer require barryvdh/laravel-dompdf

PDF出力(Route設定)

Route::get('/payslip', 'Payslip\PayslipController@index');
Route::group(['middleware' => 'auth.check.user'], function () {
  Route::get('/payslip/pdf', [ 'as' => 'payslip.pdf', 'uses' => 'Payslip\PayslipController@printPDF']);
});

コントローラー (PayslipController)

# HTML確認時
return view('payslip.pdf', $data);


# PDFファイル出力時
$pdf = PDF::loadView('payslip.pdf', $data)
        ->setPaper('a4')->setWarnings(false);

 return $pdf->download('payslip.pdf');

view

  1. resource/view/payslip/index.blade.phpを作成し、下記のリンクを貼る

aタグのリンクURL:{{route('payslip.pdf')}}?ym=201911 (例:2019年11月分)

  1. resource/view/payslip/pdf.blade.phpを作成し、PDF用内容を実装

日本語化への対応

  1. fontsディレクトリの作成
    storage/fonts
  2. IPAフォントのダウンロード
    https://ipafont.ipa.go.jp/old/ipafont/download.html
    zipファイル解凍後、storage/fonts/ディレクトリの下にコピー
  3. CSSの@font-faceを利用
18
19
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
18
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?