11#!/usr/bin/env php
22<?php declare (strict_types=1 );
33
4+ use PrinsFrank \PdfParser \Document \Security \StandardSecurity ;
45use PrinsFrank \PdfParser \Exception \PdfParserException ;
56use PrinsFrank \PdfParser \PdfParser ;
67use 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