2
1

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.

Mac Laravelでライブラリ「laravel-dompdf」をインストールする

Last updated at Posted at 2020-03-23

目的

  • Laravelのライブラリインストールが初めてだったので実施方法をまとめる
  • ライブラリ「laravel-dompdf」のインストール方法をまとめる

実施環境

  • ハードウェア環境
項目 情報
OS macOS Catalina(10.15.3)
ハードウェア MacBook Air (11-inch ,2012)
プロセッサ 1.7 GHz デュアルコアIntel Core i5
メモリ 8 GB 1600 MHz DDR3
グラフィックス Intel HD Graphics 4000 1536 MB
  • ソフトウェア環境
項目 情報
PHP バージョン 7.4.3
Laravel バージョン 7.0.8
  • ライブラリ環境
項目 情報 備考
laravel-dompdf v0.8.6 アプリ名ディレクトリに移動後コマンド`composer show -i

実施方法概要

  1. インストール
  2. 設定
  3. 確認

実施方法詳細

  • ローカル開発環境を前提に説明を記載する。
  1. インストール
    1. 下記コマンドを実行してライブラリ「laravel-dompdf」をインストールする。

      $ composer require barryvdh/laravel-dompdf
      
  2. 設定
    1. 下記コマンドを実行して設定を記載するファイルを開く。

      $ vi アプリ名ディレクトリ/app.php
      
    2. 先のコマンドを実行後、viのコマンドモードにて下記のコマンドを入力して設定を追記する行を検索する。

      /'providers' => [
      
    3. 検索にヒットした部分が下記の様な記載になっていることを確認する。

      app.php
      'providers' => [
      
          /*
           * Laravel Framework Service Providers...
           */
          Illuminate\Auth\AuthServiceProvider::class,
          Illuminate\Broadcasting\BroadcastServiceProvider::class,
          Illuminate\Bus\BusServiceProvider::class,
          Illuminate\Cache\CacheServiceProvider::class,
          Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class,
          Illuminate\Cookie\CookieServiceProvider::class,
          Illuminate\Database\DatabaseServiceProvider::class,
          Illuminate\Encryption\EncryptionServiceProvider::class,
          Illuminate\Filesystem\FilesystemServiceProvider::class,
          Illuminate\Foundation\Providers\FoundationServiceProvider::class,
          Illuminate\Hashing\HashServiceProvider::class,
          Illuminate\Mail\MailServiceProvider::class,
          Illuminate\Notifications\NotificationServiceProvider::class,
          Illuminate\Pagination\PaginationServiceProvider::class,
          Illuminate\Pipeline\PipelineServiceProvider::class,
          Illuminate\Queue\QueueServiceProvider::class,
          Illuminate\Redis\RedisServiceProvider::class,
          Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
          Illuminate\Session\SessionServiceProvider::class,
          Illuminate\Translation\TranslationServiceProvider::class,
          Illuminate\Validation\ValidationServiceProvider::class,
          Illuminate\View\ViewServiceProvider::class,
      
          /*
           * Package Service Providers...
           */
      
          /*
           * Application Service Providers...
           */
          App\Providers\AppServiceProvider::class,
          App\Providers\AuthServiceProvider::class,
          // App\Providers\BroadcastServiceProvider::class,
          App\Providers\EventServiceProvider::class,
          App\Providers\RouteServiceProvider::class,
      
      ],
      
    4. 先に「app.php」ファイルを検索したファイルのApp\Providers\RouteServiceProvider::class,の後に下記の内容を追記する。

      Barryvdh\DomPDF\ServiceProvider::class,
      
    5. 先の内容を追記後、下記の様に「app.php」ファイルが記載されていることを確認する。

      app.php
      'providers' => [
      
          /*
           * Laravel Framework Service Providers...
           */
          Illuminate\Auth\AuthServiceProvider::class,
          Illuminate\Broadcasting\BroadcastServiceProvider::class,
          Illuminate\Bus\BusServiceProvider::class,
          Illuminate\Cache\CacheServiceProvider::class,
          Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class,
          Illuminate\Cookie\CookieServiceProvider::class,
          Illuminate\Database\DatabaseServiceProvider::class,
          Illuminate\Encryption\EncryptionServiceProvider::class,
          Illuminate\Filesystem\FilesystemServiceProvider::class,
          Illuminate\Foundation\Providers\FoundationServiceProvider::class,
          Illuminate\Hashing\HashServiceProvider::class,
          Illuminate\Mail\MailServiceProvider::class,
          Illuminate\Notifications\NotificationServiceProvider::class,
          Illuminate\Pagination\PaginationServiceProvider::class,
          Illuminate\Pipeline\PipelineServiceProvider::class,
          Illuminate\Queue\QueueServiceProvider::class,
          Illuminate\Redis\RedisServiceProvider::class,
          Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
          Illuminate\Session\SessionServiceProvider::class,
          Illuminate\Translation\TranslationServiceProvider::class,
          Illuminate\Validation\ValidationServiceProvider::class,
          Illuminate\View\ViewServiceProvider::class,
      
          /*
           * Package Service Providers...
           */
      
          /*
           * Application Service Providers...
           */
          App\Providers\AppServiceProvider::class,
          App\Providers\AuthServiceProvider::class,
          // App\Providers\BroadcastServiceProvider::class,
          App\Providers\EventServiceProvider::class,
          App\Providers\RouteServiceProvider::class,
          Barryvdh\DomPDF\ServiceProvider::class,
      
      ],
      
    6. 先のコマンドを実行後、viのコマンドモードにて下記のコマンドを入力して設定を追記する行を検索する。

      /'aliases' => [
      
    7. 検索にヒットした部分が下記の様な記載になっていることを確認する。

      app.php
      'aliases' => [
      
          'App' => Illuminate\Support\Facades\App::class,
          'Arr' => Illuminate\Support\Arr::class,
          'Artisan' => Illuminate\Support\Facades\Artisan::class,
          'Auth' => Illuminate\Support\Facades\Auth::class,
          'Blade' => Illuminate\Support\Facades\Blade::class,
          'Broadcast' => Illuminate\Support\Facades\Broadcast::class,
          'Bus' => Illuminate\Support\Facades\Bus::class,
          'Cache' => Illuminate\Support\Facades\Cache::class,
          'Config' => Illuminate\Support\Facades\Config::class,
          'Cookie' => Illuminate\Support\Facades\Cookie::class,
          'Crypt' => Illuminate\Support\Facades\Crypt::class,
          'DB' => Illuminate\Support\Facades\DB::class,
          'Eloquent' => Illuminate\Database\Eloquent\Model::class,
          'Event' => Illuminate\Support\Facades\Event::class,
          'File' => Illuminate\Support\Facades\File::class,
          'Gate' => Illuminate\Support\Facades\Gate::class,
          'Hash' => Illuminate\Support\Facades\Hash::class,
          'Http' => Illuminate\Support\Facades\Http::class,
          'Lang' => Illuminate\Support\Facades\Lang::class,
          'Log' => Illuminate\Support\Facades\Log::class,
          'Mail' => Illuminate\Support\Facades\Mail::class,
          'Notification' => Illuminate\Support\Facades\Notification::class,
          'Password' => Illuminate\Support\Facades\Password::class,
          'Queue' => Illuminate\Support\Facades\Queue::class,
          'Redirect' => Illuminate\Support\Facades\Redirect::class,
          'Redis' => Illuminate\Support\Facades\Redis::class,
          'Request' => Illuminate\Support\Facades\Request::class,
          'Response' => Illuminate\Support\Facades\Response::class,
          'Route' => Illuminate\Support\Facades\Route::class,
          'Schema' => Illuminate\Support\Facades\Schema::class,
          'Session' => Illuminate\Support\Facades\Session::class,
          'Storage' => Illuminate\Support\Facades\Storage::class,
          'Str' => Illuminate\Support\Str::class,
          'URL' => Illuminate\Support\Facades\URL::class,
          'Validator' => Illuminate\Support\Facades\Validator::class,
          'View' => Illuminate\Support\Facades\View::class,
      
      ],
      
    8. 先に「app.php」ファイルを検索したファイルの'View' => Illuminate\Support\Facades\View::class,の後に下記の内容を追記する。

      'PDF' => Barryvdh\DomPDF\Facade::class,
      
    9. 先の内容を追記後、下記の様に「app.php」ファイルが記載されていることを確認する。

      app.php
      'aliases' => [
      
          'App' => Illuminate\Support\Facades\App::class,
          'Arr' => Illuminate\Support\Arr::class,
          'Artisan' => Illuminate\Support\Facades\Artisan::class,
          'Auth' => Illuminate\Support\Facades\Auth::class,
          'Blade' => Illuminate\Support\Facades\Blade::class,
          'Broadcast' => Illuminate\Support\Facades\Broadcast::class,
          'Bus' => Illuminate\Support\Facades\Bus::class,
          'Cache' => Illuminate\Support\Facades\Cache::class,
          'Config' => Illuminate\Support\Facades\Config::class,
          'Cookie' => Illuminate\Support\Facades\Cookie::class,
          'Crypt' => Illuminate\Support\Facades\Crypt::class,
          'DB' => Illuminate\Support\Facades\DB::class,
          'Eloquent' => Illuminate\Database\Eloquent\Model::class,
          'Event' => Illuminate\Support\Facades\Event::class,
          'File' => Illuminate\Support\Facades\File::class,
          'Gate' => Illuminate\Support\Facades\Gate::class,
          'Hash' => Illuminate\Support\Facades\Hash::class,
          'Http' => Illuminate\Support\Facades\Http::class,
          'Lang' => Illuminate\Support\Facades\Lang::class,
          'Log' => Illuminate\Support\Facades\Log::class,
          'Mail' => Illuminate\Support\Facades\Mail::class,
          'Notification' => Illuminate\Support\Facades\Notification::class,
          'Password' => Illuminate\Support\Facades\Password::class,
          'Queue' => Illuminate\Support\Facades\Queue::class,
          'Redirect' => Illuminate\Support\Facades\Redirect::class,
          'Redis' => Illuminate\Support\Facades\Redis::class,
          'Request' => Illuminate\Support\Facades\Request::class,
          'Response' => Illuminate\Support\Facades\Response::class,
          'Route' => Illuminate\Support\Facades\Route::class,
          'Schema' => Illuminate\Support\Facades\Schema::class,
          'Session' => Illuminate\Support\Facades\Session::class,
          'Storage' => Illuminate\Support\Facades\Storage::class,
          'Str' => Illuminate\Support\Str::class,
          'URL' => Illuminate\Support\Facades\URL::class,
          'Validator' => Illuminate\Support\Facades\Validator::class,
          'View' => Illuminate\Support\Facades\View::class,
          'PDF' => Barryvdh\DomPDF\Facade::class,
      
      ],
      
  3. 確認
    1. 下記コマンドをアプリ名ルートディレクトリで実行してライブラリ導入確認のためのコントローラを作成する。

      $ php artisan make:controller PdfController
      
    2. 先のコマンドで作成されたコントローラを下記の様に修正する。

      アプリ名ルートディレクトリ/app/Http/Controllers/pdf.Controller.php
      <?php
      
      namespace App\Http\Controllers;
      
      use Illuminate\Http\Request;
      
      use PDF;
      
      class PdfController extends Controller
      {
          public function test(){
          $pdf = PDF::loadHTML('<h1>test</h1>');
          return $pdf->stream();
          }
      }
      
    3. ルーティングファイルに下記の記載を追記する。

      アプリ名ルートディレクトリ/routes/web.php
      Route::get('/pdf', 'PdfController@test');
      
    4. 下記のリンクにアクセスする。

    5. 下記の様にPDF形式で「test」の文字が表示されていることを確認する。

      スクリーンショット 2020-03-23 17.23.44.png

    6. 上記の様に表示されればLaravelのライブラリ「laravel-dompdf」のインストールは完了である。

参考文献

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?