Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.54% covered (success)
92.54%
62 / 67
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
Page
92.54% covered (success)
92.54%
62 / 67
0.00% covered (danger)
0.00%
0 / 1
33.45
0.00% covered (danger)
0.00%
0 / 1
 toPdf
92.54% covered (success)
92.54%
62 / 67
0.00% covered (danger)
0.00%
0 / 1
33.45
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;
15use Phpdftk\Pdf\Core\Content\Resources;
16use Phpdftk\Pdf\Core\PdfVersion;
17use Phpdftk\Pdf\Core\RequiresPdfVersion;
18
19/**
20 * PDF Page object (/Type /Page).
21 * Represents a single page in the document.
22 */
23class Page extends PdfObject
24{
25    public const PDF_TYPE = 'Page';
26
27    public ?PdfReference $parent = null;    // /Parent - required
28    public ?Resources $resources = null;    // /Resources - required (inline)
29    public ?PdfArray $mediaBox = null;      // /MediaBox
30    public ?PdfArray $cropBox = null;       // /CropBox
31    public ?PdfArray $bleedBox = null;      // /BleedBox
32    public ?PdfArray $trimBox = null;       // /TrimBox
33    public ?PdfArray $artBox = null;        // /ArtBox
34    /** @var array<int, PdfReference> */
35    public array $contents = [];            // /Contents - refs to content streams
36    public int $rotate = 0;                 // /Rotate
37    /** @var array<int, PdfReference> */
38    public array $annots = [];              // /Annots - refs to annotations
39    public ?PdfReference $group = null;     // /Group
40    public ?PdfReference $thumb = null;     // /Thumb
41    public ?PdfNumber $userUnit = null;     // /UserUnit
42    public ?int $structParents = null;      // /StructParents
43    public Serializable|null $transition = null; // /Trans - TransitionDict or PdfDictionary
44    public ?PdfNumber $dur = null;          // /Dur
45    public ?PdfReference $metadata = null;  // /Metadata - XMP stream reference
46    public ?PdfName $tabs = null;            // /Tabs - tab order
47    public ?PdfString $id = null;            // /ID - page identifier
48    public ?PdfNumber $pz = null;            // /PZ - preferred zoom
49    public ?PdfReference $aa = null;         // /AA - additional actions dict
50    public ?PdfReference $pieceInfo = null;  // /PieceInfo - application data
51    public BoxColorInfo|PdfDictionary|null $boxColorInfo = null; // /BoxColorInfo
52    public ?PdfArray $b = null;             // /B - article beads
53    #[RequiresPdfVersion(PdfVersion::V2_0)]
54    public ?PdfArray $af = null;            // /AF - associated files
55    #[RequiresPdfVersion(PdfVersion::V1_4)]
56    public ?PdfArray $outputIntents = null; // /OutputIntents - page-level output intents
57    #[RequiresPdfVersion(PdfVersion::V2_0)]
58    public ?PdfReference $dPart = null;     // /DPart - document part reference (PDF 2.0)
59    public ?PdfDictionary $separationInfo = null; // /SeparationInfo
60    public ?PdfName $templateInstantiated = null; // /TemplateInstantiated
61    public ?PdfReference $presSteps = null; // /PresSteps - presentation steps
62    public ?PdfArray $vp = null;            // /VP - viewport array
63
64    public function toPdf(): string
65    {
66        $dict = new PdfDictionary();
67        $dict->set('Type', new PdfName(self::PDF_TYPE));
68
69        if ($this->parent !== null) {
70            $dict->set('Parent', $this->parent);
71        }
72        if ($this->resources !== null) {
73            $dict->set('Resources', $this->resources);
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
91        // /Contents: single ref or array of refs
92        if (count($this->contents) === 1) {
93            $dict->set('Contents', $this->contents[0]);
94        } elseif (count($this->contents) > 1) {
95            $dict->set('Contents', new PdfArray($this->contents));
96        }
97
98        if ($this->rotate !== 0) {
99            $dict->set('Rotate', new PdfNumber($this->rotate));
100        }
101        if (!empty($this->annots)) {
102            $dict->set('Annots', new PdfArray($this->annots));
103        }
104        if ($this->group !== null) {
105            $dict->set('Group', $this->group);
106        }
107        if ($this->thumb !== null) {
108            $dict->set('Thumb', $this->thumb);
109        }
110        if ($this->userUnit !== null) {
111            $dict->set('UserUnit', $this->userUnit);
112        }
113        if ($this->structParents !== null) {
114            $dict->set('StructParents', new PdfNumber($this->structParents));
115        }
116        if ($this->transition !== null) {
117            $dict->set('Trans', $this->transition);
118        }
119        if ($this->dur !== null) {
120            $dict->set('Dur', $this->dur);
121        }
122        if ($this->metadata !== null) {
123            $dict->set('Metadata', $this->metadata);
124        }
125        if ($this->tabs !== null) {
126            $dict->set('Tabs', $this->tabs);
127        }
128        if ($this->id !== null) {
129            $dict->set('ID', $this->id);
130        }
131        if ($this->pz !== null) {
132            $dict->set('PZ', $this->pz);
133        }
134        if ($this->aa !== null) {
135            $dict->set('AA', $this->aa);
136        }
137        if ($this->pieceInfo !== null) {
138            $dict->set('PieceInfo', $this->pieceInfo);
139        }
140        if ($this->boxColorInfo !== null) {
141            $dict->set('BoxColorInfo', $this->boxColorInfo);
142        }
143        if ($this->b !== null) {
144            $dict->set('B', $this->b);
145        }
146        if ($this->af !== null) {
147            $dict->set('AF', $this->af);
148        }
149        if ($this->outputIntents !== null) {
150            $dict->set('OutputIntents', $this->outputIntents);
151        }
152        if ($this->dPart !== null) {
153            $dict->set('DPart', $this->dPart);
154        }
155        if ($this->separationInfo !== null) {
156            $dict->set('SeparationInfo', $this->separationInfo);
157        }
158        if ($this->templateInstantiated !== null) {
159            $dict->set('TemplateInstantiated', $this->templateInstantiated);
160        }
161        if ($this->presSteps !== null) {
162            $dict->set('PresSteps', $this->presSteps);
163        }
164        if ($this->vp !== null) {
165            $dict->set('VP', $this->vp);
166        }
167
168        return $dict->toPdf();
169    }
170}