FabioFratini преди 11 месеца
родител
ревизия
b95502ce7d
променени са 2 файла, в които са добавени 206 реда и са изтрити 5 реда
  1. 205 4
      app/Http/Livewire/Report.php
  2. 1 1
      resources/views/components/verbale/allegati/index.blade.php

+ 205 - 4
app/Http/Livewire/Report.php

@@ -23,6 +23,7 @@ use App\Models\ReportDataVeicoli;
 use App\Models\Vehicle;
 use \Illuminate\Support\Facades\DB;
 use \Illuminate\Support\Facades\Log;
+use Webklex\PDFMerger\Facades\PDFMergerFacade as PDFMerger;
 
 class Report extends Component
 {
@@ -1415,9 +1416,11 @@ class Report extends Component
 
     public function print($id)
     {
+        Log::info("Starting PDF generation for report ID: $id");
 
         $record = \App\Models\Report::findOrFail($id);
         $parti_coinvolte = ReportDataPartiCoinvolte::where('report_id', $id)->get();
+        Log::info("Loaded report and parties involved. Found " . count($parti_coinvolte) . " parties.");
 
         $datiVeicolo = array();
         $datiInfortunati = array();
@@ -1426,6 +1429,7 @@ class Report extends Component
         $datiCascoOmologazione = array();
         $datiCasco = array();
         $datiCintureSicurezza = array();
+
         foreach ($parti_coinvolte as $pr) {
             $datiVeicolo[$pr->progressive] = ReportDataVeicoli::where('progressive', $pr->progressive)->where('report_id', $id)->first();
             $datiPedoni[$pr->progressive] = \App\Models\ReportDataPedoni::where('progressive', $pr->progressive)->where('report_id', $id)->get();
@@ -1453,6 +1457,9 @@ class Report extends Component
         $segnaletica_orizzontale_3 = \App\Models\ReportSegnaleticaOrizzontale3::where('report_id', $id)->get();
         $segnaletica_orizzontale_4 = \App\Models\ReportSegnaleticaOrizzontale4::where('report_id', $id)->get();
         $allegati = \App\Models\ReportAllegatiGallery::where('report_id', $id)->get();
+
+        Log::info("Loaded all report data. Found " . count($allegati) . " attachments.");
+
         $data = array(
             'record' => $record,
             'parti_coinvolte' => $parti_coinvolte,
@@ -1482,16 +1489,210 @@ class Report extends Component
             'allegati' => $allegati
         );
 
+        $hasPdfAttachments = false;
+        $pdfAttachments = [];
+        $totalAttachmentPages = 0;
+
+        foreach ($allegati as $allegato) {
+            if ($allegato->is_visible && $allegato->files) {
+                $files = explode('|', $allegato->files);
+                foreach ($files as $file) {
+                    if (strtolower(pathinfo($file, PATHINFO_EXTENSION)) === 'pdf') {
+                        $attachmentPath = public_path('storage/' . $file);
+                        if (file_exists($attachmentPath)) {
+                            $attachmentPageCount = $this->countPdfPages($attachmentPath);
+                            Log::info("Attachment {$file} has {$attachmentPageCount} pages");
+
+                            $pdfAttachments[] = $attachmentPath;
+                            $totalAttachmentPages += $attachmentPageCount;
+                            $hasPdfAttachments = true;
+                        }
+                    }
+                }
+            }
+        }
+
+        Log::info("Total attachment pages: {$totalAttachmentPages}");
+
+        $data['attachment_pages'] = $totalAttachmentPages;
         $pdf = Pdf::loadView('pdf.verbale', array('data' => $data));
         $pdf->render();
-        $pageCount = $pdf->getDomPDF()->getCanvas()->get_page_count();
-        $data['total_pages'] = $pageCount;
+
+        $mainPageCount = $pdf->getDomPDF()->getCanvas()->get_page_count();
+        Log::info("Main PDF has {$mainPageCount} pages");
+
+        // Calculate total pages and regenerate PDF with correct count
+        $totalPages = $mainPageCount + $totalAttachmentPages;
+        Log::info("Total pages (main + attachments): {$totalPages}");
+
+        $data['total_pages'] = $totalPages;
         $pdf = Pdf::loadView('pdf.verbale', array('data' => $data));
         $pdfName = "verbale_" . $record->protocollo_num . '_' . $record->protocollo_anno . ".pdf";
-        $pdfContent = $pdf->output();
-        return $pdf->stream($pdfName, $pdfContent);
+
+        if ($hasPdfAttachments) {
+            try {
+                Log::info("Starting PDF merge using GhostScript with " . count($pdfAttachments) . " attachments");
+
+                $tempDir = storage_path('app/temp');
+                if (!file_exists($tempDir)) {
+                    mkdir($tempDir, 0755, true);
+                }
+
+                $tempMainPdf = $tempDir . '/verbale_' . uniqid() . '.pdf';
+                file_put_contents($tempMainPdf, $pdf->output());
+
+                $pdfFiles = [$tempMainPdf];
+
+                try {
+                    $separatorPath = $this->createSeparatorPage($tempDir, "DOCUMENTI ALLEGATI");
+                    if ($separatorPath) {
+                        $pdfFiles[] = $separatorPath;
+                    }
+                } catch (\Exception $e) {
+                    Log::warning("Failed to create separator page: " . $e->getMessage());
+                }
+
+                foreach ($pdfAttachments as $attachmentPath) {
+                    if (file_exists($attachmentPath)) {
+                        $pdfFiles[] = $attachmentPath;
+                    } else {
+                        Log::warning("Attachment file not found: $attachmentPath");
+                    }
+                }
+
+                $outputPath = $tempDir . '/verbale_merged_' . uniqid() . '.pdf';
+
+                $success = $this->mergeWithGhostScript($pdfFiles, $outputPath);
+
+                if ($success) {
+                    Log::info("PDF merge successful, returning merged file");
+                    $pdfContent = file_get_contents($outputPath);
+
+                    @unlink($tempMainPdf);
+                    @unlink($outputPath);
+                    if (isset($separatorPath)) {
+                        @unlink($separatorPath);
+                    }
+
+                    return response($pdfContent)
+                        ->header('Content-Type', 'application/pdf')
+                        ->header('Content-Disposition', 'inline; filename="' . $pdfName . '"');
+                } else {
+                    Log::warning("PDF merge failed, falling back to main PDF only");
+                    @unlink($tempMainPdf);
+                    if (isset($separatorPath)) {
+                        @unlink($separatorPath);
+                    }
+                    return $pdf->stream($pdfName);
+                }
+
+            } catch (\Exception $e) {
+                Log::error("PDF merging failed: " . $e->getMessage());
+                return $pdf->stream($pdfName);
+            }
+        } else {
+            return $pdf->stream($pdfName);
+        }
     }
 
+    private function mergeWithGhostScript($pdfFiles, $outputPath)
+    {
+        try {
+            // Check if GhostScript is installed
+            exec('which gs', $output, $returnVar);
+            if ($returnVar !== 0) {
+                Log::error("GhostScript (gs) not found on system");
+                return false;
+            }
+
+            // Build GhostScript command
+            $fileList = implode(' ', array_map('escapeshellarg', $pdfFiles));
+            $outputFile = escapeshellarg($outputPath);
+
+            // GhostScript command to merge PDFs
+            $command = "gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=$outputFile $fileList";
+
+            // Execute command
+            exec($command, $output, $returnVar);
+
+            // Check if command was successful
+            if ($returnVar !== 0) {
+                Log::error("GhostScript command failed: " . implode("\n", $output));
+                return false;
+            }
+
+            return file_exists($outputPath);
+        } catch (\Exception $e) {
+            Log::error("GhostScript merge failed: " . $e->getMessage());
+            return false;
+        }
+    }
+
+    private function countPdfPages($pdfPath)
+    {
+        try {
+            $cmd = "gs -q -dNODISPLAY -c \"({$pdfPath}) (r) file runpdfbegin pdfpagecount = quit\"";
+            exec($cmd, $output, $returnVar);
+
+            if ($returnVar === 0 && !empty($output) && is_numeric($output[0])) {
+                return (int)$output[0];
+            }
+
+            $content = file_get_contents($pdfPath);
+            $pageCount = preg_match_all("/\/Page\W/", $content, $matches);
+
+            if ($pageCount > 0) {
+                return $pageCount;
+            }
+
+            Log::warning("Could not determine page count for PDF: {$pdfPath}. Assuming 1 page.");
+            return 1;
+        } catch (\Exception $e) {
+            Log::error("Error counting PDF pages: " . $e->getMessage());
+            return 1;
+        }
+    }
+
+    private function createSeparatorPage($tempDir, $title)
+    {
+        try {
+            Log::info("Creating separator page with title: $title");
+            $separatorPath = $tempDir . '/separator_' . uniqid() . '.pdf';
+            Log::info("Separator path: $separatorPath");
+
+            // Check if view exists
+            if (!view()->exists('pdf.separator')) {
+                Log::warning("Separator view 'pdf.separator' does not exist");
+                // Create an alternative simple view
+                $html = '<html><body><h1 style="text-align:center;margin-top:50%;">' . $title . '</h1></body></html>';
+                $separatorPdf = Pdf::loadHTML($html);
+            } else {
+                // Create a simple PDF with the title
+                Log::info("Loading separator view");
+                $separatorPdf = Pdf::loadView('pdf.separator', [
+                    'title' => $title
+                ]);
+            }
+
+            Log::info("Generating separator PDF");
+            $separatorContent = $separatorPdf->output();
+            $bytesWritten = file_put_contents($separatorPath, $separatorContent);
+
+            if ($bytesWritten === false) {
+                Log::error("Failed to write separator PDF to file");
+                return false;
+            }
+
+            Log::info("Separator PDF created successfully: $bytesWritten bytes written");
+            Log::info("Separator file exists: " . (file_exists($separatorPath) ? 'Yes' : 'No'));
+
+            return $separatorPath;
+        } catch (\Exception $e) {
+            Log::error("Failed to create separator page: " . $e->getMessage());
+            Log::error("Stack trace: " . $e->getTraceAsString());
+            return false;
+        }
+    }
     public function updated($property, $value)
     {
         if ($property === 'verificatosi_in_data' && !preg_match('/^\d{4}-\d{2}-\d{2}$/', $value)) {

+ 1 - 1
resources/views/components/verbale/allegati/index.blade.php

@@ -15,7 +15,7 @@
     <table width="100%">
         <tr>
             <td width="100%" valign="top" align="center">
-                <h2>ALLEGATI</h2>
+                <h2>ALLEGATI FOTOGRAFICI</h2>
             </td>
         </tr>
     </table>