Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.65% covered (success)
92.65%
63 / 68
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
PageTree
92.65% covered (success)
92.65%
63 / 68
0.00% covered (danger)
0.00%
0 / 1
32.41
0.00% covered (danger)
0.00%
0 / 1
 toPdf
92.65% covered (success)
92.65%
63 / 68
0.00% covered (danger)
0.00%
0 / 1
32.41
1<?php
2
3declare(strict_types=1);
4
5namespace Phpdftk\Pdf\Core\Document;
6
7use Phpdftk\Pdf\Core\PdfArray;
8use Phpdftk\Pdf\Core\PdfDictionary;
9use Phpdftk\Pdf\Core\PdfName;
10use Phpdftk\Pdf\Core\PdfNumber;
11use Phpdftk\Pdf\Core\PdfObject;
12use Phpdftk\Pdf\Core\PdfReference;
13use Phpdftk\Pdf\Core\PdfString;
14use Phpdftk\Pdf\Core\Serializable;
15
16/**
17 * PDF Page Tree node (/Type /Pages).
18 * Serves as the root of the page tree and parent of individual Page objects.
19 */
20class PageTree extends PdfObject
21{
22    public const PDF_TYPE = 'Pages';
23
24    public ?PdfReference $parent = null;   // /Parent (for non-root nodes)
25    /** @var array<int, PdfReference> */
26    public array $kids = [];               // /Kids - array of PdfReference to pages/subtrees
27    public int $count = 0;                 // /Count - total leaf pages
28    public ?PdfArray $mediaBox = null;     // /MediaBox - inherited by pages
29    public ?PdfArray $cropBox = null;      // /CropBox - inheritable box
30    public ?PdfArray $bleedBox = null;     // /BleedBox - inheritable box
31    public ?PdfArray $trimBox = null;      // /TrimBox - inheritable box
32    public ?PdfArray $artBox = null;       // /ArtBox - inheritable box
33    public ?PdfReference $resources = null; // /Resources - inherited by pages
34    public int $rotate = 0;                // /Rotate
35    public ?PdfName $tabs = null;          // /Tabs - tab order (R, C, S)
36    public ?PdfNumber $userUnit = null;    // /UserUnit - points-per-unit multiplier
37    public ?PdfReference $group = null;    // /Group - transparency group (inheritable)
38    public ?PdfReference $thumb = null;    // /Thumb - thumbnail image
39    public ?PdfArray $b = null;            // /B - article bead refs
40    public ?PdfNumber $dur = null;         // /Dur - page display duration
41    public Serializable|null $transition = null; // /Trans - transition dict
42    public ?PdfArray $annots = null;       // /Annots - inheritable annotations
43    public ?PdfReference $aa = null;       // /AA - additional actions
44    public ?PdfReference $metadata = null; // /Metadata - XMP stream
45    public ?PdfDictionary $pieceInfo = null; // /PieceInfo - application data
46    public ?int $structParents = null;     // /StructParents
47    public ?PdfString $id = null;          // /ID - page identifier
48    public ?PdfNumber $pz = null;          // /PZ - preferred zoom
49    public BoxColorInfo|PdfDictionary|null $boxColorInfo = null; // /BoxColorInfo
50    public ?PdfArray $af = null;            // /AF - associated files
51    public ?PdfArray $outputIntents = null; // /OutputIntents
52    public ?PdfReference $dPart = null;     // /DPart
53    public ?PdfDictionary $separationInfo = null; // /SeparationInfo
54    public ?PdfName $templateInstantiated = null; // /TemplateInstantiated
55    public ?PdfReference $presSteps = null; // /PresSteps - presentation steps
56    public ?PdfArray $vp = null;           // /VP - viewport array
57
58    public function toPdf(): string
59    {
60        $dict = new PdfDictionary();
61        $dict->set('Type', new PdfName(self::PDF_TYPE));
62
63        if ($this->parent !== null) {
64            $dict->set('Parent', $this->parent);
65        }
66
67        // Build /Kids array
68        $kidItems = [];
69        foreach ($this->kids as $kid) {
70            $kidItems[] = $kid;
71        }
72        $dict->set('Kids', new PdfArray($kidItems));
73        $dict->set('Count', new PdfNumber($this->count));
74
75        if ($this->mediaBox !== null) {
76            $dict->set('MediaBox', $this->mediaBox);
77        }
78        if ($this->cropBox !== null) {
79            $dict->set('CropBox', $this->cropBox);
80        }
81        if ($this->bleedBox !== null) {
82            $dict->set('BleedBox', $this->bleedBox);
83        }
84        if ($this->trimBox !== null) {
85            $dict->set('TrimBox', $this->trimBox);
86        }
87        if ($this->artBox !== null) {
88            $dict->set('ArtBox', $this->artBox);
89        }
90        if ($this->resources !== null) {
91            $dict->set('Resources', $this->resources);
92        }
93        if ($this->rotate !== 0) {
94            $dict->set('Rotate', new PdfNumber($this->rotate));
95        }
96        if ($this->tabs !== null) {
97            $dict->set('Tabs', $this->tabs);
98        }
99        if ($this->userUnit !== null) {
100            $dict->set('UserUnit', $this->userUnit);
101        }
102        if ($this->group !== null) {
103            $dict->set('Group', $this->group);
104        }
105        if ($this->thumb !== null) {
106            $dict->set('Thumb', $this->thumb);
107        }
108        if ($this->b !== null) {
109            $dict->set('B', $this->b);
110        }
111        if ($this->dur !== null) {
112            $dict->set('Dur', $this->dur);
113        }
114        if ($this->transition !== null) {
115            $dict->set('Trans', $this->transition);
116        }
117        if ($this->annots !== null) {
118            $dict->set('Annots', $this->annots);
119        }
120        if ($this->aa !== null) {
121            $dict->set('AA', $this->aa);
122        }
123        if ($this->metadata !== null) {
124            $dict->set('Metadata', $this->metadata);
125        }
126        if ($this->pieceInfo !== null) {
127            $dict->set('PieceInfo', $this->pieceInfo);
128        }
129        if ($this->structParents !== null) {
130            $dict->set('StructParents', new PdfNumber($this->structParents));
131        }
132        if ($this->id !== null) {
133            $dict->set('ID', $this->id);
134        }
135        if ($this->pz !== null) {
136            $dict->set('PZ', $this->pz);
137        }
138        if ($this->boxColorInfo !== null) {
139            $dict->set('BoxColorInfo', $this->boxColorInfo);
140        }
141        if ($this->af !== null) {
142            $dict->set('AF', $this->af);
143        }
144        if ($this->outputIntents !== null) {
145            $dict->set('OutputIntents', $this->outputIntents);
146        }
147        if ($this->dPart !== null) {
148            $dict->set('DPart', $this->dPart);
149        }
150        if ($this->separationInfo !== null) {
151            $dict->set('SeparationInfo', $this->separationInfo);
152        }
153        if ($this->templateInstantiated !== null) {
154            $dict->set('TemplateInstantiated', $this->templateInstantiated);
155        }
156        if ($this->presSteps !== null) {
157            $dict->set('PresSteps', $this->presSteps);
158        }
159        if ($this->vp !== null) {
160            $dict->set('VP', $this->vp);
161        }
162
163        return $dict->toPdf();
164    }
165}