Generate Pdf in Laravel 10 using DomPdf Example

Throughout the Laravel generate pdf example tutorial you will learn How to Generate PDF in Laravel using DomPDF library. After more research, I found that Laravel-dompdf is the best to convert HTML content to generate a pdf file.

laravel-dompdf a free package to use in PHP. It is the style-driven renderer: it will download and read external stylesheets, inline style tags, and individual HTML elements’ style attributes. It also supports most presentational HTML attributes.

This step-by-step tutorial to download pdf files in laravel application. You can generate a pdf file from a view using DomPDF. To export into PDF, We need to write the view file.

Laravel Generate Pdf File Example

So, Follow the following steps to generate a pdf file in Laravel application:

  • Step 1: Download Laravel App
  • Step 2: Install DomPDF Package in Laravel
  • Step 3: Configure DomPDF Package in Laravel
  • Step 4: Add Routes
  • Step 5: Create and Configure Controller
  • Step 6: Create Blade View File
  • Step 7: Start your Laravel project

Download Laravel App

For Laravel export to PDF demo example, we require to download a fresh Laravel app. Run the command to induct fresh Laravel project.

composer create-project laravel/laravel laravel-pdf --prefer-dist

Install DomPDF Package in Laravel

In general, there are many other third-party packages available for HTML to PDF conversion in Laravel. I seldom use those packages, but the DomPDF package is a better choice, among others.

Run the under-mentioned command to install DomPDF in Laravel.

sudo composer require barryvdh/laravel-dompdf

Configure DomPDF Package in Laravel

Open config/app.php file and incorporate DomPDF service provider in providers array along with DomPDF facade to the aliases array.

'providers' => [
	....

	Barryvdh\DomPDF\ServiceProvider::class,

],

'aliases' => [

	....

	'PDF' => Barryvdh\DomPDF\Facade::class,

],

Execute the following command to publish the assets from vendor.

php artisan vendor:publish

Make Routes

Open routes/web.php and insert the following code. It will create a relation between the controller & the view over and above that it will also invoke the pdf download.

<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PdfController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('generate-pdf', [PdfController::class, 'generatePdf']);

Create Controller

Please create a new controller, and It will have the logic to display the user’s list. Run the command to create the controller.

php artisan make:controller PdfController

Open app/Http/Controllers/PdfController.php and add the following code.

namespace App\Http\Controllers;


use App\Http\Requests;
use Illuminate\Http\Request;
use DB;
use PDF;

class PdfController extends Controller
{

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function generatePdf(Request $request)
    {
        $data['titile'] = "Welcome to codingdriver.com";
        $data['content'] = "This is content"; 
   
        $pdf = PDF::loadView('dompdf', $data);
      
       return $pdf->download('codingdriver.pdf');
    }


        return view('dompdf');
    }
}

Create View File

Now, navigate the resources/views directory and create a dompdf.blade.php file. After that open the resources/views/dompdf.blade.php file and update the following code on it.

<!DOCTYPE html>
<html>
<head>
	<title>Hi</title>
</head>
<body>
	
<h1>{{ $title }}</h1>
<p>{{ $content }}</p>
<p>   Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
   tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
   quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
   consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
   cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
   proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
</body>
</html>

Start Laravel Application

Run the below-mentioned command to run the Laravel PDF project.

php artisan serve

Explore app on http://127.0.0.1:8000/generate-pdf

The Laravel 10 Generate Pdf from HTML View Using DomPdf tutorial is over now. Feel free to contact us if you have any issue

Leave a Comment