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