Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

swamydeshetty's avatar

Laravel PDF is not downloading?

Im trying to download the pdf but it is not downloading why what is reason im posting the code here please review n let me know the issues

public function Logsexport(Request $request) { try { \Log::info('Logsexport function called');

            $startDate = $request->input('start_date');
            $endDate = $request->input('end_date');
            $empId = $request->input('employee_id');
            $submitType = $request->input('submit_type');
    
            // Retrieve organization ID from logged in admin
            \Log::info('Organization ID: ' . admin()->company_id);
            
            // Construct base query
            $query = DB::table('attendance_biologs')->where('org_id', admin()->company_id);
    
            // Apply date range filter if provided
            if ($startDate && $endDate) {
                $startDateCarbon = Carbon::createFromFormat('m-d-Y', $startDate)->startOfDay();
                $endDateCarbon = Carbon::createFromFormat('m-d-Y', $endDate)->endOfDay();
                $query->whereBetween('datetime', [$startDateCarbon, $endDateCarbon]);
                \Log::info('Date range filter applied: ' . $startDateCarbon . ' to ' . $endDateCarbon);
            }
    
            // Apply employee filter if provided
            if ($empId) {
                $id = Employee::where('id', $empId)->first();
                // Check if employee exists
                if ($id) {
                    $userId = $id->employeeID;
                    if ($userId) {
                        $query->where('user_id', $userId);
                        \Log::info('Employee filter applied: ' . $userId);
                    }
                } else {
                    // If employee doesn't exist, return an empty result set
                    $results = [];
                    \Log::info('Employee not found');
                }
            }
    
            // Retrieve results if not already set
            if (!isset($results)) {
                $results = $query->get();
                $count = count($results);
                \Log::info('Results retrieved from database'.$results);
                \Log::info('Count'.$count);

            }
    
            // Modify $results if needed...
    
            // Generate and return PDF/Excel based on $submitType
            if ($submitType == 'pdf') {
                // Generate PDF
                \Log::info('PDF generation initiated');
    
                \Log::info('The data'.$results);
                $pdf = PDF::loadView('attendancelogs_report', ['results' => $results]);
                $pdf->setOptions(['dpi' => 150, 'defaultFont' => 'sans-serif']);
                \Log::info('PDF loaded with data');
                // $pdf = PDF::loadView('productlist', ['products' => $data]);
                // Download PDF
                // return $pdf->download('attendance_report.pdf');
                $content = $pdf->download('file-name-you-want.pdf');
                return $content;

                
            } elseif ($submitType == 'excel') {
                // Generate Excel
                \Log::info('Excel generation initiated');
                return Excel::download(new AttendanceLogsExport($results), 'attendance_report.xlsx');
            }
    
            // Handle invalid submit type
            \Log::error('Invalid submit type: ' . $submitType);
            return response()->json(['error' => 'Invalid submit type.'], 400);
        } catch (\Exception $e) {
            \Log::error('Error occurred while exporting logs: ' . $e->getMessage());
            return response()->json(['error' => 'An error occurred while exporting logs.'], 500);
        }
    }
     This is my code
0 likes
0 replies

Please or to participate in this conversation.