The issue you're experiencing is likely due to the fact that the view() method in your PdfController is not being reached because the URL being generated in your Blade view is incorrect or not accessible. Let's go through a few steps to troubleshoot and resolve this issue:
-
Check the URL Generation: Ensure that the URL generated by
route('pdf.view', ['filename' => $filename])is correct and accessible. You can do this by manually visiting the URL in your browser to see if it triggers theview()method. -
Verify Route Configuration: Double-check that the route for
pdf.viewis correctly defined and matches the URL pattern you are trying to access. The route should look like this:Route::get('/pdf/view/{filename?}', [PdfController::class, 'view']) ->where('filename', '(.*)') ->name('pdf.view'); -
Check File Accessibility: Ensure that the file you are trying to access exists on the remote server and that the path is correct. You can add additional logging in the
view()method to confirm the file path and check if the file is being fetched correctly. -
Cross-Origin Resource Sharing (CORS): If the PDF file is being fetched from a different domain, ensure that CORS is properly configured on the server hosting the PDF files. This is necessary for the browser to allow the PDF.js library to fetch and render the PDF.
-
Debugging: Add more logging in the
view()method to capture any potential errors or issues when fetching the file. For example:public function view($filename) { \Illuminate\Support\Facades\Log::info('Hitting view() with filename: ' . $filename); try { $fileContent = Storage::disk('sftp')->get($filename); if ($fileContent) { \Illuminate\Support\Facades\Log::info('File exists'); } else { \Illuminate\Support\Facades\Log::info('File not found'); } return response($fileContent, 200) ->header('Content-Type', 'application/pdf'); } catch (\Exception $e) { \Illuminate\Support\Facades\Log::error('Error fetching file: ' . $e->getMessage()); return response('File not found', 404); } } -
Network Tab in Developer Tools: Use the browser's developer tools to inspect the network requests. Check if the request to the PDF file is being made and if there are any errors or issues with the request.
By following these steps, you should be able to identify why the view() method is not being reached and resolve the issue. If the problem persists, consider checking server logs or any middleware that might be affecting the request.