Skip to content

Commit

Permalink
Merge pull request #101 from Setasign/development
Browse files Browse the repository at this point in the history
Handle streams with invisible content (white signs) and a length of 0.
  • Loading branch information
JanSlabon authored Apr 28, 2020
2 parents 5277614 + 7696e01 commit 50c3888
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/PdfParser/Type/PdfStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,13 @@ protected function extractStream()
}
}

// There are streams in the wild, which have only white signs in them but need to be parsed manually due
// to a problem encountered before (e.g. Length === 0). We should set them to empty streams to avoid problems
// in further processing (e.g. applying of filters).
if (trim($buffer) === '') {
$buffer = '';
}

return $buffer;
}

Expand Down
22 changes: 22 additions & 0 deletions tests/functional/PdfParser/Type/PdfStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use setasign\Fpdi\PdfParser\Type\PdfDictionary;
use setasign\Fpdi\PdfParser\Type\PdfIndirectObject;
use setasign\Fpdi\PdfParser\Type\PdfIndirectObjectReference;
use setasign\Fpdi\PdfParser\Type\PdfName;
use setasign\Fpdi\PdfParser\Type\PdfNull;
use setasign\Fpdi\PdfParser\Type\PdfNumeric;
use setasign\Fpdi\PdfParser\Type\PdfStream;
Expand Down Expand Up @@ -266,6 +267,27 @@ public function testParseWithZeroLength()
$this->assertSame($streamContent, $result->getStream());
}

public function testParseWithZeroLengthButAMixOfLinefeedAndCarrigeReturn()
{
$in = "stream\r"
. "\n"
. "\rendstream\r" // the inital \r in this line will be the content because of the \r\n | \r | \n logic to match the start of a content stream.
. "endobj";

$stream = StreamReader::createByString($in);

$stream->setOffset(6);
$this->assertSame("\r", $stream->getByte());

// Length of 0 - extract the stream manually
$dict = PdfDictionary::create(['Length' => PdfNumeric::create(0), 'Filter' => PdfName::create('FlateDecode')]);

$result = PdfStream::parse($dict, $stream);

$this->assertSame($dict, $result->value);
$this->assertSame('', $result->getStream());
}

public function testParseWithEmptyStream()
{
$streamContent = "";
Expand Down

0 comments on commit 50c3888

Please sign in to comment.