Genera storage/pdf-man con pdf app.pdf, db.pdf ecc con i contenuti dei files.
La cartella resources è suddivisa in resources_css.pdf, resources_views.pdf ecc.
Utilizza il pkg composer setasign/fpdf.
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use FPDF;
class GenerateManPdfs extends Command
{
protected $signature = 'dev:generate-man-pdfs';
protected $description = 'Genera PDF per ogni cartella principale contenente tutti i file nella cartella e sottocartelle';
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)
{
// Inizializza tFPDF
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial', '', 10);
foreach ($filePaths as $filePath) {
$relativePath = substr($filePath, strlen($rootPath) + 1);
// Aggiungi il nome del file
$pdf->SetFont('Arial', 'B', 12);
$pdf->Cell(0, 10, "File: $relativePath", 0, 1);
$pdf->SetFont('Arial', '', 10);
// Leggi il contenuto del file
$fileContent = File::get($filePath);
// Spezza il contenuto in linee
$lines = explode("\n", $fileContent);
foreach ($lines as $line) {
// Controlla se siamo vicini al fondo della pagina
if ($pdf->GetY() > 270) {
$pdf->AddPage();
}
// Aggiungi la linea al PDF
$pdf->MultiCell(0, 5, $line);
}
// Aggiungi uno spazio tra i file
$pdf->Ln(10);
}
$pdfFilePath = $pdfOutputPath . '/' . $pdfFileName . '.pdf';
$pdf->Output('F', $pdfFilePath);
$this->info("PDF generato: $pdfFilePath");
}
}