Skip to content

Commit aeab673

Browse files
committed
Throw exceptions on pdf size and orientation parsing
1 parent 1bf6afb commit aeab673

File tree

1 file changed

+29
-29
lines changed

1 file changed

+29
-29
lines changed

src/Util/Util.php

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@
3232
use Exception;
3333
use Firstred\PostNL\Exception\InvalidArgumentException;
3434
use setasign\Fpdi\Fpdi;
35+
use setasign\Fpdi\PdfParser\PdfParserException;
3536
use setasign\Fpdi\PdfParser\StreamReader;
37+
use setasign\Fpdi\PdfReader\PdfReaderException;
3638

3739
/**
3840
* Class Util.
@@ -85,38 +87,36 @@ public static function urlEncode($arr, $prefix = null)
8587
/**
8688
* @param string $pdf Raw PDF string
8789
*
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
9195
*/
9296
public static function getPdfSizeAndOrientation($pdf)
9397
{
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';
120120
}
121121

122122
return [

0 commit comments

Comments
 (0)