LoginSignup
1
1

More than 3 years have passed since last update.

laravel-dompdf の使い方

Last updated at Posted at 2020-03-17

1) プロジェクトの作成

laravel new pdf01

2) ライブラリーのインストール

cd pdf01
composer require barryvdh/laravel-dompdf
php artisan vendor:publish --provider="Barryvdh\DomPDF\ServiceProvider"

3) コントローラーの作成

php artisan make:controller HomeController
app/Http/Controllers/HomeController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HomeController extends Controller
{
    //
public function index() {
        $pdf = \PDF::loadView('generate_pdf');
        return $pdf->download('test01.pdf');
        }
}

4) View の作成

resources/views/generate_pdf.blade.php
<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="UTF-8">
  <title>My First Page</title>
</head>
<body>
<h1>Hello World!!</h1>
<blockquote>
        Good Afternoon<p />
</blockquote>
Mar/17/2020 PM 15:48<p />
</body>
</html>

5) routes の作成

routes/web.php
<?php

use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return view('welcome');
});

Route::get('pdf', 'HomeController@index')->name('generate_pdf.index');

6) サーバーの起動

php artisan serve --host 0.0.0.0

7) クライアントからアクセス

dompdf_mar17.png

test01.pdf という PDF がダウンロードされます。
dompdf_mar1702.png

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