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

Development #225

Merged
merged 5 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
vendor
/.idea
/**/vendor
/phpstan.phar
/composer.phar
/tests/visual/**/compare/
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ FPDI - Free PDF Document Importer

[![Latest Stable Version](https://poser.pugx.org/setasign/fpdi/v/stable.svg)](https://packagist.org/packages/setasign/fpdi)
[![Total Downloads](https://poser.pugx.org/setasign/fpdi/downloads.svg)](https://packagist.org/packages/setasign/fpdi)
[![Latest Unstable Version](https://poser.pugx.org/setasign/fpdi/v/unstable.svg)](https://packagist.org/packages/setasign/fpdi)
[![License](https://poser.pugx.org/setasign/fpdi/license.svg)](https://packagist.org/packages/setasign/fpdi)

:heavy_exclamation_mark: This document refers to FPDI 2. Version 1 is deprecated and development is discontinued. :heavy_exclamation_mark:
Expand Down
61 changes: 35 additions & 26 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/FpdfTplTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public function useTemplate($tpl, $x = 0, $y = 0, $width = null, $height = null,
unset($x['tpl']);
\extract($x, EXTR_IF_EXISTS);
/** @noinspection NotOptimalIfConditionsInspection */
/** @noinspection PhpConditionAlreadyCheckedInspection */
/** @phpstan-ignore function.alreadyNarrowedType */
if (\is_array($x)) {
$x = 0;
}
Expand Down
1 change: 1 addition & 0 deletions src/FpdiTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@ public function useImportedPage($pageId, $x = 0, $y = 0, $width = null, $height
unset($x['pageId']);
\extract($x, EXTR_IF_EXISTS);
/** @noinspection NotOptimalIfConditionsInspection */
/** @phpstan-ignore function.alreadyNarrowedType */
if (\is_array($x)) {
$x = 0;
}
Expand Down
53 changes: 27 additions & 26 deletions src/PdfParser/PdfParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,33 +281,34 @@ public function readValue($token = null, $expectedType = null)

default:
if (\is_numeric($token)) {
if (($token2 = $this->tokenizer->getNextToken()) !== false) {
if (\is_numeric($token2) && ($token3 = $this->tokenizer->getNextToken()) !== false) {
switch ($token3) {
case 'obj':
if ($expectedType !== null && $expectedType !== PdfIndirectObject::class) {
throw new Type\PdfTypeException(
'Got unexpected token type.',
Type\PdfTypeException::INVALID_DATA_TYPE
);
}

return $this->parsePdfIndirectObject((int)$token, (int)$token2);
case 'R':
if (
$expectedType !== null &&
$expectedType !== PdfIndirectObjectReference::class
) {
throw new Type\PdfTypeException(
'Got unexpected token type.',
Type\PdfTypeException::INVALID_DATA_TYPE
);
}

return PdfIndirectObjectReference::create((int)$token, (int)$token2);
$token2 = $this->tokenizer->getNextToken();
if ($token2 !== false) {
if (\is_numeric($token2)) {
$token3 = $this->tokenizer->getNextToken();
if ($token3 === 'obj') {
if ($expectedType !== null && $expectedType !== PdfIndirectObject::class) {
throw new Type\PdfTypeException(
'Got unexpected token type.',
Type\PdfTypeException::INVALID_DATA_TYPE
);
}

return $this->parsePdfIndirectObject((int) $token, (int) $token2);
} elseif ($token3 === 'R') {
if (
$expectedType !== null &&
$expectedType !== PdfIndirectObjectReference::class
) {
throw new Type\PdfTypeException(
'Got unexpected token type.',
Type\PdfTypeException::INVALID_DATA_TYPE
);
}

return PdfIndirectObjectReference::create((int) $token, (int) $token2);
} elseif ($token3 !== false) {
$this->tokenizer->pushStack($token3);
}

$this->tokenizer->pushStack($token3);
}

$this->tokenizer->pushStack($token2);
Expand Down
10 changes: 7 additions & 3 deletions src/PdfParser/StreamReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -418,16 +418,20 @@ public function reset($pos = 0, $length = 200)
\fseek($this->stream, $pos);

$this->position = $pos;
$this->buffer = $length > 0 ? \fread($this->stream, $length) : '';
$this->bufferLength = \strlen($this->buffer);
$this->offset = 0;
if ($length > 0) {
$this->buffer = (string) \fread($this->stream, $length);
} else {
$this->buffer = '';
}
$this->bufferLength = \strlen($this->buffer);

// If a stream wrapper is in use it is possible that
// length values > 8096 will be ignored, so use the
// increaseLength()-method to correct that behavior
if ($this->bufferLength < $length && $this->increaseLength($length - $this->bufferLength)) {
// increaseLength parameter is $minLength, so cut to have only the required bytes in the buffer
$this->buffer = \substr($this->buffer, 0, $length);
$this->buffer = (string) \substr($this->buffer, 0, $length);
$this->bufferLength = \strlen($this->buffer);
}
}
Expand Down
9 changes: 3 additions & 6 deletions src/PdfParser/Tokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function pushStack($token)
/**
* Get next token.
*
* @return bool|string
* @return false|string
*/
public function getNextToken()
{
Expand Down Expand Up @@ -117,11 +117,8 @@ public function getNextToken()
} while (
// Break the loop if a delimiter or white space char is matched
// in the current buffer or increase the buffers length
$lastBuffer !== false &&
(
$bufferOffset + $pos === \strlen($lastBuffer) &&
$this->streamReader->increaseLength()
)
$bufferOffset + $pos === \strlen($lastBuffer)
&& $this->streamReader->increaseLength()
);

$result = \substr($lastBuffer, $bufferOffset - 1, $pos + 1);
Expand Down
9 changes: 4 additions & 5 deletions src/PdfParser/Type/PdfDictionary.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,13 @@ public static function parse(Tokenizer $tokenizer, StreamReader $streamReader, P
if (!($key instanceof PdfName)) {
$lastToken = null;
// ignore all other entries and search for the closing brackets
while (($token = $tokenizer->getNextToken()) !== '>' && $token !== false && $lastToken !== '>') {
while (($token = $tokenizer->getNextToken()) !== '>' || $lastToken !== '>') {
if ($token === false) {
return false;
}
$lastToken = $token;
}

if ($token === false) {
return false;
}

break;
}

Expand Down
2 changes: 1 addition & 1 deletion src/PdfReader/PdfReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public function getPageCount()
/**
* Get a page instance.
*
* @param int $pageNumber
* @param int|numeric-string $pageNumber
* @return Page
* @throws PdfTypeException
* @throws CrossReferenceException
Expand Down
8 changes: 8 additions & 0 deletions tests/functional/PdfParser/Type/PdfArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ public function parseProvider()
PdfNumeric::create(123),
PdfNumeric::create(456)
]
],
[
"123 456 789]",
[
PdfNumeric::create(123),
PdfNumeric::create(456),
PdfNumeric::create(789)
]
]
];

Expand Down
15 changes: 14 additions & 1 deletion tests/functional/PdfParser/Type/PdfDictionaryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ public function parseProvider()
'A' => PdfString::create('hello')
])
],
[
'/A (hello) B [<>]>>',
PdfDictionary::create([
'A' => PdfString::create('hello')
])
],
// this faulty structure is currently not handled
// [
// '/A (hello) B [<<>>]>>',
// PdfDictionary::create([
// 'A' => PdfString::create('hello')
// ])
// ],
[
'/A (1) /B (2) /C <</D (4) /E (5)>> >>',
PdfDictionary::create([
Expand Down Expand Up @@ -120,8 +133,8 @@ public function testParse($in, $expectedResult)
$result = PdfDictionary::parse($tokenizer, $stream, $parser);

$this->assertInstanceOf(PdfDictionary::class, $result);

$this->assertEquals($expectedResult->value, $result->value);
$this->assertFalse($tokenizer->getNextToken());
}

public function testParseBehind()
Expand Down