44 * This file is part of FPDI
55 *
66 * @package setasign\Fpdi
7- * @copyright Copyright (c) 2020 Setasign GmbH & Co. KG (https://www.setasign.com)
7+ * @copyright Copyright (c) 2023 Setasign GmbH & Co. KG (https://www.setasign.com)
88 * @license http://opensource.org/licenses/mit-license The MIT License
99 */
1010
@@ -124,17 +124,18 @@ protected function setMinPdfVersion($pdfVersion)
124124 * Get a new pdf parser instance.
125125 *
126126 * @param StreamReader $streamReader
127+ * @param array $parserParams Individual parameters passed to the parser instance.
127128 * @return PdfParser|FpdiPdfParser
128129 */
129- protected function getPdfParserInstance (StreamReader $ streamReader )
130+ protected function getPdfParserInstance (StreamReader $ streamReader, array $ parserParams = [] )
130131 {
131132 // note: if you get an exception here - turn off errors/warnings on not found for your autoloader.
132133 // psr-4 (https://www.php-fig.org/psr/psr-4/) says: Autoloader implementations MUST NOT throw
133134 // exceptions, MUST NOT raise errors of any level, and SHOULD NOT return a value.
134135 /** @noinspection PhpUndefinedClassInspection */
135136 if (\class_exists (FpdiPdfParser::class)) {
136137 /** @noinspection PhpUndefinedClassInspection */
137- return new FpdiPdfParser ($ streamReader );
138+ return new FpdiPdfParser ($ streamReader, $ parserParams );
138139 }
139140
140141 return new PdfParser ($ streamReader );
@@ -145,9 +146,10 @@ protected function getPdfParserInstance(StreamReader $streamReader)
145146 *
146147 * @param string|resource|PdfReader|StreamReader $file An open file descriptor, a path to a file, a PdfReader
147148 * instance or a StreamReader instance.
149+ * @param array $parserParams Individual parameters passed to the parser instance.
148150 * @return string
149151 */
150- protected function getPdfReaderId ($ file )
152+ protected function getPdfReaderId ($ file, array $ parserParams = [] )
151153 {
152154 if (\is_resource ($ file )) {
153155 $ id = (string ) $ file ;
@@ -178,7 +180,7 @@ protected function getPdfReaderId($file)
178180 $ streamReader = $ file ;
179181 }
180182
181- $ reader = new PdfReader ($ this ->getPdfParserInstance ($ streamReader ));
183+ $ reader = new PdfReader ($ this ->getPdfParserInstance ($ streamReader, $ parserParams ));
182184 /** @noinspection OffsetOperationsInspection */
183185 $ this ->readers [$ id ] = $ reader ;
184186
@@ -211,7 +213,24 @@ protected function getPdfReader($id)
211213 */
212214 public function setSourceFile ($ file )
213215 {
214- $ this ->currentReaderId = $ this ->getPdfReaderId ($ file );
216+ return $ this ->setSourceFileWithParserParams ($ file );
217+ }
218+
219+ /**
220+ * Set the source PDF file with parameters which are passed to the parser instance.
221+ *
222+ * This method allows us to pass e.g. authentication information to the parser instance.
223+ *
224+ * @param string|resource|StreamReader $file Path to the file or a stream resource or a StreamReader instance.
225+ * @param array $parserParams Individual parameters passed to the parser instance.
226+ * @return int The page count of the PDF document.
227+ * @throws CrossReferenceException
228+ * @throws PdfParserException
229+ * @throws PdfTypeException
230+ */
231+ public function setSourceFileWithParserParams ($ file , array $ parserParams = [])
232+ {
233+ $ this ->currentReaderId = $ this ->getPdfReaderId ($ file , $ parserParams );
215234 $ this ->objectsToCopy [$ this ->currentReaderId ] = [];
216235
217236 $ reader = $ this ->getPdfReader ($ this ->currentReaderId );
0 commit comments