Output (HasOutput)
Saving to File
The simplest way to produce a PDF is to write it directly to disk:
use Yeeefang\TcpdfNext\Core\Document;
$pdf = Document::create()
->addPage()
->setFont('Helvetica', '', 12)
->cell(0, 10, 'Hello, World!')
->save('/path/to/output.pdf');save(string $path) writes the rendered PDF to the given file path and returns static.
Output with Destination Control
For more control over delivery, use output() with the OutputDestination enum:
use Yeeefang\TcpdfNext\Core\Document;
use Yeeefang\TcpdfNext\Contracts\OutputDestination;
$pdf = Document::create()
->addPage()
->setFont('Helvetica', '', 12)
->cell(0, 10, 'Hello, World!');
// Display in browser (inline)
$pdf->output('document.pdf', OutputDestination::Inline);
// Force download
$pdf->output('document.pdf', OutputDestination::Download);
// Save to file
$pdf->output('document.pdf', OutputDestination::File);
// Return as string
$pdf->output('document.pdf', OutputDestination::String);OutputDestination Enum
| Value | Behavior | HTTP Headers |
|---|---|---|
Inline | Display in browser | Content-Type: application/pdf, Content-Disposition: inline |
Download | Force download dialog | Content-Type: application/pdf, Content-Disposition: attachment |
File | Save to server filesystem | None |
String | Return raw PDF bytes | None |
When using Inline or Download, TCPDF-Next automatically sends the appropriate Content-Type and Content-Disposition headers before flushing the PDF content.
Getting PDF as a String
Use toString() to get the raw PDF bytes without writing to disk or sending headers. This is useful for email attachments, storage APIs, or further processing:
use Yeeefang\TcpdfNext\Core\Document;
$pdf = Document::create()
->addPage()
->setFont('Helvetica', '', 12)
->cell(0, 10, 'Monthly Report');
$content = $pdf->toString();
// Write to disk manually
file_put_contents('/path/to/output.pdf', $content);
// Or use for email attachments
Mail::send([], [], function ($message) use ($content) {
$message->attachData($content, 'report.pdf', ['mime' => 'application/pdf']);
});Streaming Output for Large PDFs
For documents with thousands of pages, holding the entire PDF in memory can be expensive. PdfWriterChunked writes PDF content incrementally to a stream:
use Yeeefang\TcpdfNext\Core\Document;
$pdf = Document::create();
$pdf->addPage()->setFont('Helvetica', '', 10);
for ($i = 0; $i < 5000; $i++) {
$pdf->cell(0, 5, "Row {$i}: data content here", newLine: true);
}
// Streaming happens internally — save triggers chunked write
$pdf->save('large-report.pdf');The chunked writer flushes page data as it is finalized, avoiding the need to buffer the entire document before writing. This is transparent to the caller — the same save() and output() API applies.
Linearization (Fast Web View)
Linearization reorders PDF objects so the first page can be rendered before the entire file is downloaded. This is essential for web-delivered PDFs:
use Yeeefang\TcpdfNext\Core\Document;
$pdf = Document::create()
->setLinearization(true)
->addPage()
->setFont('Helvetica', '', 12)
->cell(0, 10, 'Optimized for web viewing')
->save('linearized.pdf');The Linearizer writes hint streams that tell PDF viewers how to progressively render the document. Enable this when PDFs will be served over HTTP and viewed in-browser.