Raffales-LMS/app/Http/Controllers/PdfController.php

295 lines
14 KiB
PHP
Raw Normal View History

2024-04-16 09:58:24 +00:00
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Models\Campaigns;
use App\Models\Countries;
use App\Models\Registrations;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use setasign\Fpdi\Fpdi;
use Intervention\Image\Facades\Image;
use Endroid\QrCode\Color\Color;
use Endroid\QrCode\Encoding\Encoding;
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelLow;
use Endroid\QrCode\QrCode;
use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin;
use Endroid\QrCode\Writer\PngWriter;
class PdfController extends Controller
{
public function generateMultipagePdf(Request $request, $ids)
{
$registration_ids = $ids;
$ids = explode(",", $ids);
foreach ($ids as $id) {
$data = Registrations::findOrFail($id);
$name = slugify($data->name);
$pdfTemplatePath = public_path(env("CLIENT_PATH") . '/form.pdf');
$filledPdfPath = public_path(env("CLIENT_PATH") . '/generated/' . $name . '-registration-form-' . $id . '.pdf');
if (!file_exists($filledPdfPath)) {
$pdf = new Fpdi();
// Add a page
$pdf->AddPage();
// Set the template file
$pdf->setSourceFile($pdfTemplatePath);
$tplIdx = $pdf->importPage(1);
// Use the template
$pdf->useTemplate($tplIdx);
// Set form field values based on your data
$pdf->SetFont('Arial');
$pdf->SetXY(102, 7);
$pdf->Write(0, $data->name);
$pdf->SetFont('Arial');
$pdf->SetFontSize('7');
$pdf->SetXY(102, 22);
$pdf->Write(0, $data->email);
$pdf->SetXY(162, 22);
$pdf->Write(0, $data->mobile);
$pdf->SetXY(118, 34);
$pdf->Write(0, $data->highest_qualification);
$pdf->SetXY(177, 34);
$pdf->Write(0, ($data->highest_grade) ? $data->highest_grade : "");
$pdf->SetXY(108, 46);
$pdf->Write(0, ($data->preparation_class) ? $data->preparation_class : "");
$pdf->SetXY(177, 46);
$pdf->Write(0, ($data->preparation_score) ? $data->preparation_score : "");
$pdf->SetXY(118, 60);
$pdf->Write(0, ($data->intrested_for_country) ? $data->intrested_for_country : "AAA");
$pdf->SetXY(68, 25);
$pdf->SetFontSize('14');
$pdf->Write(0, ($data->registration_id) ? $data->registration_id : "AAA");
// Repeat the above steps for other form fields
// Output or download the filled PDF
$pdf->Output($filledPdfPath, 'F'); //D for Download
}
}
//return response()->file($filledPdfPath)->deleteFileAfterSend(true);
}
public function generatePdf(Request $request)
{
$registration_id = $request->query('id');
$data = Registrations::findOrFail($registration_id);
$name = slugify($data->name);
$pdfTemplatePath = public_path(env("CLIENT_PATH") . '/inquiry-form-org.pdf');
$filledPdfPath = public_path(env("CLIENT_PATH") . '/generated/' . $name . '-enquiry-form-' . $registration_id . '.pdf');
if (!file_exists($filledPdfPath)) {
$pdf = new Fpdi();
// $pdf->SetMargins(float $left, float $top, float $right = -1, bool $resetPage = true);
$pdf->SetMargins(0,0,0);
$pdf->SetAutoPageBreak(false);
$pdf->AddPage('potrait', 'A5');
$border=0;
$pdf->setSourceFile($pdfTemplatePath);
$tplIdx = $pdf->importPage(1);
$pdf->useTemplate($tplIdx);
$pdf->SetFont('Arial');
$pdf->SetFontSize('7');
$pdf->SetXY(13, 32);
$pdf->Cell(13, 3, "Registration ID: " . (($data->registration_id) ? $data->registration_id : "ID"), $border, 1, "L");
$pdf->SetXY(23, 41.5);
$pdf->Cell(113, 3, ($data->name) ? $data->name : "", $border, 1, "L");
$pdf->SetXY(30, 46.5);
$pdf->Cell(34, 3, ($data->dob) ? $data->dob : "", $border, 1, "L");
$pdf->SetXY(77, 46.5);
$pdf->Cell(15, 3, ($data->gender) ? $data->gender : "", $border, 1, "L");
$pdf->SetXY(111, 46.5);
$pdf->Cell(25, 3, ($data->marital_status) ? $data->marital_status : "", $border, 1, "L");
$pdf->SetXY(39, 52);
$pdf->Cell(37, 3, ($data->mobile) ? $data->mobile : "", $border, 1, "L");
$pdf->SetXY(88, 52);
$pdf->Cell(48, 3, ($data->email) ? $data->email : "", $border, 1, "L");
$pdf->SetXY(26, 57.7);
$pdf->Cell(110, 3, ($data->address) ? $data->address : "", $border, 1, "L");
$pdf->SetXY(37, 63);
$pdf->Cell(56, 3, ($data->guardian_name) ? $data->guardian_name : "", $border, 1, "L");
$pdf->SetXY(106, 63);
$pdf->Cell(30, 3, ($data->phone) ? $data->phone : "", $border, 1, "L");
$pdf->SetXY(53, 69);
$pdf->Cell(83, 3, ($data->applied_before) ? $data->applied_before : "", $border, 1, "L");
$pdf->SetXY(96, 78);
$pdf->Cell(40, 3, $data->highest_qualification, $border, 1, "R"); //$pdf->Cell(float $w, float $h = 0, string $txt = '', mixed $border = 0, int $ln = 0, string $align = '', bool $fill = false, mixed $link = '')
$pdf->SetXY(41, 86.5);
$pdf->Cell(28, 3, ($data->see_stream) ? $data->see_stream : "", $border, 1, "C");
$pdf->SetXY(72, 86.5);
$pdf->Cell(28, 3, ($data->see_school) ? $data->see_school : "", $border, 1, "C");
$pdf->SetXY(102, 86.5);
$pdf->Cell(16.5, 3, ($data->see_grade) ? $data->see_grade : "", $border, 1, "C");
$pdf->SetXY(120.5, 86.5);
$pdf->Cell(15, 3, ($data->see_year) ? $data->see_year : "", $border, 1, "C");
$lineY=91.5;
$pdf->SetXY(41, $lineY);
$pdf->Cell(28, 3, ($data->plus2_stream) ? $data->plus2_stream : "", $border, 1, "C");
$pdf->SetXY(72, $lineY);
$pdf->Cell(28, 3, ($data->plus2_college) ? $data->plus2_college : "", $border, 1, "C");
$pdf->SetXY(102, $lineY);
$pdf->Cell(16.5, 3, ($data->plus2_grade) ? $data->plus2_grade : "", $border, 1, "C");
$pdf->SetXY(120.5, $lineY);
$pdf->Cell(15, 3, ($data->plus2_year) ? $data->plus2_year : "", $border, 1, "C");
$lineY=97;
$pdf->SetXY(41, $lineY);
$pdf->Cell(28, 3, ($data->bachelors_stream) ? $data->bachelors_stream : "", $border, 1, "C");
$pdf->SetXY(72, $lineY);
$pdf->Cell(28, 3, ($data->bachelors_college) ? $data->bachelors_college : "", $border, 1, "C");
$pdf->SetXY(102, $lineY);
$pdf->Cell(16.5, 3, ($data->bachelors_grade) ? $data->bachelors_grade : "", $border, 1, "C");
$pdf->SetXY(120.5, $lineY);
$pdf->Cell(15, 3, ($data->bachelors_year) ? $data->bachelors_year : "", $border, 1, "C");
$lineY=102.5;
$pdf->SetXY(41, $lineY);
$pdf->Cell(28, 3, ($data->highest_stream) ? $data->highest_stream : "", $border, 1, "C");
$pdf->SetXY(72, $lineY);
$pdf->Cell(28, 3, ($data->highest_college) ? $data->highest_college : "", $border, 1, "C");
$pdf->SetXY(102, $lineY);
$pdf->Cell(16.5, 3, ($data->highest_grade) ? $data->highest_grade : "", $border, 1, "C");
$pdf->SetXY(120.5, $lineY);
$pdf->Cell(15, 3, ($data->highest_year) ? $data->highest_year : "", $border, 1, "C");
$pdf->SetXY(36, 109.7);
$pdf->Cell(100, 3, ($data->experience) ? $data->experience : "", $border, 1, "L");
$pdf->SetXY(44, 115.7);
$pdf->Cell(92, 3, ($data->intrested_for_country) ? Countries::where("country_id",$data->intrested_for_country)->first()->title : "", $border, 1, "L");
$pdf->SetXY(36, 123.7);
$pdf->Cell(29, 3, ($data->preparation_class) ? $data->preparation_class : "", $border, 1, "L");
$pdf->SetXY(80, 123.7);
$pdf->Cell(56, 3, ($data->preparation_score) ? $data->preparation_score : "", $border, 1, "L");
$pdf->SetXY(50, 133.5);
$pdf->Cell(38, 3, ($data->how_you_know) ? $data->how_you_know : "", $border, 1, "L");
$pdf->SetXY(105, 133.5);
$pdf->Cell(18, 3, ($data->reference) ? $data->reference : "", $border, 1, "L");
$pdf->SetXY(15, 196);
$pdf->Cell(25, 3, "", $border, 1, "L");
$pdf->SetXY(70, 196);
$pdf->Cell(21, 3, (($data->created_at) ? date('Y-m-d', strtotime($data->created_at)) : "date"), $border, 1, "C");
$pdf->SetXY(120, 196);
$pdf->Cell(25, 3, "", $border, 1, "L");
// $pdf->Cell(0, 10, json_encode($data), 0, 1, 'R');
//$pdf->Write(float $h, string $txt, mixed $link = '');
//$pdf->Cell(float $w, float $h = 0, string $txt = '', mixed $border = 0, int $ln = 0, string $align = '', bool $fill = false, mixed $link = '')
// $pdf->MultiCell(float $w, float $h, string $txt, mixed $border = 0, string $align = 'J', bool $fill = false);
// $pdf->MultiCell(120, 3, json_encode($data), 0, "R", false);
// Repeat the above steps for other form fields
// Output or download the filled PDF
$pdf->Output($filledPdfPath, 'D'); //D for Download
}
}
//return response()->file($filledPdfPath)->deleteFileAfterSend(true);
public function generatePass(Request $request)
{
$registration_id = $request->query('id');
$data = Registrations::findOrFail($registration_id);
$registration = DB::table("registrations")
->where('status', 1)
->where('registration_id', $registration_id)
->orderBy('display_order')
->first();
if (!$registration) {
// Handle the case where the registration is not found
return;
}
$Campaign = Campaigns::findOrFail($registration->campaigns_id);
$QRURL = route("registration.view", md5($registration_id));
$writer = new PngWriter();
$qrCode = QrCode::create($QRURL)
->setEncoding(new Encoding('UTF-8'))
->setErrorCorrectionLevel(new ErrorCorrectionLevelLow())
->setSize(150)
->setMargin(1)
->setRoundBlockSizeMode(new RoundBlockSizeModeMargin())
->setForegroundColor(new Color(0, 0, 0))
->setBackgroundColor(new Color(255, 255, 255));
$result = $writer->write($qrCode);
$dataUri = $result->getDataUri();
if ($Campaign->pass_layout != "") {
$pass_layout = storage_path(str_replace("storage/", "app/public/", $Campaign->pass_layout));
$backgroundImage = Image::make($pass_layout);
} else {
$backgroundImage = Image::make(public_path(env("CLIENT_PATH") . '/assets/images/pass.png'));
}
$qrCodeImage = Image::make($dataUri);
$qrCodePosition = ($Campaign->qr_position != "") ? $Campaign->qr_position : "100,100";
$qrCodePosition = explode(",", $qrCodePosition);
$backgroundImage->insert($qrCodeImage, 'top-left', $qrCodePosition[0], $qrCodePosition[1]);
$text = $registration->registration_id;
$idPosition = ($Campaign->id_position != "") ? $Campaign->id_position : "100,100";
$idPosition = explode(",", $idPosition);
$fontPath = public_path('assets/fonts/Poppins-Regular.ttf'); // Replace with the actual font path
$backgroundImage->text($text, $idPosition[0], $idPosition[1], function ($font) use ($fontPath) {
$font->file($fontPath);
$font->size(16);
$font->color('#000');
$font->align('left');
$font->valign('top');
});
$text = $registration->name;
$namePosition = ($Campaign->name_position != "") ? $Campaign->name_position : "100,100";
$namePosition = explode(",", $namePosition);
$fontPath = public_path('assets/fonts/Poppins-Regular.ttf'); // Replace with the actual font path
$backgroundImage->text($text, $namePosition[0], $namePosition[1], function ($font) use ($fontPath) {
$font->file($fontPath);
$font->size(24);
$font->color('#ffffff');
$font->align('left');
$font->valign('top');
});
$text = $registration->email;
$emailPosition = ($Campaign->email_position != "") ? $Campaign->email_position : "100,100";
$emailPosition = explode(",", $emailPosition);
$fontPath = public_path('assets/fonts/Poppins-Regular.ttf'); // Replace with the actual font path
$backgroundImage->text($text, $emailPosition[0], $emailPosition[1], function ($font) use ($fontPath) {
$font->file($fontPath);
$font->size(20);
$font->color('#fff');
$font->align('left');
$font->valign('top');
});
$outputPath = public_path(env("CLIENT_PATH") . '/assets/images/generated/pass_' . date("ymdhis") . '_image.png');
$backgroundImage->save($outputPath);
$qr_code=$outputPath;
$downloadFileName = env("CLIENT_PATH").'_pass.png';
return response()->download($qr_code, $downloadFileName);
}
}