public function downloadDocument() { try { // Get storage disk instance $storage = Storage::disk('local'); // or whatever disk you're using // Ensure directory exists $compiledPath = $recordID . '/compiled'; if (!$storage->exists($compiledPath)) { $storage->makeDirectory($compiledPath); } // Build and execute command $command = [ // ... your command array here ]; $process = new Process($command); $process->setTimeout(30); // Run the process and capture output $process->mustRun(function ($type, $buffer) { logger()->debug($type . ': ' . $buffer); }); // Define PDF path $pdfPath = $compiledPath . '/main.pdf'; // Verify file exists and is readable if (!$storage->exists($pdfPath)) { throw new RuntimeException('PDF file does not exist after compilation.'); } // Get the full path for local files $fullPath = $storage->path($pdfPath); // Verify file is actually there and readable if (!file_exists($fullPath) || !is_readable($fullPath)) { throw new RuntimeException('PDF file exists but is not readable.'); } // Get file size $size = $storage->size($pdfPath); if ($size === 0) { throw new RuntimeException('PDF file is empty.'); } // Set proper headers for PDF download $headers = [ 'Content-Type' => 'application/pdf', 'Content-Disposition' => 'attachment; filename="document.pdf"', 'Content-Length' => $size, ]; // Return the download response return response()->download( $fullPath, 'document.pdf', $headers ); } catch (ProcessFailedException $exception) { logger()->error('PDF compilation failed', [ 'error' => $exception->getMessage(), 'output' => $exception->getProcess()->getOutput(), 'errorOutput' => $exception->getProcess()->getErrorOutput() ]); throw new RuntimeException('PDF compilation failed: ' . $exception->getMessage()); } catch (\Exception $e) { logger()->error('Download failed', [ 'error' => $e->getMessage() ]); throw new RuntimeException('Failed to download document: ' . $e->getMessage()); } }