Skip to content

Commit 6792baf

Browse files
authored
Properly support encrypted documents in sample generation (#365)
1 parent 6f0fbcb commit 6792baf

4 files changed

Lines changed: 19 additions & 12 deletions

File tree

src/Document/Document.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function __construct(
4242
public readonly Stream $stream,
4343
public readonly Version $version,
4444
public readonly CrossReferenceSource $crossReferenceSource,
45-
?StandardSecurity $security,
45+
public readonly ?StandardSecurity $security,
4646
) {
4747
$this->fileEncryptionKey = $this->getFileEncryptionKeyFromSecurity($security);
4848
}

src/Document/Security/StandardSecurity.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515

1616
public function __construct(
1717
#[SensitiveParameter]
18-
private ?string $userPassword = null,
18+
public ?string $userPassword = null,
1919
#[SensitiveParameter]
20-
private ?string $ownerPassword = null,
20+
public ?string $ownerPassword = null,
2121
) {}
2222

2323
/** @throws ParseFailureException|NotImplementedException */
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
# yaml-language-server: $schema=../../schema.json
2-
version: 1.7
2+
version: '1.7'
33
userPassword: hello
44
ownerPassword: hello
55
fileEncryptionKey: 50b2fc01398fa68e4b8c96bd3a10c589
66
title: null
7-
producer: LibreOffice 24.2
7+
producer: 'LibreOffice 24.2'
88
author: null
99
creator: Writer
1010
creationDate: 2024-11-22T22:24:39+01:00
1111
modificationDate: null
1212
pages:
1313
-
14-
content: |-2
15-
Hello world
14+
content: 'Hello world'

tests/Samples/update-content

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env php
22
<?php declare(strict_types=1);
33

4+
use PrinsFrank\PdfParser\Document\Security\StandardSecurity;
45
use PrinsFrank\PdfParser\Exception\PdfParserException;
56
use PrinsFrank\PdfParser\PdfParser;
67
use Symfony\Component\Yaml\Yaml;
@@ -13,8 +14,15 @@ foreach (array_diff(scandir($filesDir = __DIR__ . '/files'), ['..', '.']) as $sa
1314
throw new RuntimeException(sprintf('File "%s" does not exist', $pdfPath));
1415
}
1516

17+
$userPassword = $ownerPassword = null;
18+
if (file_exists($contentsYamlPath = $sampleFolder . DIRECTORY_SEPARATOR . 'contents.yml')) {
19+
$currentYamlContent = Yaml::parseFile($contentsYamlPath, Yaml::PARSE_OBJECT_FOR_MAP | Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE | Yaml::PARSE_DATETIME);
20+
$userPassword = $currentYamlContent->userPassword;
21+
$ownerPassword = $currentYamlContent->ownerPassword;
22+
}
23+
1624
try {
17-
$parsedPDF = (new PdfParser())->parseFile($pdfPath);
25+
$parsedPDF = (new PdfParser())->parseFile($pdfPath, security: new StandardSecurity($userPassword, $ownerPassword));
1826
} catch (PdfParserException $e) {
1927
echo sprintf('Failed to parse file "%s": %s', $pdfPath, $e->getMessage()) . PHP_EOL;
2028
continue;
@@ -46,9 +54,9 @@ foreach (array_diff(scandir($filesDir = __DIR__ . '/files'), ['..', '.']) as $sa
4654
$yamlContent = Yaml::dump(
4755
[
4856
'version' => $parsedPDF->version->value,
49-
'userPassword' => null,
50-
'ownerPassword' => null,
51-
'fileEncryptionKey' => null,
57+
'userPassword' => $parsedPDF->security->userPassword,
58+
'ownerPassword' => $parsedPDF->security->ownerPassword,
59+
'fileEncryptionKey' => $parsedPDF->fileEncryptionKey !== null ? bin2hex($parsedPDF->fileEncryptionKey->value) : null,
5260
'title' => $parsedPDF->getInformationDictionary()?->getTitle(),
5361
'producer' => $parsedPDF->getInformationDictionary()?->getProducer(),
5462
'author' => $parsedPDF->getInformationDictionary()?->getAuthor(),
@@ -63,7 +71,7 @@ foreach (array_diff(scandir($filesDir = __DIR__ . '/files'), ['..', '.']) as $sa
6371
);
6472

6573
file_put_contents(
66-
$sampleFolder . DIRECTORY_SEPARATOR . 'contents.yml',
74+
$contentsYamlPath,
6775
'# yaml-language-server: $schema=../../schema.json' . PHP_EOL . trim($yamlContent, PHP_EOL) . PHP_EOL
6876
);
6977
}

0 commit comments

Comments
 (0)