|
32 | 32 | use Exception; |
33 | 33 | use Firstred\PostNL\Exception\InvalidArgumentException; |
34 | 34 | use setasign\Fpdi\Fpdi; |
| 35 | +use setasign\Fpdi\PdfParser\PdfParserException; |
35 | 36 | use setasign\Fpdi\PdfParser\StreamReader; |
| 37 | +use setasign\Fpdi\PdfReader\PdfReaderException; |
36 | 38 |
|
37 | 39 | /** |
38 | 40 | * Class Util. |
@@ -85,38 +87,36 @@ public static function urlEncode($arr, $prefix = null) |
85 | 87 | /** |
86 | 88 | * @param string $pdf Raw PDF string |
87 | 89 | * |
88 | | - * @return array|false|string Returns an array with the dimensions or ISO size and orientation |
89 | | - * The orientation is in FPDF format, so L for Landscape and P for Portrait |
90 | | - * Sizes are in mm |
| 90 | + * @return array Returns an array with the dimensions or ISO size and orientation |
| 91 | + * The orientation is in FPDF format, so L for Landscape and P for Portrait |
| 92 | + * Sizes are in mm |
| 93 | + * |
| 94 | + * @throws PdfParserException|PdfReaderException |
91 | 95 | */ |
92 | 96 | public static function getPdfSizeAndOrientation($pdf) |
93 | 97 | { |
94 | | - try { |
95 | | - $fpdi = new Fpdi('P', 'mm'); |
96 | | - $fpdi->setSourceFile(StreamReader::createByString($pdf)); |
97 | | - // import page 1 |
98 | | - $tplIdx1 = $fpdi->importPage(1); |
99 | | - $size = $fpdi->getTemplateSize($tplIdx1); |
100 | | - $width = $size['width']; |
101 | | - $height = $size['height']; |
102 | | - $orientation = $size['orientation']; |
103 | | - |
104 | | - $length = 'P' === $orientation ? $height : $width; |
105 | | - if ($length >= (148 - static::ERROR_MARGIN) && $length <= (148 + static::ERROR_MARGIN)) { |
106 | | - $iso = 'A6'; |
107 | | - } elseif ($length >= (210 - static::ERROR_MARGIN) && $length <= (210 + static::ERROR_MARGIN)) { |
108 | | - $iso = 'A5'; |
109 | | - } elseif ($length >= (420 - static::ERROR_MARGIN) && $length <= (420 + static::ERROR_MARGIN)) { |
110 | | - $iso = 'A3'; |
111 | | - } elseif ($length >= (594 - static::ERROR_MARGIN) && $length <= (594 + static::ERROR_MARGIN)) { |
112 | | - $iso = 'A2'; |
113 | | - } elseif ($length >= (841 - static::ERROR_MARGIN) && $length <= (841 + static::ERROR_MARGIN)) { |
114 | | - $iso = 'A1'; |
115 | | - } else { |
116 | | - $iso = 'A4'; |
117 | | - } |
118 | | - } catch (Exception $e) { |
119 | | - return false; |
| 98 | + $fpdi = new Fpdi('P', 'mm'); |
| 99 | + $fpdi->setSourceFile(StreamReader::createByString($pdf)); |
| 100 | + // import page 1 |
| 101 | + $tplIdx1 = $fpdi->importPage(1); |
| 102 | + $size = $fpdi->getTemplateSize($tplIdx1); |
| 103 | + $width = $size['width']; |
| 104 | + $height = $size['height']; |
| 105 | + $orientation = $size['orientation']; |
| 106 | + |
| 107 | + $length = 'P' === $orientation ? $height : $width; |
| 108 | + if ($length >= (148 - static::ERROR_MARGIN) && $length <= (148 + static::ERROR_MARGIN)) { |
| 109 | + $iso = 'A6'; |
| 110 | + } elseif ($length >= (210 - static::ERROR_MARGIN) && $length <= (210 + static::ERROR_MARGIN)) { |
| 111 | + $iso = 'A5'; |
| 112 | + } elseif ($length >= (420 - static::ERROR_MARGIN) && $length <= (420 + static::ERROR_MARGIN)) { |
| 113 | + $iso = 'A3'; |
| 114 | + } elseif ($length >= (594 - static::ERROR_MARGIN) && $length <= (594 + static::ERROR_MARGIN)) { |
| 115 | + $iso = 'A2'; |
| 116 | + } elseif ($length >= (841 - static::ERROR_MARGIN) && $length <= (841 + static::ERROR_MARGIN)) { |
| 117 | + $iso = 'A1'; |
| 118 | + } else { |
| 119 | + $iso = 'A4'; |
120 | 120 | } |
121 | 121 |
|
122 | 122 | return [ |
|
0 commit comments