- Laravel 11 backend with API REST - React 18 + TypeScript + Vite frontend - Multi-parser architecture for accounting systems (CONTPAQi, Aspel, SAP) - 27+ financial metrics calculation - PDF report generation with Browsershot - Complete documentation (10 documents) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
61 lines
1.5 KiB
PHP
61 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\Reporte;
|
|
use Spatie\Browsershot\Browsershot;
|
|
|
|
class GeneradorPdf
|
|
{
|
|
private string $frontendUrl;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->frontendUrl = config('app.frontend_url', 'http://localhost:5173');
|
|
}
|
|
|
|
/**
|
|
* Genera el PDF del reporte
|
|
*
|
|
* @param Reporte $reporte
|
|
* @return string Path relativo del PDF generado
|
|
*/
|
|
public function generar(Reporte $reporte): string
|
|
{
|
|
$outputPath = 'reportes/' . $reporte->cliente_id . '/' . $reporte->id . '.pdf';
|
|
$fullPath = storage_path('app/' . $outputPath);
|
|
|
|
// Crear directorio si no existe
|
|
$dir = dirname($fullPath);
|
|
if (!is_dir($dir)) {
|
|
mkdir($dir, 0755, true);
|
|
}
|
|
|
|
// URL del frontend para renderizar el reporte
|
|
$url = $this->frontendUrl . '/pdf-view/' . $reporte->id . '?token=' . $this->generarTokenTemporal($reporte);
|
|
|
|
Browsershot::url($url)
|
|
->waitUntilNetworkIdle()
|
|
->format('Letter')
|
|
->margins(0, 0, 0, 0)
|
|
->showBackground()
|
|
->save($fullPath);
|
|
|
|
return $outputPath;
|
|
}
|
|
|
|
/**
|
|
* Genera un token temporal para acceso al PDF
|
|
*/
|
|
private function generarTokenTemporal(Reporte $reporte): string
|
|
{
|
|
// Token válido por 5 minutos
|
|
$data = [
|
|
'reporte_id' => $reporte->id,
|
|
'expires' => time() + 300,
|
|
];
|
|
|
|
return base64_encode(json_encode($data));
|
|
}
|
|
}
|