Xuất PDF (HasOutput)
Lưu ra file
Cách đơn giản nhất để tạo PDF là ghi trực tiếp ra ổ đĩa:
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) ghi PDF đã render ra đường dẫn file chỉ định và trả về static.
Xuất với điều khiển đích
Để kiểm soát nhiều hơn cách phân phối, dùng output() với enum OutputDestination:
use Yeeefang\TcpdfNext\Core\Document;
use Yeeefang\TcpdfNext\Contracts\OutputDestination;
$pdf = Document::create()
->addPage()
->setFont('Helvetica', '', 12)
->cell(0, 10, 'Hello, World!');
// Hiển thị trong trình duyệt (inline)
$pdf->output('document.pdf', OutputDestination::Inline);
// Bắt buộc tải xuống
$pdf->output('document.pdf', OutputDestination::Download);
// Lưu ra file
$pdf->output('document.pdf', OutputDestination::File);
// Trả về dạng chuỗi
$pdf->output('document.pdf', OutputDestination::String);Enum OutputDestination
| Giá trị | Hành vi | HTTP Header |
|---|---|---|
Inline | Hiển thị trong trình duyệt | Content-Type: application/pdf, Content-Disposition: inline |
Download | Bắt buộc hộp thoại tải xuống | Content-Type: application/pdf, Content-Disposition: attachment |
File | Lưu ra filesystem server | Không có |
String | Trả về byte PDF thô | Không có |
Khi dùng Inline hoặc Download, TCPDF-Next tự động gửi header Content-Type và Content-Disposition phù hợp trước khi flush nội dung PDF.
Lấy PDF dạng chuỗi
Dùng toString() để lấy byte PDF thô mà không ghi ra ổ đĩa hoặc gửi header. Hữu ích cho đính kèm email, storage API, hoặc xử lý tiếp:
use Yeeefang\TcpdfNext\Core\Document;
$pdf = Document::create()
->addPage()
->setFont('Helvetica', '', 12)
->cell(0, 10, 'Monthly Report');
$content = $pdf->toString();
// Ghi ra ổ đĩa thủ công
file_put_contents('/path/to/output.pdf', $content);
// Hoặc dùng cho đính kèm email
Mail::send([], [], function ($message) use ($content) {
$message->attachData($content, 'report.pdf', ['mime' => 'application/pdf']);
});Streaming cho PDF lớn
Với document có hàng nghìn trang, giữ toàn bộ PDF trong bộ nhớ rất tốn kém. PdfWriterChunked ghi nội dung PDF từng phần ra 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 xảy ra nội bộ — save kích hoạt ghi chunked
$pdf->save('large-report.pdf');Chunked writer flush dữ liệu trang khi đã hoàn thiện, tránh phải buffer toàn bộ document trước khi ghi. Điều này trong suốt với caller — cùng API save() và output() áp dụng.
Linearization (Fast Web View)
Linearization sắp xếp lại object PDF để trang đầu tiên có thể render trước khi toàn bộ file được tải xuống. Điều này thiết yếu cho PDF phân phối qua web:
use Yeeefang\TcpdfNext\Core\Document;
$pdf = Document::create()
->setLinearization(true)
->addPage()
->setFont('Helvetica', '', 12)
->cell(0, 10, 'Optimized for web viewing')
->save('linearized.pdf');Linearizer ghi hint stream cho trình xem PDF biết cách render document dần dần. Bật tính năng này khi PDF sẽ được phục vụ qua HTTP và xem trong trình duyệt.