How do I convert HTML data to PDF using Laravel PHP?

Published January 17, 2022

PDF generation from HTML is a commonly used functionality in web applications. In this post, we'll see how to do this using Laravel PHP.

In laravel, we usually generate PDF files using the barryvdh/laravel-dompdf laravel package. This package is easy to use and offers users several customization options.


Convert HTML to PDF with Laravel PHP

You can easily convert HTML view to PDF, just following the following simple steps:

·       Step 1: From a terminal or command prompt, create a Laravel project using composer

composer create-project laravel/laravel htmlpdf


Convert HTML to pdf laravel php


Create an application key using the command below once the project is created successfully.

php artisan key:generate


Step 2: Now, use the commands below to install the barryvdh/laravel-dompdf package

composer require barryvdh/laravel-dompdf



Convert HTML to pdf laravel php 2


Now that our barryvdh/laravel-dompdf package has been successfully installed, let's configure it.


Convert HTML to pdf laravel php 7


Step 3: Configure your barryvdh/laravel-dompdf package by adding the following code to the config/app.php file.

Add the following code in Providers


'providers' => [






Add the following code in aliases.

'aliases' => [


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




Convert HTML to pdf laravel php 3

Step 4:  You need to configure our PDF settings, including the paper size, DPI, font type, etc. To do so, run the following command, and it will create the config/dompdf.php file

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


Step 5: Now, create a PDF download route by pasting the code below into the routes/web.php file


Route::get('html-pdf', 'HTMLPDFController@htmlPdf')->name('htmlPdf');



Step 6: Now that the route has been created, we need to create a controller. Run the command: php artisan make:controller HTMLPDFController

This command will generate the app/Http/Controllers/HTMLPDFController.php

Convert HTML to pdf laravel php 4

Now open  the HTMLPDFController.php file and paste the following code below


Convert HTML to pdf laravel php 5



namespace App\Http\Controllers;


use PDF;

use Illuminate\Http\Request;


class HTMLPDFController extends Controller



     * generate PDF file from blade view.


     * @return \Illuminate\Http\Response


    public function htmlPdf()


        // selecting PDF view

        $pdf = PDF::loadView('htmlView');


        // download pdf file

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




Now, open app/Http/Controllers and add the following code:


 * The controller namespace for the application.


 * When present, controller route declarations will automatically be prefixed with this namespace.


 * @var string|null


protected $namespace = 'App\\Http\\Controllers';


Step 7: Finally, create the view file in the ressource/views/htmlPdf.blade.php and paste the code below. This view contains the HTML document that will be converted to PDF

<!DOCTYPE html>

<html lang="en-US">


        <meta charset="utf-8">

        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

        <meta name="viewport" content="width=device-width, initial-scale=1">

        <title>HTML 2 PDF</title>

        <style type="text/css">

            .center {

                text-align: center;





        <div class="center">

            <h1>This is heading</h1>


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




Convert HTML to pdf laravel php 6

We are done. You are now ready to run the php artisan server to run your application.

In this php laravel example we covered how to generate PDF from html content using laravel8


Download Source code


Article Contributed By :