Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
66 / 66 |
|
100.00% |
13 / 13 |
CRAP | |
100.00% |
1 / 1 |
| Page | |
100.00% |
66 / 66 |
|
100.00% |
13 / 13 |
27 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
20 / 20 |
|
100.00% |
1 / 1 |
13 | |||
| createFromImage | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
2 | |||
| addImage | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| addText | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
| addTextStream | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| addAnnotation | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| addUrl | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| addLink | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| addPath | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| addField | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| importPageObject | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| hasImportedPageObject | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getImportedPageObject | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | /** |
| 4 | * Pop PHP Framework (https://www.popphp.org/) |
| 5 | * |
| 6 | * @link https://github.com/popphp/popphp-framework |
| 7 | * @author Nick Sagona, III <nick@popphp.org> |
| 8 | * @copyright Copyright (c) 2009-2026 Nick Sagona, III |
| 9 | * @license https://www.popphp.org/license New BSD License |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * @namespace |
| 14 | */ |
| 15 | namespace Pop\Pdf\Document; |
| 16 | |
| 17 | use Pop\Pdf\Document\Page\Annotation; |
| 18 | use Pop\Pdf\Build\PdfObject; |
| 19 | use Pop\Pdf\Build\Image; |
| 20 | |
| 21 | /** |
| 22 | * Pdf page class |
| 23 | * |
| 24 | * @category Pop |
| 25 | * @package Pop\Pdf |
| 26 | * @author Nick Sagona, III <nick@popphp.org> |
| 27 | * @copyright Copyright (c) 2009-2026 Nick Sagona, III |
| 28 | * @license https://www.popphp.org/license New BSD License |
| 29 | * @version 6.0.0 |
| 30 | */ |
| 31 | class Page extends AbstractPage |
| 32 | { |
| 33 | |
| 34 | /** |
| 35 | * Imported page object |
| 36 | * @var ?PdfObject\PageObject |
| 37 | */ |
| 38 | protected ?PdfObject\PageObject $importedPageObject = null; |
| 39 | |
| 40 | /** |
| 41 | * Constructor |
| 42 | * |
| 43 | * Instantiate a PDF page. |
| 44 | * |
| 45 | * @throws Exception |
| 46 | */ |
| 47 | public function __construct() |
| 48 | { |
| 49 | $args = func_get_args(); |
| 50 | $width = null; |
| 51 | $height = null; |
| 52 | $index = null; |
| 53 | |
| 54 | if (is_string($args[0]) && array_key_exists($args[0], $this->sizes)) { |
| 55 | $width = $this->sizes[$args[0]]['width']; |
| 56 | $height = $this->sizes[$args[0]]['height']; |
| 57 | |
| 58 | if (isset($args[1]) && is_numeric($args[1])) { |
| 59 | $index = $args[1]; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | if (($width === null) && ($height === null) && (count($args) >= 2)) { |
| 64 | $width = $args[0]; |
| 65 | $height = $args[1]; |
| 66 | |
| 67 | if (isset($args[2]) && is_numeric($args[2])) { |
| 68 | $index = $args[2]; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | if (($width === null) && ($height === null)) { |
| 73 | throw new Exception('Error: The page size was not correctly passed or was not valid.'); |
| 74 | } else { |
| 75 | $this->setWidth($width); |
| 76 | $this->setHeight($height); |
| 77 | if ($index !== null) { |
| 78 | $this->setIndex($index); |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Create a page from an image |
| 85 | * |
| 86 | * @param string $image |
| 87 | * @param int $quality |
| 88 | * @throws Exception|Page\Exception |
| 89 | * @return Page |
| 90 | */ |
| 91 | public static function createFromImage(string $image, int $quality = 70): Page |
| 92 | { |
| 93 | if (!file_exists($image)) { |
| 94 | throw new Exception('Error: That image file does not exist.'); |
| 95 | } |
| 96 | |
| 97 | $imageParser = Image\Parser::createImageFromFile($image, 0, 0); |
| 98 | $imageParser->convertToJpeg($quality); |
| 99 | |
| 100 | $image = $imageParser->getConvertedImage(); |
| 101 | $width = $imageParser->getWidth(); |
| 102 | $height = $imageParser->getHeight(); |
| 103 | $page = new self($width, $height); |
| 104 | $page->addImage(Page\Image::createImageFromFile($image), 0, 0); |
| 105 | |
| 106 | return $page; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Add an image to the PDF page |
| 111 | * |
| 112 | * @param Page\Image $image |
| 113 | * @param int|float $x |
| 114 | * @param int|float $y |
| 115 | * @return Page |
| 116 | */ |
| 117 | public function addImage(Page\Image $image, int|float $x = 0, int|float $y = 0): Page |
| 118 | { |
| 119 | $this->images[] = [ |
| 120 | 'image' => $image, |
| 121 | 'x' => $x, |
| 122 | 'y' => $y |
| 123 | ]; |
| 124 | return $this; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Add text to the PDF page |
| 129 | * |
| 130 | * @param Page\Text|string $text |
| 131 | * @param string $fontStyle (can be either a reference to a font or a style) |
| 132 | * @param int|float $x |
| 133 | * @param int|float $y |
| 134 | * @return Page |
| 135 | */ |
| 136 | public function addText(Page\Text|string $text, string $fontStyle, int|float $x = 0, int|float $y = 0): Page |
| 137 | { |
| 138 | $this->text[] = [ |
| 139 | 'text' => (is_string($text)) ? new Page\Text($text) : $text, |
| 140 | 'font' => $fontStyle, |
| 141 | 'x' => $x, |
| 142 | 'y' => $y |
| 143 | ]; |
| 144 | return $this; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Add text to the PDF page |
| 149 | * |
| 150 | * @param Page\Text\Stream $textStream |
| 151 | * @return Page |
| 152 | */ |
| 153 | public function addTextStream(Page\Text\Stream $textStream): Page |
| 154 | { |
| 155 | $this->textStreams[] = $textStream; |
| 156 | return $this; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Add an annotation to the PDF page |
| 161 | * |
| 162 | * @param Annotation\AbstractAnnotation $annotation |
| 163 | * @param int|float $x |
| 164 | * @param int|float $y |
| 165 | * @return Page |
| 166 | */ |
| 167 | public function addAnnotation(Annotation\AbstractAnnotation $annotation, int|float $x = 0, int|float $y = 0): Page |
| 168 | { |
| 169 | $this->annotations[] = [ |
| 170 | 'annotation' => $annotation, |
| 171 | 'x' => $x, |
| 172 | 'y' => $y |
| 173 | ]; |
| 174 | return $this; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Add a URL annotation to the PDF page |
| 179 | * |
| 180 | * @param Annotation\Url $url |
| 181 | * @param int|float $x |
| 182 | * @param int|float $y |
| 183 | * @return Page |
| 184 | */ |
| 185 | public function addUrl(Annotation\Url $url, int|float $x = 0, int|float $y = 0): Page |
| 186 | { |
| 187 | return $this->addAnnotation($url, $x, $y); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Add a link annotation to the PDF page |
| 192 | * |
| 193 | * @param Annotation\Link $link |
| 194 | * @param int|float $x |
| 195 | * @param int|float $y |
| 196 | * @return Page |
| 197 | */ |
| 198 | public function addLink(Annotation\Link $link, int|float $x = 0, int|float $y = 0): Page |
| 199 | { |
| 200 | return $this->addAnnotation($link, $x, $y); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Add a path to the Pdf page |
| 205 | * |
| 206 | * @param Page\Path $path |
| 207 | * @return Page |
| 208 | */ |
| 209 | public function addPath(Page\Path $path): Page |
| 210 | { |
| 211 | $this->paths[] = $path; |
| 212 | return $this; |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Add a form to the Pdf page |
| 217 | * |
| 218 | * @param Page\Field\AbstractField $field |
| 219 | * @param string $form |
| 220 | * @param int|float $x |
| 221 | * @param int|float $y |
| 222 | * @return Page |
| 223 | */ |
| 224 | public function addField(Page\Field\AbstractField $field, string $form, int|float $x = 0, int|float $y = 0): Page |
| 225 | { |
| 226 | $this->fields[] = [ |
| 227 | 'field' => $field, |
| 228 | 'form' => $form, |
| 229 | 'x' => $x, |
| 230 | 'y' => $y |
| 231 | ]; |
| 232 | return $this; |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Import page object into the page |
| 237 | * |
| 238 | * @param PdfObject\PageObject $page |
| 239 | * @return Page |
| 240 | */ |
| 241 | public function importPageObject(PdfObject\PageObject $page): Page |
| 242 | { |
| 243 | $this->importedPageObject = $page; |
| 244 | return $this; |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Determine if the document has an imported page object |
| 249 | * |
| 250 | * @return bool |
| 251 | */ |
| 252 | public function hasImportedPageObject(): bool |
| 253 | { |
| 254 | return ($this->importedPageObject !== null); |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Get the import page object |
| 259 | * |
| 260 | * @return ?PdfObject\PageObject |
| 261 | */ |
| 262 | public function getImportedPageObject(): ?PdfObject\PageObject |
| 263 | { |
| 264 | return $this->importedPageObject; |
| 265 | } |
| 266 | |
| 267 | } |