We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 7
Laravel 9 PDF: How to Create PDF in Laravel Step by
Step
by Mrunal-Fany 282022
Greetings, Web Developers, In today’s AppDividend Tutorial, I have shown the code of How
‘to generate pdf in laravel.
If you want to know the basic laravel functionality, check out my other article, Laravel Crud
Example From Scratch.
Generate pdf in laravel 0
First, we will install the laravel-dompdf, and then we will start our example.
Step: 1 Download the laravel-dompdf package through
composer.
conposer sequise barryvdh/laravel -dospdt
Step: 2 We need to configure this package in our laravel
application.
‘So 90 to the config >> app.php and add the following configuration,
providers’ => [
Barryvdh\DosPOF\ServiceProvider::class,
L
aliases’ =>
“POF* => Barryvch\DonPOF\Facade: :elass,
L
Here we are registering the PDF register provider for our application and also set an alias for it. So
when we need to generate PDF, we need to include It in our namespace like,
‘Then, we instantiate the POF class and use its API to further enhancing the generated PDF fe.
Step: 3 Create a master blade file.
<
Gyield( content’)
Step: 4 Create a form blade file to input the data.
clo form. blade. php
Gextends(‘master')
@section(” content")
fora method="post" actior
((esrf_f42140))}
form-group">
label for="city id" class:
“input type="text” classe"form-control” id="eity id" nase="city” placcholder="Saallvi
‘control -label">¢ity
@endsectionStep: 5 Add the routes for our application.
U/ web. php
$request->get(“full_nane"),
‘street_address* => Srequest-rget(‘street_address'),
“eity! => Srequest->get ("city"),"2ip_code’ => Srequest->get('zip_code")
Ds
suser-rsave()s
return redirect("/index');
y
public function index(){
Susers = Userbetail: 2110);
return view(‘index', conpact(‘users'));
x
Step: 8 Update the web.php file to
ing page.
U/ web. php
Route: :get("/index',"UserbetailControllergindex");
Step: 9 Create a view file to display the data.
‘index.blade.php ==>
Gextends( master)
section( content)
table classe"table table-striped">
ethead>
ethorbe/th>
‘ethoFull Nasec/th>
cethoaddressc/th>
ethocittye/th>
ethozip Codec/th>
ethoactionesth
@foreach(Susers as $user)
eta ({$user-oid)}
‘tab ((Suser->full_nane))
{{Suser->street_address))
((Suser-deity)} >
{{Suser->zip_code}}
BendsectionStep: 10 Create a route for download the pdf file.
J feb. php
Route: :get('/downloadPDF/ {i¢)" , ‘UserDetailController@downloadPOF");
Step: 11 Update index.blade.php file.
ethead>
ethoipeth>
‘ethoFull Manec/th>
‘thoaddress
ethocityc/th>
cthonction
@foreach(Susers as Suser)
en
Ktdo({Suser-rid)
{{$user->ul]_nane))
tao {{$user->street_address))
tao {{$user-oeity)}
tao {{$user->2ip_code}}
ktdvea hoefe"{{actLon( "UserbetailControllensdownloadPOF", $user->id)}}">PDFE/arc/ta>
cre
Gendforeach
Gendsection
Step: 12 Create a pdf.blade.php file to design our pdf blade.
pdf. blade.php ~
-
‘ehem>
seneta chars:
etitler
oay>
table classe"table table-bordert
tre
ctd>
{{$user->full_rane)),
cite
etd
{{Suser->street_address))
te
ce
ctr
etd
{XS$user-peity)}
«ta
etd
{{$user->zip_code?}
ere
“ephta>
Step: 13 Write a controller function to download the PDF.
// UserDetoilcontrolter.php
$request->get("full_nase"),
‘street_address' > Srequest->get(‘street_address'),
edty! > Srequest->get('city"),
"zip_code" => Srequest->get(' zip_code)
Ds
Suser->save();
return redirect( /index');y
public function index(){
Susers = Userberail::a110)5
return view( index", conpact("users'));
y
public function downloadPOF ($id) (
Suser » UserDetail::Find(Si¢);
Saf = POF::loadView( ‘pdt, conpact(‘user*));
return $pdf->download( invoice. péf");
This was @ simple example of laravel pdf generation.
krunal
-Kruna athiya Is an Information Technology Engineer. By profsson, he Isa web evalope with knowledge of muttpla
backend platforms (e-9, PHP, Nodes, Python) and trantend Javasriptrameworts (9, Angulr, React, and We).
f Gin? Vo