Skip to content

Commit 87da337

Browse files
committed
Throw exceptions on pdf size and orientation parsing
1 parent 34f7371 commit 87da337

File tree

1 file changed

+26
-26
lines changed

1 file changed

+26
-26
lines changed

src/Util/Util.php

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@
3535
use Exception;
3636
use Firstred\PostNL\Exception\InvalidArgumentException;
3737
use setasign\Fpdi\Fpdi;
38+
use setasign\Fpdi\PdfParser\PdfParserException;
3839
use setasign\Fpdi\PdfParser\StreamReader;
40+
use setasign\Fpdi\PdfReader\PdfReaderException;
3941

4042
/**
4143
* Class Util.
@@ -94,35 +96,33 @@ public static function urlEncode(array $arr, ?string $prefix = null): string
9496
* Sizes are in mm
9597
*
9698
* @since 1.0.0
99+
*
100+
* @throws PdfParserException|PdfReaderException
97101
*/
98102
public static function getPdfSizeAndOrientation(string $pdf): bool|array|string
99103
{
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';
126126
}
127127

128128
return [

0 commit comments

Comments
 (0)