Document Lifecycle
The Document class is the central facade of TCPDF-Next. It is final, implements PdfDocumentInterface, and provides all functionality through 12 composable traits.
Creating a Document
php
use Yeeefang\TcpdfNext\Core\Document;
$pdf = Document::create();The constructor is private — always use the static create() factory. This initializes:
- Default page size: A4 (210 × 297 mm)
- Default margins: 10mm symmetric
- Default font size: 12pt
- Default colors: black text, white fill, black stroke
- Default line width: 0.2mm (0.57pt)
Metadata (HasMetadata)
php
$pdf = Document::create()
->setTitle('Invoice #001')
->setAuthor('Yeeefang')
->setSubject('Monthly Invoice')
->setKeywords('invoice, payment, 2026')
->setCreator('TCPDF-Next')
->setLanguage('en');All metadata methods return static for chaining. Metadata is written as XMP (PDF 2.0) and Info dictionary entries.
Viewer Preferences
Control how PDF viewers display the document:
php
$pdf->setDisplayMode('fullpage', 'SinglePage')
->setViewerPreferences([
'HideToolbar' => true,
'HideMenubar' => true,
'FitWindow' => true,
]);Document Constants
| Constant | Value | Description |
|---|---|---|
MAX_PAGES | 10,000 | Maximum pages per document |
Internal Architecture
The Document holds references to sub-engines, most of which are lazily initialized:
Document
├── DocumentData # Metadata, fonts, images, pages
├── PdfWriter # PDF serialization
├── FontManager # Font loading and subsetting
├── FontMetrics # String width calculation
├── DrawingEngine # Shape/gradient rendering
├── TransformEngine # CTM transforms
├── TextRenderer # Text state management
├── HeaderFooter # Header/footer rendering
├── ColumnLayout # Multi-column layout
├── BookmarkManager # PDF bookmarks
├── LinkManager # Internal/external links
├── TocBuilder # Table of contents
├── AnnotationManager # Text annotations
├── PageManager # Page manipulation
├── FormFieldManager? # AcroForm fields (lazy)
├── LayerManager? # OCG layers (lazy)
├── TemplateManager? # Page templates (lazy)
├── TransactionManager? # Savepoint/rollback (lazy)
├── BookletLayout? # Booklet imposition (lazy)
├── FileAttachment? # Embedded files (lazy)
├── JavaScriptManager? # Document JavaScript (lazy)
├── SvgParser? # SVG rendering (lazy)
├── EpsParser? # EPS rendering (lazy)
├── StructureTree? # Tagged PDF (lazy)
├── SpotColorManager? # Spot colors (lazy)
├── ImageLoader? # Image loading (lazy)
├── BiDiResolver? # Bidirectional text (lazy)
├── Aes256Encryptor? # AES encryption (lazy)
├── PadesOrchestrator? # Digital signatures (lazy)
└── ChromeRenderer? # Chrome CDP (lazy, Artisan)Accessing Sub-Engines
For advanced use cases, you can access internal engines:
php
$fontManager = $pdf->getFontManager();
$writer = $pdf->getWriter();
$drawingEngine = $pdf->getDrawingEngine();
$bookmarkManager = $pdf->getBookmarkManager();Complete Example
php
use Yeeefang\TcpdfNext\Core\Document;
use Yeeefang\TcpdfNext\ValueObjects\PageSize;
use Yeeefang\TcpdfNext\ValueObjects\Margin;
use Yeeefang\TcpdfNext\Contracts\Orientation;
$pdf = Document::create()
// Metadata
->setTitle('Company Report')
->setAuthor('Finance Team')
->setLanguage('en')
// First page — A4 portrait
->addPage()
->setFont('Helvetica', 'B', 24)
->cell(0, 20, 'Annual Report 2026')
->ln()
// Second page — landscape
->addPage(PageSize::A4(), Orientation::Landscape)
->setFont('Helvetica', '', 12)
->cell(0, 10, 'Financial Summary')
// Save
->save('report.pdf');