|
35 | 35 | use Exception; |
36 | 36 | use Firstred\PostNL\Exception\InvalidArgumentException; |
37 | 37 | use setasign\Fpdi\Fpdi; |
| 38 | +use setasign\Fpdi\PdfParser\PdfParserException; |
38 | 39 | use setasign\Fpdi\PdfParser\StreamReader; |
| 40 | +use setasign\Fpdi\PdfReader\PdfReaderException; |
39 | 41 |
|
40 | 42 | /** |
41 | 43 | * Class Util. |
@@ -94,35 +96,33 @@ public static function urlEncode(array $arr, ?string $prefix = null): string |
94 | 96 | * Sizes are in mm |
95 | 97 | * |
96 | 98 | * @since 1.0.0 |
| 99 | + * |
| 100 | + * @throws PdfParserException|PdfReaderException |
97 | 101 | */ |
98 | 102 | public static function getPdfSizeAndOrientation(string $pdf): bool|array|string |
99 | 103 | { |
100 | | - try { |
101 | | - $fpdi = new Fpdi(orientation: 'P', unit: 'mm'); |
102 | | - $fpdi->setSourceFile(file: StreamReader::createByString(content: $pdf)); |
103 | | - // import page 1 |
104 | | - $tplIdx1 = $fpdi->importPage(pageNumber: 1); |
105 | | - $size = $fpdi->getTemplateSize(tpl: $tplIdx1); |
106 | | - $width = $size['width']; |
107 | | - $height = $size['height']; |
108 | | - $orientation = $size['orientation']; |
109 | | - |
110 | | - $length = 'P' === $orientation ? $height : $width; |
111 | | - if ($length >= (148 - static::ERROR_MARGIN) && $length <= (148 + static::ERROR_MARGIN)) { |
112 | | - $iso = 'A6'; |
113 | | - } elseif ($length >= (210 - static::ERROR_MARGIN) && $length <= (210 + static::ERROR_MARGIN)) { |
114 | | - $iso = 'A5'; |
115 | | - } elseif ($length >= (420 - static::ERROR_MARGIN) && $length <= (420 + static::ERROR_MARGIN)) { |
116 | | - $iso = 'A3'; |
117 | | - } elseif ($length >= (594 - static::ERROR_MARGIN) && $length <= (594 + static::ERROR_MARGIN)) { |
118 | | - $iso = 'A2'; |
119 | | - } elseif ($length >= (841 - static::ERROR_MARGIN) && $length <= (841 + static::ERROR_MARGIN)) { |
120 | | - $iso = 'A1'; |
121 | | - } else { |
122 | | - $iso = 'A4'; |
123 | | - } |
124 | | - } catch (Exception $e) { |
125 | | - return false; |
| 104 | + $fpdi = new Fpdi(orientation: 'P', unit: 'mm'); |
| 105 | + $fpdi->setSourceFile(file: StreamReader::createByString(content: $pdf)); |
| 106 | + // import page 1 |
| 107 | + $tplIdx1 = $fpdi->importPage(pageNumber: 1); |
| 108 | + $size = $fpdi->getTemplateSize(tpl: $tplIdx1); |
| 109 | + $width = $size['width']; |
| 110 | + $height = $size['height']; |
| 111 | + $orientation = $size['orientation']; |
| 112 | + |
| 113 | + $length = 'P' === $orientation ? $height : $width; |
| 114 | + if ($length >= (148 - static::ERROR_MARGIN) && $length <= (148 + static::ERROR_MARGIN)) { |
| 115 | + $iso = 'A6'; |
| 116 | + } elseif ($length >= (210 - static::ERROR_MARGIN) && $length <= (210 + static::ERROR_MARGIN)) { |
| 117 | + $iso = 'A5'; |
| 118 | + } elseif ($length >= (420 - static::ERROR_MARGIN) && $length <= (420 + static::ERROR_MARGIN)) { |
| 119 | + $iso = 'A3'; |
| 120 | + } elseif ($length >= (594 - static::ERROR_MARGIN) && $length <= (594 + static::ERROR_MARGIN)) { |
| 121 | + $iso = 'A2'; |
| 122 | + } elseif ($length >= (841 - static::ERROR_MARGIN) && $length <= (841 + static::ERROR_MARGIN)) { |
| 123 | + $iso = 'A1'; |
| 124 | + } else { |
| 125 | + $iso = 'A4'; |
126 | 126 | } |
127 | 127 |
|
128 | 128 | return [ |
|
0 commit comments