HTML Renderer
La classe HtmlRenderer è l'entry point principale per tutte le operazioni HTML-to-PDF. Fornisce un'API fluent per caricare contenuto, configurare output e renderizzare documenti PDF.
Metodo Factory
Crea un'istanza renderer con il factory statico create().
use Yeeefang\TcpdfNext\Artisan\HtmlRenderer;
// Configurazione predefinita (auto-rileva Chrome)
$renderer = HtmlRenderer::create();
// Con percorso binario Chrome personalizzato
$renderer = HtmlRenderer::create(
chromePath: '/usr/bin/google-chrome',
);
// Con directory temporanea personalizzata
$renderer = HtmlRenderer::create(
chromePath: '/usr/bin/chromium',
tempDir: '/tmp/artisan-render',
);Caricamento Contenuto
Da una Stringa
Passa HTML grezzo direttamente con loadHtml(). La stringa può essere un documento HTML completo o un frammento.
$renderer->loadHtml('<h1>Ciao, Mondo!</h1>');Quando passi un frammento, Artisan lo avvolge automaticamente in un documento <!DOCTYPE html> minimale.
Da un File Locale
Carica un file .html da disco con loadFile(). I percorsi relativi a fogli stile, immagini e script nel file sono risolti dalla directory del file.
$renderer->loadFile('/templates/quarterly-report.html');Da un URL
Recupera e renderizza un URL live con loadUrl(). La pagina è caricata dentro Chrome headless, quindi JavaScript si esegue e chiamate AJAX si risolvono prima del rendering.
$renderer->loadUrl('https://reports.example.com/q4-2026');Puoi impostare un timeout navigazione per prevenire blocco su pagine lente:
$renderer->loadUrl('https://example.com/dashboard', timeoutMs: 30000);Metodi Output
Salva su File
$renderer->save('/output/report.pdf');Ottieni come Stringa
Recupera i byte PDF grezzi per elaborazione ulteriore (es. memorizzazione in database, allegato a email).
$pdfContent = $renderer->toString();
// Esempio: memorizza in database
DB::table('documents')->insert([
'name' => 'report.pdf',
'content' => $pdfContent,
]);Invia al Browser
Invia il PDF direttamente alla risposta HTTP con header appropriati.
// Display inline (visualizzatore PDF browser)
$renderer->output('report.pdf', 'inline');
// Forza download
$renderer->output('report.pdf', 'download');Esempio Completo: Fattura
use Yeeefang\TcpdfNext\Artisan\HtmlRenderer;
$html = <<<'HTML'
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: 'Inter', sans-serif; margin: 20mm; }
.header { display: flex; justify-content: space-between; align-items: flex-start; }
.company { font-size: 24px; font-weight: 700; color: #1a237e; }
.meta { text-align: right; color: #666; font-size: 13px; }
.grid { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; margin: 30px 0; }
.grid section { padding: 15px; background: #f8f9fa; border-radius: 6px; }
table { width: 100%; border-collapse: collapse; margin-top: 20px; }
th { background: #1a237e; color: white; padding: 10px 12px; text-align: left; }
td { border-bottom: 1px solid #e0e0e0; padding: 10px 12px; }
tr:nth-child(even) { background: #fafafa; }
.total { font-weight: 700; font-size: 18px; text-align: right; margin-top: 20px; }
</style>
</head>
<body>
<div class="header">
<div class="company">Acme Corporation</div>
<div class="meta">
Fattura #2026-001<br>
Data: 2026-02-16<br>
Scadenza: 2026-03-16
</div>
</div>
<div class="grid">
<section>
<strong>Fattura a</strong><br>
Jane Smith<br>
456 Oak Avenue<br>
Springfield, IL 62704
</section>
<section>
<strong>Spedizione a</strong><br>
Jane Smith<br>
789 Elm Street<br>
Springfield, IL 62704
</section>
</div>
<table>
<thead>
<tr>
<th>Articolo</th>
<th>Q.tà</th>
<th>Prezzo Unit.</th>
<th>Importo</th>
</tr>
</thead>
<tbody>
<tr><td>Sviluppo Web</td><td>40 ore</td><td>$150.00</td><td>$6,000.00</td></tr>
<tr><td>Design UI/UX</td><td>20 ore</td><td>$125.00</td><td>$2,500.00</td></tr>
<tr><td>Hosting Annuale</td><td>1</td><td>$1,200.00</td><td>$1,200.00</td></tr>
</tbody>
</table>
<div class="total">Totale: $9,700.00</div>
</body>
</html>
HTML;
HtmlRenderer::create()
->loadHtml($html)
->save('/invoices/2026-001.pdf');Concatenamento Metodi
Ogni setter su HtmlRenderer restituisce $this, abilitando un pattern builder fluent.
use Yeeefang\TcpdfNext\Artisan\HtmlRenderer;
use Yeeefang\TcpdfNext\Artisan\RenderOptions;
use Yeeefang\TcpdfNext\Artisan\StyleInjector;
HtmlRenderer::create()
->loadFile('/templates/report.html')
->withOptions(
RenderOptions::create()
->setPageSize('A4')
->setLandscape(false)
->setMargins(top: 15, right: 10, bottom: 15, left: 10)
->setPrintBackground(true)
)
->withStyleInjector(
StyleInjector::create()
->addCss('body { font-size: 12pt; }')
)
->save('/output/styled-report.pdf');Gestione Errori
use Yeeefang\TcpdfNext\Artisan\HtmlRenderer;
use Yeeefang\TcpdfNext\Artisan\Exceptions\RenderException;
use Yeeefang\TcpdfNext\Artisan\Exceptions\ChromeNotFoundException;
use Yeeefang\TcpdfNext\Artisan\Exceptions\TimeoutException;
try {
HtmlRenderer::create()
->loadUrl('https://example.com/slow-report')
->save('/output/report.pdf');
} catch (ChromeNotFoundException $e) {
// Binario Chrome non trovato -- controlla CHROME_PATH
logger()->error('Chrome non installato: ' . $e->getMessage());
} catch (TimeoutException $e) {
// La pagina ha impiegato troppo tempo a caricare o renderizzare
logger()->warning('Rendering timeout: ' . $e->getMessage());
} catch (RenderException $e) {
// Qualsiasi altro fallimento rendering
logger()->error('Rendering fallito: ' . $e->getMessage());
}Prossimi Passi
- Render Options -- Ottimizza dimensione pagina, margini, header e footer.
- Funzionalità Avanzate -- Unione PDF, iniezione CSS, screenshot.