Level 1
return view('pdf_template', ['sections' => $sections]);
Try this.
i have one code where i upload the excel then download this excel on pdf but in this showing me error when i upload excel and click on submit
Undefined variable $sections
upload.blade.php
<form action="/process-excel" method="POST" enctype="multipart/form-data">
@csrf
<input type="file" name="excel_file">
<button type="submit">Upload and Generate PDFs</button>
</form>
pdf_template.blade.php
<body>
@foreach ($sections as $section)
<div class="container">
<div class="row">
@foreach ($section as $pdf)
<div class="col-md-3">
<div class="section">
<div class="image">
<img class="person-image" src="https://i.ibb.co/R4VhmBH/istockphoto-1393750072-612x612.jpg" />
</div>
<div class="user-data">
{!! $pdf->output() !!}
</div>
</div>
</div>
@endforeach
</div>
</div>
@endforeach
</body>
controller
public function processExcel(Request $request)
{
// Validate the uploaded file
$request->validate([
'excel_file' => 'required|mimes:xlsx',
]);
// Get the uploaded Excel file
$excelFile = $request->file('excel_file');
// Load the Excel data (you may need to adjust this based on your Excel structure)
$spreadsheet = IOFactory::load($excelFile);
$worksheet = $spreadsheet->getActiveSheet();
$data = $worksheet->toArray();
// Remove the header row if necessary
array_shift($data);
// Create an array to store PDFs and sections
$pdfs = [];
$sections = [];
// Iterate through the data and generate PDFs
foreach ($data as $index => $row) {
$pdf = PDF::loadView('pdf_template', [
'name' => $row[0],
'address' => $row[1],
'city' => $row[2],
'state' => $row[3],
// Add more data as needed
]);
// Generate PDF and add it to the array
$pdfs[] = $pdf;
// Group every 5 PDFs into a section
if (($index + 1) % 5 === 0) {
$sections[] = $pdfs;
$pdfs = [];
}
}
// Add the remaining PDFs if any
if (!empty($pdfs)) {
$sections[] = $pdfs;
}
dd($sections);
// Optional: Combine multiple PDFs into one PDF (e.g., using the Spatie PDFMerger package)
// Return the sections to your view
return view('pdf_template')->with('sections', $sections);
}
web.php
Route::post('/process-excel', [PDFController::class, 'processExcel']);
Route::get('/pdf-template', [PDFController::class, 'viewTemplate']);
when i upload the excel then it go to
http://127.0.0.1:8000/process-excel
and showing me error
Undefined variable $sections
Please or to participate in this conversation.