How to Generate PDF in Laravel 6 Example

  • 08-04-2022
  • 1783
  • Laravel 6
  • Haresh Chauhan

Watch Youtube Video (HINDI) :

Hello, Dear developers friend, In this post we will learn how to generate PDF in Laravel 6. So In this post, I will show you step by step what is the procedure of the Generate PDF in Laravel 6. If you know basic Laravel 6 then you can easily use it in your Laravel 6 Application.

In this post we will require to install the DOM PDF package to generate PDF in Laravel 6 Application. So, I will guide you on how we can install the package and how to use it in our Laravel 6 Application. As well as we will learn the DOM PDF functions how we can use it as per need in our Laravel 6 Application.

So, let's follow the steps and get PDF generating in the Laravel 6 Application.

How to Generate PDF in Laravel 6

Step 1 : Install Laravel 6 Application

In this step if you haven't Laravel 6 Application then you first download Fresh Laravel 6 Application. You can use below command and clone Laravel 6 Application.

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

Step 2 : Install laravel-dompdf package through composer.

After Clone and setup we need to install laravel-dompdf package. So, let's do this.

composer require barryvdh/laravel-dompdf

Laravel 6 Image Upload Tutorial

Step 3 : Configuration laravel-dompdf.

After installed laravel-dompdf package we require to the configuration of this package. So, Now Open your project config/app.php file and add the following configuration.

'providers' => [
    ....
    Barryvdh\DomPDF\ServiceProvider::class,
],
'aliases' => [
    ....
    'PDF' => Barryvdh\DomPDF\Facade::class,
],

Here, we are registred the PDF register provider for our application and also set an alias for it. So when we need to generate PDF file, we just need to include it in our namespace like below example.

use PDF;

If you getting pdf class not found error then you need to add this line in your controller and solve it.

Step 3 : Create UserController

Now, we make UserController following bellow command.

php artisan make:controller UserController

Now we write code into UserController file.

app/Http/Controllers/UserController.php

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use PDF;
use App\User;

class UserController extends Controller
{
    public function downloadPDF(){

      $user = User::get();

      $pdf = PDF::loadView('pdf', compact('user'));

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

    }
}

Step 4 : Create Routes

In 4th step we make one route download for PDF file. So, simply copy that routes and put your file.

routes/web.php

Route::get('/downloadPDF','UserController@downloadPDF');

JQuery - Add Edit & Delete HTML Table Row Example

Step 5 : Create pdf blade file.

In this step create one pdf.blade.php file for print data into pdf file. So, we create manually.

resources/views/pdf.blade.php

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <table class="table table-bordered">
      <tr>
        <td>
          {{$user->id}}
        </td>
        <td>
          {{$user->name}}
        </td>
      </tr>
      <tr>
        <td>
          {{$user->email}}
        </td>
        <td>
          {{$user->created_at}}
        </td>
      </tr>
    </table>
  </body>
</html>

Note: Before running this example we need to exists few records for print in PDF file form the users table. So record already exists then you don't need do anything else but in users table is empty then you need to addes fews record.

Now you are ready to run. So let's quick run.

php artisan serve

Now open your browser and run this url localhost:8000/downloadPDF.

laravel-dompdf More functions

You can use chain the methods:

return PDF::loadFile(public_path().'/file.html')->save('/path-to/my_file.pdf')->stream('downloads.pdf');

PDF::loadHTML($html)->setPaper('a4', 'landscape')->setWarnings(false)->save('downloads.pdf')

AngularJS Login Logout with Laravel 6

Tip: Page breaks

You can use the CSS page-break-before/page-break-after properties to create a new page.

<style>
.page-break {
    page-break-after: always;
}
</style>
<h1>Page 1</h1>
<div class="page-break"></div>
<h1>Page 2</h1>

How to get Current Date and Time in AngularJS?

Tip: UTF-8 support

In your templates, set the UTF-8 Metatag:

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

Configuration

You can use options as per your need.

PDF::setOptions(['dpi' => 150, 'defaultFont' => 'sans-serif']);

Available options

  • fontDir: "{app_directory}/storage/fonts/" (available in config/dompdf.php)
  • tempDir: "/tmp" (available in config/dompdf.php)
  • chroot: "{app_directory}" (available in config/dompdf.php)
  • defaultMediaType: "screen" (available in config/dompdf.php)
  • fontCache: "{app_directory}/storage/fonts/" (available in config/dompdf.php)
  • defaultPaperSize: "a4" (available in config/dompdf.php)
  • defaultFont: "serif" (available in config/dompdf.php)
  • rootDir: "{app_directory}/vendor/dompdf/dompdf"
  • dpi: 96 (available in config/dompdf.php)
  • logOutputFile: "/tmp/log.htm"
  • fontHeightRatio: 1.1 (available in config/dompdf.php)
  • isPhpEnabled: false (available in config/dompdf.php)
  • isJavascriptEnabled: true (available in config/dompdf.php)
  • isRemoteEnabled: true (available in config/dompdf.php)
  • isHtml5ParserEnabled: false (available in config/dompdf.php)
  • debugLayout: false
  • isFontSubsettingEnabled: false (available in config/dompdf.php)
  • debugPng: false
  • debugKeepTemp: false
  • debugLayoutInline: true
  • debugCss: false
  • debugLayoutLines: true
  • debugLayoutBlocks: true
  • debugLayoutPaddingBox: true
  • pdflibLicense: ""
  • adminUsername: "user"
  • adminPassword: "password"
  • pdfBackend: "CPDF" (available in config/dompdf.php)

PHP Laravel JQuery Form Validation Example


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 :