Skip to content

Commit

Permalink
Some more exceptions (#745)
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasLandauer authored Dec 3, 2024
1 parent 1e38ca9 commit f44ada0
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/Smalot/PdfParser/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
namespace Smalot\PdfParser;

use Smalot\PdfParser\Encoding\PDFDocEncoding;
use Smalot\PdfParser\Exception\MissingCatalogException;

/**
* Technical references :
Expand Down Expand Up @@ -379,7 +380,7 @@ public function getFirstFont(): ?Font
/**
* @return Page[]
*
* @throws \Exception
* @throws MissingCatalogException
*/
public function getPages()
{
Expand Down Expand Up @@ -415,7 +416,7 @@ public function getPages()
return array_values($pages);
}

throw new \Exception('Missing catalog.');
throw new MissingCatalogException('Missing catalog.');
}

public function getText(?int $pageLimit = null): string
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Smalot\PdfParser\Exception;

/**
* This exception is thrown when an invalid dictionary object is encountered.
*/
class InvalidDictionaryObjectException extends \Exception
{
}
12 changes: 12 additions & 0 deletions src/Smalot/PdfParser/Exception/MissingCatalogException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Smalot\PdfParser\Exception;

/**
* This exception is thrown when the catalog is missing.
*/
class MissingCatalogException extends \Exception
{
}
5 changes: 3 additions & 2 deletions src/Smalot/PdfParser/PDFObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

namespace Smalot\PdfParser;

use Smalot\PdfParser\Exception\InvalidDictionaryObjectException;
use Smalot\PdfParser\XObject\Form;
use Smalot\PdfParser\XObject\Image;

Expand Down Expand Up @@ -559,15 +560,15 @@ private function getTJUsingFontFallback(Font $font, array $command, ?Page $page
*
* @internal
*
* @throws \Exception
* @throws InvalidDictionaryObjectException
*/
public function parseDictionary(string $dictionary): array
{
// Normalize whitespace
$dictionary = preg_replace(['/\r/', '/\n/', '/\s{2,}/'], ' ', trim($dictionary));

if ('<<' != substr($dictionary, 0, 2)) {
throw new \Exception('Not a valid dictionary object.');
throw new InvalidDictionaryObjectException('Not a valid dictionary object.');
}

$parsed = [];
Expand Down

0 comments on commit f44ada0

Please sign in to comment.