Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to ignore PDF encryption check #632

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions doc/Usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,17 @@ foreach ($pages as $page) {
];
}
```

## Ignoring PDF encryption

In some cases PDF files may be internally marked as encrypted even though the content is not encrypted and can be read.
This can be caused by the PDF being created by a tool that does not properly set the encryption flag.
If you are sure that the PDF is not encrypted, you can ignore the encryption flag by setting the `ignoreEncryption` flag to `true` in the `Config` object.

```php
$config = new \Smalot\PdfParser\Config();
$config->setIgnoreEncryption(true);

$parser = new \Smalot\PdfParser\Parser([], $config);
$pdf = $parser->parseFile('document.pdf');
```
17 changes: 17 additions & 0 deletions src/Smalot/PdfParser/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ class Config
*/
private $dataTmFontInfoHasToBeIncluded = false;

/**
* Whether to attempt to read PDFs even if they are marked as encrypted.
*
* @var bool
*/
private $ignoreEncryption = false;

public function getFontSpaceLimit()
{
return $this->fontSpaceLimit;
Expand Down Expand Up @@ -151,4 +158,14 @@ public function setDataTmFontInfoHasToBeIncluded(bool $dataTmFontInfoHasToBeIncl
{
$this->dataTmFontInfoHasToBeIncluded = $dataTmFontInfoHasToBeIncluded;
}

public function getIgnoreEncryption(): bool
{
return $this->ignoreEncryption;
}

public function setIgnoreEncryption(bool $ignoreEncryption): void
{
$this->ignoreEncryption = $ignoreEncryption;
}
}
2 changes: 1 addition & 1 deletion src/Smalot/PdfParser/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public function parseContent(string $content): Document
// Create structure from raw data.
list($xref, $data) = $this->rawDataParser->parseData($content);

if (isset($xref['trailer']['encrypt'])) {
if (isset($xref['trailer']['encrypt']) && !$this->config->getIgnoreEncryption()) {
k00ni marked this conversation as resolved.
Show resolved Hide resolved
throw new \Exception('Secured pdf file are currently not supported.');
}

Expand Down
Loading