@manzoorahmed You’d probably be better off creating a certificate using DOMPDF, like Cashier does for receipts. Then you could “design” your certificate as a Blade template that’s rendered to a PDF.
Generate certificate
Hi i'm tryin to generate dynamic certificates using intervention image package i make a Registration controller How can I get username Dynamically on certificate from database
public function certificate(Request $request ) {
$image = Image::make(public_path('frontend/img/CME CERTIFICATE .jpg'));
$name="Vishal Gupta";
imagettftext($image,50,0,365,420,$color,$font,$_POST['name']);
$image->text('Ahmed', 4050, 2250, function($font) {
$font->file(public_path('fonts/AGENCYR.TTF'));
$font->size(500);
$font->color('#EB2B33');
$font->align('center');
$font->valign('bottom');
$font->angle(0);
});
$image->save(public_path('frontend/img/username .jpg'));
Session::flash('success_message', 'Certificate Generate Successfully!');
return back();
// return view('frontend.certificate'); }
@martinbean Thank You if it is possible that i used image path in DOMPDF, and just username get from database
@Manzoorahmed why do you want to use an image? Creating a html page with laravel is generally a lot easier
@Sinnbeck Thank You please share any example that i generate dynamically username on certificate from database
@Manzoorahmed https://github.com/barryvdh/laravel-dompdf
$data['user'] = User::find(2);
$pdf = Pdf::loadView('pdf.cert', $data);
return $pdf->download('cert.pdf');
And in that pdf/cert.blade.php
{{$user->name}}
@Sinnbeck Thank You so much
@Sinnbeck public function generate_certificate(Request $request) { $registrations['registrations'] = Registration::find(2);
if ($request->has('download')) {
$pdf = PDF::loadView('admin.registrations.generate_certificate',$registrations);
$pdf->setPaper('A4', 'landscape');
return $pdf->stream('generate_certificate.pdf');
}
return view('admin.registrations.generate_certificate');
// $data['registrations'] = Registration::find(2); // $pdf = Pdf::loadView('generate_certificate', $data); // return $pdf->download('generate_certificate.pdf'); }
i have create this function but it will return the whole user record on certificate i need separate certificate for each user @foreach ($registrations as $registration) {{ $registration->first_name }} @endforeach
@Manzoorahmed You need to make multiple certificates? Then you need to write code that creates them on disk, and adds them to a zip file. You cannot return multiple files as a single download
@Sinnbeck ok Thank You
@Sinnbeck i just want to show unique name on certificate user name is showing on certificate but it get all user record on one certificate i just need unique name on certificate according to user registration i place this code in my blade file but it will retrieved the whole user record that exist in database {{ $registration->name }}
@Manzoorahmed How can it get all users on 1 certificate if you are using User::find() ? Can you show some code?
@Sinnbeck public function generate_certificate(Request $request) { $registrations = DB::table("registrations")->get(); view()->share('registrations', $registrations);
if ($request->has('download')) {
$pdf = PDF::loadView('admin.registrations.generate_certificate');
$pdf->setPaper('A4', 'landscape');
return $pdf->stream('generate_certificate.pdf');
}
return view('admin.registrations.generate_certificate');
// $data['registrations'] = Registration::findOrFail($id); // $pdf = Pdf::loadView('generate_certificate', $data); // return $pdf->download('generate_certificate.pdf'); }
@Manzoorahmed You are not passing any data to the view? Also please format your code. https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax
i want to show unique user on each certificate
@Sinnbeck before i used this one but it will not work $data['registrations'] = Registration::findOrFail(); $pdf = Pdf::loadView('generate_certificate', $data); return $pdf->download('generate_certificate.pdf');
@Manzoorahmed Not sure what to do with "it will not work". And you are only generating 1 certificate here I assume? So pass in whatever data you need to the view. Start by designing the page without PDF.. This is a basic blade view
@Sinnbeck i design certificate its work but username that i get from databse alluser record show in one certificate i need a unique certificate for each user
@manzoorahmed Can you format your code properly? It’s hard to read when some lines are formatted and others aren’t.
@martinbean i Want to display only single value in blade but it show multiple row value on one certificate how can i get only single value in blade
@Manzoorahmed show the code. I have already shown you but apparently you are doing something else
public function generate_certificate(Request $request) { $registrations = DB::table('registrations')->find(3); // $registrations = DB::table("registrations")->get(); view()->share('registrations', $registrations);
if ($request->has('download')) {
$pdf = PDF::loadView('admin.registrations.generate_certificate');
$pdf->setPaper('A4', 'landscape');
return $pdf->stream('generate_certificate.pdf');
}
return view('admin.registrations.generate_certificate');
@Sinnbeck Thank you for your support i just want to display single record value on each certificate but it show multiple username on each certificate
@Sinnbeck I used this one but it will not work Could You please check it
$registrations['registrations'] = Registration::find(2); $pdf = Pdf::loadView('pdf.generate_certificate', $registrations); return $pdf->download('generate_certificate.pdf');
@Manzoorahmed show the blade view where you show the user. And please format the code
@Sinnbeck Sure i'll share
@Sinnbeck Thank You ill try to format code in proper way
@Manzoorahmed Where is it ?
@Sinnbeck sir i paste code here but it will not show when i post
@Manzoorahmed That is because you arent formatting it correctly. Add ``` on the line before and after the code (not on the same line)
<div class="cert-container print-m-0">
<div id="content2" class="cert">
<img
src= "{{ asset('frontend/img/CME CERTIFICATE (1).png')}}"
class="cert-bg"
alt=""
/>
@foreach ($registrations as $registration)
<div class="cert-content">
<h1 class="other-font">Certificate of Completion</h1>
<span style="font-size: 40px;">{{ $registration->first_name }}</span>
<br/><br/><br/><br/>
<span class="other-font"
><i><b>has completed the</b></i></span
>
<br/>
<span style="font-size: 40px;"><b>Advance Wound Care Course</b></span>
<br/>
<small>(Component 2)</small>
<br/><br/><br/><br/>
</div>
@endforeach
</div>
</div>
@Sinnbeck I have shared b0th controller and blade code when You get a chance please have a look
public function generate_certificate(Request $request)
{
{
$registrations['registrations'] = Registration::find(2);
$pdf = Pdf::loadView('admin.registrations.generate_certificate', $registrations);
return $pdf->download('generate_certificate.pdf');
}
@Manzoorahmed Well you are doing a foreach look, and thereby adding more than one..
@foreach ($registrations as $registration)
In the controller
$data['registration'] = Registration::find(2);
$pdf = Pdf::loadView('admin.registrations.generate_certificate', $data);
and blade (not foreach!)
<div class="cert-content">
<h1 class="other-font">Certificate of Completion</h1>
<span style="font-size: 40px;">{{ $registration->first_name }}</span>
<br/><br/><br/><br/>
<span class="other-font"
><i><b>has completed the</b></i></span
>
<br/>
<span style="font-size: 40px;"><b>Advance Wound Care Course</b></span>
<br/>
<small>(Component 2)</small>
<br/><br/><br/><br/>
</div>
@Sinnbeck i removed foreacch loop but it will show error Trying to get property 'first_name' of non-object (View: C:\xampp\htdocs\new-temp\resources\views\admin\registrations\generate_certificate.blade.php)
@Manzoorahmed And you changed the controller as well?
What is the output of @dd($registration)
@Sinnbeck This one is my controller i store data in registrationcontroller and i create a certificate generate function in this controller
public function store(Request $request)
{
// dd($request);
$this->validate($request, [
// 'name' => 'required|unique:registrations,name|max:255',
'email' => 'required|unique:registrations,email|max:255',
'course' => 'required|unique:registrations,course|max:255',
'first_name' => 'required|unique:registrations,first_name|max:255',
'last_name' => 'required|unique:registrations,last_name|max:255',
// 'reference' => 'required|unique:registrations,reference|max:255',
'date' => 'required:registrations,date|max:255',
'genders' => ['required', 'in:male,female'],
'cc_email' => 'required|unique:registrations,cc_email|max:255',
'phone_no' => 'required|unique:registrations,phone_no|numeric|max:255',
'id_passport' => 'required|unique:registrations,id_passport|max:255',
'professional_board' => 'required|unique:registrations,professional_board|max:255',
'facility' => 'required|unique:registrations,facility|max:255',
'experience' => 'required|unique:registrations,experience|max:255',
'country' => 'required:registrations,country|max:255',
'image' => 'required|image|mimes:jpeg,png,jpg'
]);
$input = $request->all();
$registration = new Registration();
// $registration->name = $input['name'];
$registration->email = $input['email'];
$registration->course = $input['course'];
$registration->first_name = $input['first_name'];
$registration->last_name = $input['last_name'];
$registration->genders = $input['genders'];
$registration->reference = date("Y") . rand(100, 999);
$registration->date = $input['date'];
$registration->phone_no = $input['phone_no'];
$registration->id_passport = $input['id_passport'];
$registration->professional_board = $input['professional_board'];
$registration->cc_email = $input['cc_email'];
$registration->facility = $input['facility'];
$registration->experience = $input['experience'];
$registration->country = $input['country'];
// $registration->slug = $this->createSlug($request->input('name'));
if ($request->hasFile('image')) {
if ($request->file('image')->isValid()) {
$this->validate($request, [
'image' => '|image|mimes:jpeg,png,jpg'
]);
$file = $request->file('image');
$destinationPath = "uploads/images";
$extension = $file->getClientOriginalExtension();
$fileName = $file->getClientOriginalName();
$fileName = rand() . $fileName;
//renaming image
$request->file('image')->move($destinationPath, $fileName);
$registration->image = $fileName;
$registration->save();
}
}
$registration->save();
Session::flash('success_message', 'Great! Registrations has been saved successfully!');
return redirect()->back();
}
public function generate_certificate(Request $request)
{
{
$registrations['registration'] = Registration::find(2);
$pdf = Pdf::loadView('admin.registrations.generate_certificate', $registrations);
return $pdf->stream('generate_certificate.pdf');
}
}
@Sinnbeck Please have a look code i'll try but unable to solve the error
@Sinnbeck its not showing any output
<div class="cert-content">
<h1 class="other-font">Certificate of Completion</h1>
<span style="font-size: 40px;">{{ $registrations->first_name }}</span>
@dd($registrations)
<br/><br/><br/><br/>
<span class="other-font"
><i><b>has completed the</b></i></span
>
<br/>
<span style="font-size: 40px;"><b>Advance Wound Care Course</b></span>
<br/>
<small>(Component 2)</small>
<br/><br/><br/><br/>
</div>
@Sinnbeck Undefined variable: registrations (View: C:\xampp\htdocs\new-temp\resources\views\admin\registrations\generate_certificate.blade.php)
@Manzoorahmed is it called registration or registrations? My examples use registrations, but you seem to use both at random?
@Sinnbeck please check this one
$registrations['registration'] = Registration::find(2);
$pdf = Pdf::loadView('admin.registrations.generate_certificate', $registrations);
if ($request->has('download')) {
$pdf->setPaper('A4', 'landscape');
return $pdf->stream('pdfview.pdf');
}
return view('admin.registrations.generate_certificate');
}
@Sinnbeck if i used $registration it will show this error
ErrorException
Trying to get property 'first_name' of non-object (View: C:\xampp\htdocs\new-temp\resources\views\admin\registrations\generate_certificate.blade.php)
http://127.0.0.1:8000/admin/generate_certificate
@Manzoorahmed I think my syntax might have confused you. Use this syntax if you better understand it
$pdf = Pdf::loadView('admin.registrations.generate_certificate', ['registration' => Registration::find(2));
And add @dd() before $registration->first_name. Notice I wrote registration not registrations
@Sinnbeck i debug blade code it show null on output instead of name
@Manzoorahmed so there is no registration in the database with id = 2 it seems
@Sinnbeck yesteday i shared a controller function that i used for registration stored in db its working fine please have a look
@Manzoorahmed ok don't know how to help you then. Start by making a completely normal view, and once that is working look into pdf. Passing a variable is basic laravel :)
@Sinnbeck I'm facing this issue
ErrorException
Trying to get property 'first_name' of non-object (View: C:\xampp\htdocs\new-temp\resources\views\admin\registrations\generate_certificate.blade.php)
@Sinnbeck Now i'm facing this issue I dod some changes in blade
Using $this when not in object context
<div class="cert-content">
<h1 class="other-font">Certificate of Completion</h1>
<span style="font-size: 40px;"> {{ $registration['first_name'] }}</span>
<br/><br/><br/><br/>
<span class="other-font"
><i><b>has completed the</b></i></span
>
<br/>
<span style="font-size: 40px;"><b>Advance Wound Care Course</b></span>
<br/>
<small>(Component 2)</small>
<br/><br/><br/><br/>
</div>
This code is quite thick, my friend. What is the problem with this code that Mr @Sinnbeck wrote?
You save variable information such as name, surname, etc. through the form in the database and this information is placed on the pre-designed certificate and the user downloads her certificate.
@LoverToHelp yes i create controller and store user information now i want to use that variable to generate certificate for user but i'm facing issue in my blade i did some changes in code and now this error will come
Using $this when not in object context
@LoverToHelp where i define this one in model
no add it controller above your public Function . First, see what this word is for and define it consistently
@LoverToHelp ok let me try
@Manzoorahmed What is the next error it gives?
@LoverToHelp like that but it show error
private static $this =" '';
public function generate_certificate(Request $request)
{
$pdf = Pdf::loadView('admin.registrations.generate_certificate', ['registration' => Registration::find(1)]);
if ($request->has('download')) {
$pdf->setPaper('A4', 'landscape');
return $pdf->stream('pdfview.pdf');
}
return view('admin.registrations.generate_certificate');
}
@LoverToHelp this error should came in model please have a look Registration model code
Using $this when not in object context
App\Models\Registration::find
C:\xampp\htdocs\new-temp\app\Models\Registration.php:55
// protected $hidden = [
// 'email',
// 'remember_token',
// ];
// /**
// * The attributes that should be cast.
// *
// * @var array<string, string>
// */
// protected $casts = [
// 'email_verified_at' => 'datetime',
// ];
public function collection()
{
return Registration::all();
}
public function find()
{
return $this->belongsTo(Registration::class);
}
public function Event()
{
return $this->belongsTo(Event::class);
}
}
@Manzoorahmed Don't listen to @lovertohelp, he has no clue what he is doing here.
$this is a reserverd variable name, that is the current instance of the object. It should never be defined as he told showed you.
@Tray2 It is better in life to learn to solve problems in a beautiful way and away from destroying others. Not everyone was a professor from the beginning.
@Manzoorahmed What exactly is in your controller and model? please show
@Manzoorahmed Can I suggest you watch this before going further. It is important to understand basic laravel, like how to pass data to a view, before you attempt something more complex.
@Sinnbeck sure but i'm facing issue in model i shared code here please check it
@Manzoorahmed As I said, ignore pdf completely for now. Just get it working as a 100% regular view
@LoverToHelp this one is controller code
public function store(Request $request)
{
// dd($request);
$this->validate($request, [
// 'name' => 'required|unique:registrations,name|max:255',
'email' => 'required|unique:registrations,email|max:255',
'course' => 'required|unique:registrations,course|max:255',
'first_name' => 'required|unique:registrations,first_name|max:255',
'last_name' => 'required|unique:registrations,last_name|max:255',
// 'reference' => 'required|unique:registrations,reference|max:255',
'date' => 'required:registrations,date|max:255',
'genders' => ['required', 'in:male,female'],
'cc_email' => 'required|unique:registrations,cc_email|max:255',
'phone_no' => 'required|unique:registrations,phone_no|numeric|max:255',
'id_passport' => 'required|unique:registrations,id_passport|max:255',
'professional_board' => 'required|unique:registrations,professional_board|max:255',
'facility' => 'required|unique:registrations,facility|max:255',
'experience' => 'required|unique:registrations,experience|max:255',
'country' => 'required:registrations,country|max:255',
'image' => 'required|image|mimes:jpeg,png,jpg'
]);
$input = $request->all();
$registration = new Registration();
// $registration->name = $input['name'];
$registration->email = $input['email'];
$registration->course = $input['course'];
$registration->first_name = $input['first_name'];
$registration->last_name = $input['last_name'];
$registration->genders = $input['genders'];
$registration->reference = date("Y") . rand(100, 999);
$registration->date = $input['date'];
$registration->phone_no = $input['phone_no'];
$registration->id_passport = $input['id_passport'];
$registration->professional_board = $input['professional_board'];
$registration->cc_email = $input['cc_email'];
$registration->facility = $input['facility'];
$registration->experience = $input['experience'];
$registration->country = $input['country'];
// $registration->slug = $this->createSlug($request->input('name'));
if ($request->hasFile('image')) {
if ($request->file('image')->isValid()) {
$this->validate($request, [
'image' => '|image|mimes:jpeg,png,jpg'
]);
$file = $request->file('image');
$destinationPath = "uploads/images";
$extension = $file->getClientOriginalExtension();
$fileName = $file->getClientOriginalName();
$fileName = rand() . $fileName;
//renaming image
$request->file('image')->move($destinationPath, $fileName);
$registration->image = $fileName;
$registration->save();
}
}
$registration->save();
Session::flash('success_message', 'Great! Registrations has been saved successfully!');
return redirect()->back();
}
public function generate_certificate(Request $request)
{
$pdf = Pdf::loadView('admin.registrations.generate_certificate', ['registration' => Registration::find(1)]);
if ($request->has('download')) {
$pdf->setPaper('A4', 'landscape');
return $pdf->stream('pdfview.pdf');
}
return view('admin.registrations.generate_certificate');
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
class Registration extends Model
{
protected $fillable = [
// 'id',
'reference',
'email',
'course',
'first_name',
'last_name',
'genders',
'image',
'date',
'cc_email',
'country',
'phone_no',
'id_passport',
'professional_board',
'facility',
];
// /**
// * The attributes that should be hidden for serialization.
// *
// * @var array<int, string>
// */
// protected $hidden = [
// 'email',
// 'remember_token',
// ];
// /**
// * The attributes that should be cast.
// *
// * @var array<string, string>
// */
// protected $casts = [
// 'email_verified_at' => 'datetime',
// ];
public function collection()
{
return Registration::all();
}
public function find()
{
return $this->belongsTo(Registration::class);
}
public function Event()
{
return $this->belongsTo(Event::class);
}
}
@Sinnbeck sir i checked its working the problem is in blade view when i used foreach loop then it show multiple data but when i removed foreach loop be because i need single value so it will not work it show error
public function collection()
{
return Registration::all();
}
is this in model is correct work ?
@LoverToHelp yes its working fine only error on generate certificate function i just add this function in model then it show this error #Error Using $this when not in object context http://127.0.0.1:8000/admin/generate_certificate
public function find()
{
return $this->belongsTo(Registration::class);
}
And where is it used? I could not see the connection problem. If it works properly without it, why should you write it?
@LoverToHelp its used generate certificate function
@Manzoorahmed I could not understand the problem either. I think that for your work, you only need to enter the user's name in the database and from that side the user will download the file and the page that will be displayed. I've made something like this before and it wasn't all that difficult. Always choose the easiest solution
@LoverToHelp sir as i shared before blade code when i used foreach loop it will work and return multiple username on single certificate that exists in database but i need separate certificate for each user so i retrieve a single record for each certificate so i removed foreach loop and jus used like that{{$registration - >first-name}} but it shoe error
@LoverToHelp could you share source code of this one
@Manzoorahmed Our team made it before. But I think that in the worst case, you should create the same thing with Photoshop and upload its pdf or image to the user. easy way ;)
@LoverToHelp i try this one before but how i take the username from databases to view it will not work
@Manzoorahmed its for you ;) https://postimg.cc/9wtfKYHW
@LoverToHelp this is just picture i need username that came from database
@Manzoorahmed its better . if you want take from database you should make html certificate and then use from database
<select>select user</select> for store in database
then
@foreach($user as $item)
{{ $item->name}}
@endforeach
hello / its OK?
@LoverToHelp no it will not work
Do not focus on solvable problems. Website design is not the work of one person. It requires a ui ux team.
Solve just add this code
$registration = DB::table('registrations')->find($id);
return view('admin.registrations.generate_certificate', compact('registration'));
Please or to participate in this conversation.