'Export PDF(DomPdf) properly for a show blade on Laravel based by id
I've been trying to export my HTML view into a PDF when a button is clicked and all is good for a normal let's say index blad as I've seen through some tutorials but for a show blade where the information of each post(in my case I named it patient) which are separated by id isn't working, I get several different errors for different ways I tried it and this is the closes I got to
This is my controller for my posts(named patient for me):
public function show($id)
{
$patient = Patient::find($id);
return view('patients.show')->with('patient', $patient);
}
It shows all the posts details for the specific post and this is the PDF function inside the same controller
public function createPDF($id){
$patient = Patient::find($id);
$object = Patient::where('id',$id)->get();
$data =[
'id' => $id,
'object' => $object,
];
$pdf = PDF::loadView('patients.show')->with('patient', $patient); //load view page
return $pdf->download('test.pdf')->with('patient', $patient); // download pdf file
}
I was trying different ways to fetch them but was unsuccessful
And this is the blade I created for the HTML to be converted into PDF(saw in a tutorial its better done this way to remove the elements we don't want to show in the pdf file such as the navigation or footer)
@extends('layouts.app')
@section('content')
<div >
<div
<h3 >Applicant Information</h3>
<p >{{$patient->title}}</p>
</div>
<div >
<dl>
<div>
<dt >Full name</dt>
<dd>{{$patient->name}}</dd>
</div>
<div >
<dt >Date of birth</dt>
<dd >{{$patient->dateofbirth}}</dd>
</div>
<div >
<dt >Gender</dt>
<dd >{{$patient->gender}}</dd>
</div>
<div >
<dt >Day of Control</dt>
<dd >{{$patient->dayofcontrol}}</dd>
</div>
<div >
<dt >Condition</dt>
<dd >{{$patient->condition}}</dd>
</div>
<div >
<dt >Description</dt>
<dd >{{$patient->description}}</dd>
</div>
<div >
<dt >Prescription</dt>
<dd >{{$patient->prescription}}</dd>
</div>
<div >
<dt >Attachments</dt>
<dd >
<ul role="list" >
<li >
<div >
<!-- Heroicon name: solid/paper-clip -->
<span> {{$patient->name}}.pdf </span>
</div>
<div >
<a href="#" > Download as PDF</a>
</div>
</li>
</ul>
</dd>
</div>
</dl>
</div>
</div>
</div>
@endsection
and the button I used on the show blade to route it
<a href="{{ URL::to('/patients/8/pdf', $patient) }}" class="font-medium text-indigo-600 hover:text-indigo-500"> Download as PDF</a>
I tried to make the id dynamic and such but it wasn't fetching it so I was trying to do it hardcoded just for testing
and these are my routes:
//Routes for patients
Route::resource('patients', 'PatientsController');
//Routes for PDF Convertion
Route::get('/patients/{id}/pdf', [PatientsController::class, 'createPDF']);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
