Use barryvdh/laravel-dompdf composer pkg

<?php
 
namespace App\Console\Commands\Dev;
 
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use PDF;
 
class GenerateManPdfs extends Command
{
  protected $signature = 'dev:generate-man-pdfs';
 
  protected $description = 'Genera PDF per cartelle specifiche, escludendo altre cartelle';
 
 
  public function handle()
  {
    $rootPath = base_path();
    $pdfOutputPath = storage_path('pdf-man');
 
    // Assicura che la directory di output esista
    if (!File::exists($pdfOutputPath)) {
      File::makeDirectory($pdfOutputPath, 0755, true);
    }
 
    // Specifica le cartelle da escludere
    $excludeFolders = ['node_modules', 'vendor', 'public', 'storage', 'tests', 'resources', '.git'];
 
    // Ottiene tutte le directory nel percorso root, escludendo quelle specificate
    $folders = collect(File::directories($rootPath))->filter(function ($folderPath) use ($excludeFolders) {
      $folderName = basename($folderPath);
      return !in_array($folderName, $excludeFolders);
    });
 
    // **Processa le cartelle principali**
    foreach ($folders as $folderPath) {
      $folderName = basename($folderPath);
      $this->info("Processando la cartella: $folderName");
 
      $filePaths = $this->getAllFiles($folderPath, $excludeFolders);
 
      $this->generatePdf($filePaths, $folderName, $pdfOutputPath, $rootPath);
    }
 
    // **Processa la cartella 'resources' e le sue sottocartelle**
    $resourcesPath = $rootPath . DIRECTORY_SEPARATOR . 'resources';
 
    $resourcesSubfolders = ['css', 'js', 'views'];
 
    foreach ($resourcesSubfolders as $subfolder) {
      $subfolderPath = $resourcesPath . DIRECTORY_SEPARATOR . $subfolder;
      if (File::exists($subfolderPath)) {
        $this->info("Processando la sottocartella: resources/$subfolder");
 
        // Non escludiamo alcuna cartella all'interno delle sottocartelle di resources
        $filePaths = $this->getAllFiles($subfolderPath, []);
 
        $pdfFileName = 'resources_' . $subfolder;
 
        $this->generatePdf($filePaths, $pdfFileName, $pdfOutputPath, $rootPath);
      } else {
        $this->warn("La sottocartella resources/$subfolder non esiste.");
      }
    }
 
    $this->info('Tutti i PDF sono stati generati con successo!');
  }
 
  private function getAllFiles($directory, $excludeFolders)
  {
    $files = [];
 
    foreach (File::allFiles($directory) as $file) {
      $shouldExclude = false;
 
      // Controlla se il file si trova in una cartella da escludere
      foreach ($excludeFolders as $excludeFolder) {
        if (strpos($file->getPathname(), DIRECTORY_SEPARATOR . $excludeFolder . DIRECTORY_SEPARATOR) !== false) {
          $shouldExclude = true;
          break;
        }
      }
 
      if (!$shouldExclude) {
        $files[] = $file->getPathname();
      }
    }
 
    return $files;
  }
 
  private function generatePdf($filePaths, $pdfFileName, $pdfOutputPath, $rootPath)
  {
    $content = '';
 
    // Aggiungi stili CSS per migliorare la formattazione
    $content .= '<style>
          body { font-family: DejaVu Sans, sans-serif; font-size: 10px; }
          pre { background-color: #f5f5f5; padding: 10px; font-size: 10px; }
          h2 { color: #333; font-size: 14px; }
          hr { border: none; border-top: 1px solid #ddd; margin: 20px 0; }
      </style>';
 
    foreach ($filePaths as $filePath) {
      $relativePath = substr($filePath, strlen($rootPath) + 1);
 
      // Aggiungi il nome del file
      $content .= "<h2>File: $relativePath</h2>";
 
      // Leggi il contenuto del file ed esegui l'escape dei caratteri HTML
      $fileContent = File::get($filePath);
      $escapedContent = htmlspecialchars($fileContent);
 
      // Aggiungi il contenuto del file in un blocco di codice preformattato
      $content .= '<pre>' . $escapedContent . '</pre>';
 
      // Aggiungi uno spazio tra i file
      $content .= '<hr>';
    }
 
    // Genera il PDF utilizzando la vista inline
    $pdf = PDF::loadHTML($content);
 
    // Imposta la dimensione della pagina e l'orientamento se necessario
    // $pdf->setPaper('a4', 'portrait');
 
    $pdfFilePath = $pdfOutputPath . '/' . $pdfFileName . '.pdf';
    $pdf->save($pdfFilePath);
 
    $this->info("PDF generato: $pdfFilePath");
  }
}