How to Generate PDFs in Laravel 7 with DomPdf | Quick Guide

Laravel 7 PDF Generator DomPdf  | How to Export PDF in Laravel


Laravel 7 PDF Generator DomPdf  | How to Export PDF in Laravel


                    Hi , In this article we are going to see how can we use Laravel 7 pdf generator ,  we will see in detail that how can you convert a page data to pdf using Larave DomPdf package and ofcourse this package is absolutely free and very easy to use . So let's see step by step how can we use DomPdf. As we have given the title as Laravel 7 PDF generator , it does not mean that it will only work on Laravel 7 , You can follow the same process for Laravel 5 and laravel 6 .

                   We will follow these following steps throughout our article learn everything about Laravel Dompdf one by one ..


  1. Create Laravel 7 Project
  2. Install DomPdf package in your project
  3. Setup page to be Downloaded
  4. Setup Route and Controller
  5. Check Whether Working or Not

Step 1 : Create a Project :

         To create a project a use the following command..


composer create-project laravel/laravel project_name


Laravel 7 PDF Generator DomPdf  | How to Export PDF in Laravel


Step 2 : Install DomPdf package in your Project :


          Use the following command to install dompdf package in your project..



composer require barryvdh/laravel-dompdf

After using this command it might take a bit of time depending on your internet speed and install all dependencies of DomPdf on your project . Now we are ready to playwith DomPdf  and see how dompdf works as a laravel pdf generator .


Step 3 : Setup Page to be Downloaded :


                 Next step is that make your blade file or view page whose contents you want to be downloaded as pdf . so that whenever we visit that page it will show all the contents of that page and also download all the content available on that page in a PDF  format. 

For example i am creating page named demo.blade.php .. , and an already available page that is welcome.blade.php

demo.blade.php ( source code )



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
<!DOCTYPE html>
<html>
   <head>
      <title>Studywithkishan</title>
   </head>
   <body>
      <center>
         <h1>-- Content of this will be downloaded whenever this page loads --</h1>
      </center>
      {{-- sample texts --}}
      <p>
         Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
      </p>
      <p>
         Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.
         The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.
      </p>
   </body>
</html>


welcome.blade.php ( source code )



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<html lang="{{ str_replace('_', '-', app()-&gt;getLocale()) }}">
   <head>
      
      
      <title>Laravel</title>
      <!-- Fonts -->
      <link href="https://fonts.googleapis.com/css?family=Nunito:200,600" rel="stylesheet"></link>
      <!-- Styles -->
      <style>
         html, body {
         background-color: #fff;
         color: #636b6f;
         font-family: 'Nunito', sans-serif;
         font-weight: 200;
         height: 100vh;
         margin: 0;
         }
         .full-height {
         height: 100vh;
         }
         .flex-center {
         align-items: center;
         display: flex;
         justify-content: center;
         }
         .position-ref {
         position: relative;
         }
         .top-right {
         position: absolute;
         right: 10px;
         top: 18px;
         }
         .content {
         text-align: center;
         }
         .title {
         font-size: 84px;
         }
         .links > a {
         color: #636b6f;
         padding: 0 25px;
         font-size: 13px;
         font-weight: 600;
         letter-spacing: .1rem;
         text-decoration: none;
         text-transform: uppercase;
         }
         .m-b-md {
         margin-bottom: 30px;
         }
      </style>
   </head>
   <body>
      <div class="flex-center position-ref full-height">
         @if (Route::has('login'))
         <div class="top-right links">
            @auth
            <a href="https://www.blogger.com/%7B%7B%20url('/home')%20%7D%7D">Home</a>
            @else
            <a href="https://www.blogger.com/%7B%7B%20route('login')%20%7D%7D">Login</a>
            @if (Route::has('register'))
            <a href="https://www.blogger.com/%7B%7B%20route('register')%20%7D%7D">Register</a>
            @endif
            @endauth
         </div>
@endif
         <div class="content">
            <div class="title m-b-md">
               Laravel 7 PDF Generation using DomPdf
            </div>
<div class="links">
               <a href="https://laravel.com/docs">Docs</a>
               <a href="https://laracasts.com/">Laracasts</a>
               <a href="https://laravel-news.com/">News</a>
               <a href="https://blog.laravel.com/">Blog</a>
               <a href="https://nova.laravel.com/">Nova</a>
               <a href="https://forge.laravel.com/">Forge</a>
               <a href="https://vapor.laravel.com/">Vapor</a>
               <a href="https://github.com/laravel/laravel">GitHub</a>
            </div>
</div>
</div>
</body>
</html>

Step 4 : Setup Route and Controller :

       Setup your route and use the following two line of codes to download the page as PDF .


$pdf = PDF::loadView('view_page_name');
return $pdf->download('any_name.pdf');


web.php ( route file and controller configuration ) :



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<?php
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
});
Route::get('/invoice',function(){
$pdf = PDF::loadView('demo');
return $pdf->download('invoice.pdf');
return view('demo');
});



              Now you are all ready , whenever you visit "  /invoice  "  route  , the content present in your demo.blade.php page will be downloaded in pdf format .

Laravel 7 PDF Generator DomPdf  | How to Export PDF in Laravel
----------   After downloaded ------------

Laravel 7 PDF Generator DomPdf  | How to Export PDF in Laravel

                             
                          Laravel 7 PDF Generator works as a great tool if you have to send any payment detail or invoice to your user and the interesting thing is it's very easy to use as you can see .

Thank you for giving your precious time for this article


Hope it Helped you -:)


Also Read --


  1. Laravel pagination 
  2. SweetAlert package in Laravel
  3. How to integrate VUE with Laravel
  4. Laravel File Upload
Previous Post Next Post

Contact Form