Laravel 10 PDF Export Example With DomPDF

  • 01-03-2023
  • 3689
  • Laravel 10
  • Haresh Chauhan

Watch Youtube Video (HINDI) :

Hello Developers,

We will integrate dompdf in laravel 10 in this post step by step from scratch, dompdf is very easy and simple to integrate. Here you just need to press a few commands and up to setting your pdf view using blade file that's it. dompdf we must need while we work on the e-commerce site you want to generate an invoice.

dompdf to generate pdf files you must need to learn if you are a beginner or fresher. This post definitely will help you from scratch installing a project to generate or download a pdf file from. In this Pdf generate example tutorial we will see pdf laravel 10 generate.

Dompdf is a popular HTML-to-PDF conversion library for Laravel. It can be used with Laravel by installing the dompdf/dompdf package via composer. Once it is installed it can be used by passing an HTML string in the param or you can render a full HTML file. You can also render and add a blade template in the Dompdf class.

The PDf generated by you, can download, save, and stored in the database also, in additional, the DomPdf also provides a helper function for generating PDF files directly from a URL or a file.

Laravel 10 PDF Export Example With DomPDF Steps :

  • Step 1. Install Laravel 10 Project.
  • Step 2. DOM PDF Composer Install.
  • Step 3. DomPdf Composer Install.
  • Step 4. Create Dummy User Data.
  • Step 5. Create PDFController.
  • Step 6. Define Route.
  • Step 7. Create Blade File.
  • Step 8. Start Server.

Step 1. Install Laravel 10 Project

Will start by cloning the new project, using the command given, and installing a new fresh laravel project, skip this step if you already installed it. Make sure you updated your php version to 8. In laravel 10 php 8 must require.

composer create-project laravel/laravel example-app

Step 2. DOM PDF Composer Install

Configure database credentials, open your phpMyAdmin, and create a database, now open your project ".env" file. Add your database login details.

.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=

After that, migrate your migration, this below command will migrate your migrations in the database, and you will create some default tables in the database.

php artisan migrate

Step 3. DomPdf Composer Install

Install the "barryvdh/laravel-dompdf" composer, this composer will install the core library file in our application, it may take some time to install, use the below command in your app terminal and install.

composer require barryvdh/laravel-dompdf

We will generate a default dompdf configuration file. Copy the below command and paste it into your terminal. This command will generate a default configured dompdf setting file in the config.

php artisan vendor:publish --provider="Barryvdh\DomPDF\ServiceProvider"

Step 4. Create Dummy User Data

We will create the Dummy user's data using the laravel factory, copy the below command and paste it into your terminal project root. After that, you need to paste the user factory code.

php artisan tinker

Paste this code in your "tinker", and hit enter, this will create the user's record in your database. this user's data is just for demonstration purposes only.

User::factory(10)->create();

Step 5. Create PDFController

After the user dummy data is created, we will create a controller, use the below command and create a PDFController.

php artisan make:controller PDFController

Now open your controller, and paste the given controller code into your PDFController. Here you can stream pdf or you can also download pdf files.

App\Http\Controllers\PDFController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;
use PDF;

class PDFController extends Controller
{
    public function generatePDF()
    {
        $users = User::get();

        $data = [
            'title' => 'Welcome to Webappfix',
            'date'  => date('m/d/Y'),
            'users' => $users
        ];

        $pdf = PDF::loadView('myPDF',$data);

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

        // return $pdf->stream('webappfix.pdf');
    }
}

Step 6. Define Route

We will define a route for the redirect to the controller method. copy the below route and paste into your web.php file.

routes\web.php
use App\Http\Controllers\PDFController;

Route::get('generate-pdf',[PDFController::class,'generatePDF']);

Step 7. Create Blade File

This is the blade file for the pdf print view, Copy this blade file and paste it into your myPDF.blade.php file. Goto below the given path and create myPDF.blade.php name file.

resources\views\myPDF.blade.php
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Laravel 10 Generate PDF Example - Webappfix</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>
<body>
    <h1>{{ $title }}</h1>
    <p>{{ $date }}</p>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
    tempor incididunt ut labore et dolore magna aliqua.</p>

    <table class="table table-bordered">
        <thead>
            <tr>
                <th>ID</th>
                <th>Name</th>
                <th>Email</th>
            </tr>
        </thead>
        <tbody>	
            @foreach($users as $user)
            <tr>
                <td>{{ $user->id }}</td>
                <td>{{ $user->name }}</td>
                <td>{{ $user->email }}</td>
            </tr>
            @endforeach
        </tbody>
    </table>
</body>
</html>

Step 8. Start Server

Use the below command and start your local development server, skip this step if already set up and upload your project on the server.

php artisan server

Now open your browser, copy the given URL, and paste it into your browser. you will return and download a pdf file. That's it, I hope you guys enjoy this tutorial and learn something new by following this post. we also provide youtube video, you can follow this video also for more practice.

http://127.0.0.1:8000/generate-pdf

We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :