Creating PDF Files in Laravel 9: A Step-by-Step Guide using DomPDF
Tags: laravel, programming, php, tutorial
Published: 2023-01-27 08:33:34

Creating PDF files in Laravel is a common task for many web applications. Whether it’s for invoices, reports, or other types of documents, the ability to generate PDFs can be a valuable feature for your users. In this tutorial, we will show you how to create PDF files in Laravel 9 using the [laravel-dompdf](https://github.com/barryvdh/laravel-dompdf) package.

### Step 1: Install DomPDF

The first thing you need to do is add the DomPDF package to your laravel-app. You can do this by running the following command in Composer:

```bash 
composer require barryvdh/laravel-dompdf
```

### Step 2: Register the Service Provider

Once the package is installed, you need to register the service provider in the `config/app.php` file. You can do this by adding the following line to the `providers` array:
```php
'providers' => [
    // ...
    Barryvdh\DomPDF\ServiceProvider::class,
],
```

Next, we’ll add an alias for the package to the `aliases` array in the same file:

```php
'aliases' => [
    // ...
    'PDF' => Barryvdh\DomPDF\Facade::class,
],
```
### Step 3: Create a Route

Next, you need to create a route that will handle the PDF generation. In this example, we will create a route that generates a PDF from an HTML view.
```php
Route::get('/pdf', function () {
    $pdf = PDF::loadView('invoice', $data);
    return $pdf->download('invoice.pdf');
});
```

The above code will take the `invoice` view and data, and generate a PDF that the user can download. The download method is used to prompt the user to download the file.

### Step 4: Create a View

Now you need to create an HTML view that will be used as the template for the PDF. You can do this by creating a new file in the `resources/views` folder called `invoice.blade.php`. This file can contain any HTML and CSS that you want to be included in the PDF.

### Step 5: Customize and Style the PDF

You can customize the PDF by editing the view file or you can use css file separately. You can use the following code in order to include css file in pdf.
```php
$pdf = PDF::loadView('invoice')->setPaper('a4', 'portrait')->setWarnings(false);
return $pdf->download('invoice.pdf');
```

### Step 6: Download or Save the PDF

Finally, you can use the `download` method to prompt the user to download the PDF or use the `save` method to save it to a specific location on your server.
```php
Route::get('/pdf', function () {
    $pdf = PDF::loadView('invoice', $data);
    return $pdf->save('/path-to/my_stored_file.pdf')->stream('invoice.pdf');
    //return $pdf->download('invoice.pdf');
});
```

In conclusion, creating PDF files in Laravel 9 is a simple task that can be accomplished using the [laravel-dompdf](https://github.com/barryvdh/laravel-dompdf)  package. By following the steps outlined in this tutorial, you can easily generate PDFs for your web application. This can be a great feature for creating invoices, reports, and other types of documents, with the added benefit of being SEO optimized.

I hope this tutorial has been helpful in understanding how to use DomPDF to generate PDFs in Laravel 9. Happy coding!