This commit is contained in:
steven 2025-08-11 22:23:30 +02:00
commit 72a26edcff
22092 changed files with 2101903 additions and 0 deletions

View file

@ -0,0 +1,184 @@
<?php
/**
* @file
* This file is part of the PdfParser library.
*
* @author Sébastien MALOT <sebastien@malot.fr>
* @date 2017-01-03
* @license LGPLv3
* @url <https://github.com/smalot/pdfparser>
*
* PdfParser is a pdf library written in PHP, extraction oriented.
* Copyright (C) 2017 - Sébastien MALOT <sebastien@malot.fr>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program.
* If not, see <http://www.pdfparser.org/sites/default/LICENSE.txt>.
*
*/
namespace Smalot\PdfParser\Tests\Units;
use mageekguy\atoum;
/**
* Class Document
*
* @package Smalot\PdfParser\Tests\Units
*/
class Document extends atoum\test
{
public function testSetObjects()
{
$document = new \Smalot\PdfParser\Document();
$object = new \Smalot\PdfParser\Object($document);
// Obj #1 is missing
$this->assert->variable($document->getObjectById(1))->isNull();
$document->setObjects(array(1 => $object));
// Obj #1 exists
$this->assert->object($document->getObjectById(1))->isInstanceOf('\Smalot\PdfParser\Object');
$content = '<</Type/Page>>';
$header = \Smalot\PdfParser\Header::parse($content, $document);
$object = new \Smalot\PdfParser\Object($document, $header);
$document->setObjects(array(2 => $object));
// Obj #1 is missing
$this->assert->assert->variable($document->getObjectById(1))->isNull();
// Obj #2 exists
$this->assert->object($document->getObjectById(2))->isInstanceOf('\Smalot\PdfParser\Object');
}
public function testGetObjects()
{
$document = new \Smalot\PdfParser\Document();
$object1 = new \Smalot\PdfParser\Object($document);
$content = '<</Type/Page>>unparsed content';
$header = \Smalot\PdfParser\Header::parse($content, $document);
$object2 = new \Smalot\PdfParser\Page($document, $header);
$document->setObjects(array(1 => $object1, 2 => $object2));
$this->assert->integer(count($objects = $document->getObjects()))->isEqualTo(2);
$this->assert->object($objects[1])->isInstanceOf('\Smalot\PdfParser\Object');
$this->assert->object($objects[2])->isInstanceOf('\Smalot\PdfParser\Object');
$this->assert->object($objects[2])->isInstanceOf('\Smalot\PdfParser\Page');
}
public function testDictionary()
{
$document = new \Smalot\PdfParser\Document();
$this->assert->integer(count($objects = $document->getDictionary()))->isEqualTo(0);
$object1 = new \Smalot\PdfParser\Object($document);
$content = '<</Type/Page>>';
$header = \Smalot\PdfParser\Header::parse($content, $document);
$object2 = new \Smalot\PdfParser\Page($document, $header);
$document->setObjects(array(1 => $object1, 2 => $object2));
$this->assert->integer(count($objects = $document->getDictionary()))->isEqualTo(1);
$this->assert->integer(count($objects['Page']))->isEqualTo(1);
$this->assert->integer($objects['Page'][2])->isEqualTo(2);
}
public function testGetObjectsByType()
{
$document = new \Smalot\PdfParser\Document();
$object1 = new \Smalot\PdfParser\Object($document);
$content = '<</Type/Page>>';
$header = \Smalot\PdfParser\Header::parse($content, $document);
$object2 = new \Smalot\PdfParser\Page($document, $header);
$document->setObjects(array(1 => $object1, 2 => $object2));
$this->assert->integer(count($objects = $document->getObjectsByType('Page')))->isEqualTo(1);
$this->assert->object($objects[2])->isInstanceOf('\Smalot\PdfParser\Object');
$this->assert->object($objects[2])->isInstanceOf('\Smalot\PdfParser\Page');
}
public function testGetPages()
{
// Missing catalog
$document = new \Smalot\PdfParser\Document();
try {
$pages = $document->getPages();
$this->assert->boolean($pages)->isEqualTo(false);
} catch (\Exception $e) {
$this->assert->object($e)->isInstanceOf('\Exception');
}
// Listing pages from type Page
$content = '<</Type/Page>>';
$header = \Smalot\PdfParser\Header::parse($content, $document);
$object1 = new \Smalot\PdfParser\Page($document, $header);
$header = \Smalot\PdfParser\Header::parse($content, $document);
$object2 = new \Smalot\PdfParser\Page($document, $header);
$document->setObjects(array(1 => $object1, 2 => $object2));
$pages = $document->getPages();
$this->assert->integer(count($pages))->isEqualTo(2);
$this->assert->object($pages[0])->isInstanceOf('\Smalot\PdfParser\Page');
$this->assert->object($pages[1])->isInstanceOf('\Smalot\PdfParser\Page');
// Listing pages from type Pages (kids)
$content = '<</Type/Page>>';
$header = \Smalot\PdfParser\Header::parse($content, $document);
$object1 = new \Smalot\PdfParser\Page($document, $header);
$header = \Smalot\PdfParser\Header::parse($content, $document);
$object2 = new \Smalot\PdfParser\Page($document, $header);
$header = \Smalot\PdfParser\Header::parse($content, $document);
$object3 = new \Smalot\PdfParser\Page($document, $header);
$content = '<</Type/Pages/Kids[1 0 R 2 0 R]>>';
$header = \Smalot\PdfParser\Header::parse($content, $document);
$object4 = new \Smalot\PdfParser\Pages($document, $header);
$content = '<</Type/Pages/Kids[3 0 R]>>';
$header = \Smalot\PdfParser\Header::parse($content, $document);
$object5 = new \Smalot\PdfParser\Pages($document, $header);
$document->setObjects(
array('1_0' => $object1, '2_0' => $object2, '3_0' => $object3, '4_0' => $object4, '5_0' => $object5)
);
$pages = $document->getPages();
$this->assert->integer(count($pages))->isEqualTo(3);
$this->assert->object($pages[0])->isInstanceOf('\Smalot\PdfParser\Page');
$this->assert->object($pages[1])->isInstanceOf('\Smalot\PdfParser\Page');
$this->assert->object($pages[2])->isInstanceOf('\Smalot\PdfParser\Page');
// Listing pages from type Catalog
$content = '<</Type/Page>>';
$header = \Smalot\PdfParser\Header::parse($content, $document);
$object1 = new \Smalot\PdfParser\Page($document, $header);
$header = \Smalot\PdfParser\Header::parse($content, $document);
$object2 = new \Smalot\PdfParser\Page($document, $header);
$header = \Smalot\PdfParser\Header::parse($content, $document);
$object3 = new \Smalot\PdfParser\Page($document, $header);
$content = '<</Type/Pages/Kids[1 0 R 2 0 R]>>';
$header = \Smalot\PdfParser\Header::parse($content, $document);
$object4 = new \Smalot\PdfParser\Pages($document, $header);
$content = '<</Type/Pages/Kids[4 0 R 3 0 R]>>';
$header = \Smalot\PdfParser\Header::parse($content, $document);
$object5 = new \Smalot\PdfParser\Pages($document, $header);
$content = '<</Type/Catalog/Pages 5 0 R >>';
$header = \Smalot\PdfParser\Header::parse($content, $document);
$object6 = new \Smalot\PdfParser\Pages($document, $header);
$document->setObjects(
array(
'1_0' => $object1,
'2_0' => $object2,
'3_0' => $object3,
'4_0' => $object4,
'5_0' => $object5,
'6_0' => $object6
)
);
$pages = $document->getPages();
$this->assert->integer(count($pages))->isEqualTo(3);
$this->assert->object($pages[0])->isInstanceOf('\Smalot\PdfParser\Page');
$this->assert->object($pages[1])->isInstanceOf('\Smalot\PdfParser\Page');
$this->assert->object($pages[2])->isInstanceOf('\Smalot\PdfParser\Page');
}
}

View file

@ -0,0 +1,154 @@
<?php
/**
* @file
* This file is part of the PdfParser library.
*
* @author Sébastien MALOT <sebastien@malot.fr>
* @date 2017-01-03
* @license LGPLv3
* @url <https://github.com/smalot/pdfparser>
*
* PdfParser is a pdf library written in PHP, extraction oriented.
* Copyright (C) 2017 - Sébastien MALOT <sebastien@malot.fr>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program.
* If not, see <http://www.pdfparser.org/sites/default/LICENSE.txt>.
*
*/
namespace Smalot\PdfParser\Tests\Units;
use mageekguy\atoum;
/**
* Class Element
*
* @package Smalot\PdfParser\Tests\Units
*/
class Element extends atoum\test
{
public function testParse()
{
$document = new \Smalot\PdfParser\Document(array());
// Only_values = false.
$content = '/NameType /FlateDecode
/Contents[4 0 R 42]/Fonts<</F1 41/F2 43>>/NullType
null/StringType(hello)/DateType(D:20130901235555+02\'00\')/XRefType 2 0 R
/NumericType 8/HexaType<0020>/BooleanType false';
$offset = 0;
$elements = \Smalot\PdfParser\Element::parse($content, $document, $offset, false);
$this->assert->array($elements)->hasKey('NameType');
$this->assert->object($elements['NameType'])->isInstanceOf('\Smalot\PdfParser\Element\ElementName');
$this->assert->string($elements['NameType']->getContent())->isEqualTo('FlateDecode');
$this->assert->boolean(array_key_exists('Contents', $elements))->isEqualTo(true);
$this->assert->object($elements['Contents'])->isInstanceOf('\Smalot\PdfParser\Element\ElementArray');
$this->assert->boolean($elements['Contents']->contains(42))->isEqualTo(true);
$this->assert->boolean(array_key_exists('Fonts', $elements))->isEqualTo(true);
$this->assert->object($elements['Fonts'])->isInstanceOf('\Smalot\PdfParser\Header');
$this->assert->boolean(array_key_exists('NullType', $elements))->isEqualTo(true);
$this->assert->object($elements['NullType'])->isInstanceOf('\Smalot\PdfParser\Element\ElementNull');
$this->assert->castToString($elements['NullType'])->isEqualTo('null');
$this->assert->boolean(array_key_exists('StringType', $elements))->isEqualTo(true);
$this->assert->object($elements['StringType'])->isInstanceOf('\Smalot\PdfParser\Element\ElementString');
$this->assert->string($elements['StringType']->getContent())->isEqualTo('hello');
$this->assert->boolean(array_key_exists('DateType', $elements))->isEqualTo(true);
$this->assert->object($elements['DateType'])->isInstanceOf('\Smalot\PdfParser\Element\ElementDate');
// $this->assert->castToString($elements['DateType'])->isEqualTo('2013-09-01T23:55:55+02:00');
$this->assert->boolean(array_key_exists('XRefType', $elements))->isEqualTo(true);
$this->assert->object($elements['XRefType'])->isInstanceOf('\Smalot\PdfParser\Element\ElementXRef');
$this->assert->string($elements['XRefType']->getId())->isEqualTo('2_0');
$this->assert->boolean(array_key_exists('NumericType', $elements))->isEqualTo(true);
$this->assert->object($elements['NumericType'])->isInstanceOf('\Smalot\PdfParser\Element\ElementNumeric');
$this->assert->castToString($elements['NumericType'])->isEqualTo('8');
$this->assert->boolean(array_key_exists('HexaType', $elements))->isEqualTo(true);
$this->assert->object($elements['HexaType'])->isInstanceOf('\Smalot\PdfParser\Element\ElementString');
$this->assert->string($elements['HexaType']->getContent())->isEqualTo(' ');
$this->assert->boolean(array_key_exists('BooleanType', $elements))->isEqualTo(true);
$this->assert->object($elements['BooleanType'])->isInstanceOf('\Smalot\PdfParser\Element\ElementBoolean');
$this->assert->boolean($elements['BooleanType']->getContent())->isEqualTo(false);
// Only_values = true.
$content = '/NameType /FlateDecode';
$offset = 0;
$elements = \Smalot\PdfParser\Element::parse($content, $document, $offset, true);
$this->assert->array($elements)->hasSize(2);
$this->assert->integer($offset)->isEqualTo(22);
// Test error.
$content = '/NameType /FlateDecode $$$';
$offset = 0;
$elements = \Smalot\PdfParser\Element::parse($content, $document, $offset, false);
$this->assert->array($elements)->hasSize(1);
$this->assert->integer($offset)->isEqualTo(22);
$this->assert->string(key($elements))->isEqualTo('NameType');
$this->assert->object(current($elements))->isInstanceOf('\Smalot\PdfParser\Element\ElementName');
$content = '/NameType $$$';
$offset = 0;
$elements = \Smalot\PdfParser\Element::parse($content, $document, $offset, false);
$this->assert->integer($offset)->isEqualTo(0);
$this->assert->array($elements)->isEmpty();
/*$this->assert->boolean(array_key_exists('NameType', $elements))->isEqualTo(true);
$this->assert->boolean($elements['NameType'])->isInstanceOf('\Smalot\PdfParser\Element\ElementName)->isEqualTo(true);
$this->assert->string($elements['NameType']->getContent())->isEqualTo('FlateDecode');*/
}
public function testGetContent()
{
$element = new \Smalot\PdfParser\Element(42);
$content = $element->getContent();
$this->assert->integer($content)->isEqualTo(42);
$element = new \Smalot\PdfParser\Element(array(4, 2));
$content = $element->getContent();
$this->assert->array($content)->hasSize(2);
}
public function testEquals()
{
$element = new \Smalot\PdfParser\Element(2);
$this->assert->boolean($element->equals(2))->isEqualTo(true);
$this->assert->boolean($element->equals(8))->isEqualTo(false);
}
public function testContains()
{
$val_4 = new \Smalot\PdfParser\Element(4);
$val_2 = new \Smalot\PdfParser\Element(2);
$element = new \Smalot\PdfParser\Element(array($val_4, $val_2));
$this->assert->boolean($element->contains(2))->isEqualTo(true);
$this->assert->boolean($element->contains(8))->isEqualTo(false);
}
public function test__toString()
{
$element = new \Smalot\PdfParser\Element(2);
$this->assert->castToString($element)->isEqualTo('2');
}
}

View file

@ -0,0 +1,189 @@
<?php
/**
* @file
* This file is part of the PdfParser library.
*
* @author Sébastien MALOT <sebastien@malot.fr>
* @date 2017-01-03
* @license LGPLv3
* @url <https://github.com/smalot/pdfparser>
*
* PdfParser is a pdf library written in PHP, extraction oriented.
* Copyright (C) 2017 - Sébastien MALOT <sebastien@malot.fr>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program.
* If not, see <http://www.pdfparser.org/sites/default/LICENSE.txt>.
*
*/
namespace Smalot\PdfParser\Tests\Units\Element;
use mageekguy\atoum;
use Smalot\PdfParser\Document;
use Smalot\PdfParser\Header;
use Smalot\PdfParser\Page;
/**
* Class ElementArray
*
* @package Smalot\PdfParser\Tests\Units\Element
*/
class ElementArray extends atoum\test
{
public function testParse()
{
$document = new \Smalot\PdfParser\Document(array());
// Skipped.
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementArray::parse('ABC', $document, $offset);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementArray::parse(' / [ 4 2 ] ', $document, $offset);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementArray::parse(' 0 [ 4 2 ] ', $document, $offset);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementArray::parse(" 0 \n [ 4 2 ] ", $document, $offset);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
// Valid.
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementArray::parse(' [ 4 2 ] ', $document, $offset);
$this->assert->boolean($element->contains(4))->isEqualTo(true);
$this->assert->boolean($element->contains(2))->isEqualTo(true);
$this->assert->boolean($element->contains(8))->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(8);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementArray::parse(' [ 4 2 ]', $document, $offset);
$this->assert->boolean($element->contains(4))->isEqualTo(true);
$this->assert->boolean($element->contains(2))->isEqualTo(true);
$this->assert->boolean($element->contains(8))->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(8);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementArray::parse('[ 4 2 ]', $document, $offset);
$this->assert->boolean($element->contains(4))->isEqualTo(true);
$this->assert->boolean($element->contains(2))->isEqualTo(true);
$this->assert->boolean($element->contains(8))->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(7);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementArray::parse(" \n [ 4 2 ] ", $document, $offset);
$this->assert->boolean($element->contains(4))->isEqualTo(true);
$this->assert->boolean($element->contains(2))->isEqualTo(true);
$this->assert->boolean($element->contains(8))->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(10);
}
public function testGetContent()
{
$val_4 = new \Smalot\PdfParser\Element\ElementNumeric('4');
$val_2 = new \Smalot\PdfParser\Element\ElementNumeric('2');
$element = new \Smalot\PdfParser\Element\ElementArray(array($val_4, $val_2));
$content = $element->getContent();
$this->assert->array($content)->hasSize(2);
}
public function testContains()
{
$val_4 = new \Smalot\PdfParser\Element\ElementNumeric('4');
$val_2 = new \Smalot\PdfParser\Element\ElementNumeric('2');
$element = new \Smalot\PdfParser\Element\ElementArray(array($val_4, $val_2));
$this->assert->boolean($element->contains(2))->isEqualTo(true);
$this->assert->boolean($element->contains(8))->isEqualTo(false);
}
public function testResolveXRef()
{
// Document with text.
$filename = __DIR__ . '/../../../../../../samples/Document1_pdfcreator_nocompressed.pdf';
$parser = new \Smalot\PdfParser\Parser();
$document = $parser->parseFile($filename);
$object = $document->getObjectById('3_0');
$kids = $object->get('Kids');
$this->assert->object($kids)->isInstanceOf('\Smalot\PdfParser\Element\ElementArray');
$this->assert->array($kids->getContent())->hasSize(1);
$pages = $kids->getContent();
$this->assert->object(reset($pages))->isInstanceOf('\Smalot\PdfParser\Page');
}
public function testGetDetails()
{
// // Document with text.
// $filename = __DIR__ . '/../../../../../../samples/Document1_pdfcreator_nocompressed.pdf';
// $parser = new \Smalot\PdfParser\Parser();
// $document = $parser->parseFile($filename);
// $object = $document->getObjectById('3_0');
// /** @var \Smalot\PdfParser\Element\ElementArray $kids */
// $kids = $object->get('Kids');
// $details = $kids->getDetails();
//
// $this->assert->array($details)->hasSize(1);
// $this->assert->string($details[0]['Type'])->isEqualTo('Page');
$document = new Document();
$content = '<</Type/Page/Types[8]/Sizes[1 2 3 4 5 <</Subtype/XObject>> [8 [9 <</FontSize 10>>]]]>>';
$details_reference = array(
'Type' => 'Page',
'Types' => array(
8,
),
'Sizes' => array(
1,
2,
3,
4,
5,
array(
'Subtype' => 'XObject',
),
array(
8,
array(
9,
array(
'FontSize' => 10,
),
),
),
),
);
$header = Header::parse($content, $document);
$details = $header->getDetails();
$this->assert->array($details)->hasSize(3);
$this->assert->array($details)->isEqualTo($details_reference);
}
public function test__toString()
{
$val_4 = new \Smalot\PdfParser\Element\ElementNumeric('4');
$val_2 = new \Smalot\PdfParser\Element\ElementNumeric('2');
$element = new \Smalot\PdfParser\Element\ElementArray(array($val_4, $val_2));
$this->assert->castToString($element)->isEqualTo('4,2');
$document = new \Smalot\PdfParser\Document(array());
$element = \Smalot\PdfParser\Element\ElementArray::parse(' [ 4 2 ]', $document);
$this->assert->castToString($element)->isEqualTo('4,2');
}
}

View file

@ -0,0 +1,135 @@
<?php
/**
* @file
* This file is part of the PdfParser library.
*
* @author Sébastien MALOT <sebastien@malot.fr>
* @date 2017-01-03
* @license LGPLv3
* @url <https://github.com/smalot/pdfparser>
*
* PdfParser is a pdf library written in PHP, extraction oriented.
* Copyright (C) 2017 - Sébastien MALOT <sebastien@malot.fr>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program.
* If not, see <http://www.pdfparser.org/sites/default/LICENSE.txt>.
*
*/
namespace Smalot\PdfParser\Tests\Units\Element;
use mageekguy\atoum;
/**
* Class ElementBoolean
*
* @package Smalot\PdfParser\Tests\Units\Element
*/
class ElementBoolean extends atoum\test
{
public function testParse()
{
// Skipped.
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementBoolean::parse('ABC', null, $offset);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementBoolean::parse(' [ false ]', null, $offset);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementBoolean::parse(' << true >>', null, $offset);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementBoolean::parse(' / false ', null, $offset);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementBoolean::parse(' 0 true ', null, $offset);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementBoolean::parse(" 0 \n true ", null, $offset);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
// Valid.
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementBoolean::parse(' true ', null, $offset);
$this->assert->boolean($element->getContent())->isEqualTo(true);
$this->assert->integer($offset)->isEqualTo(5);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementBoolean::parse(' TRUE ', null, $offset);
$this->assert->boolean($element->getContent())->isEqualTo(true);
$this->assert->integer($offset)->isEqualTo(5);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementBoolean::parse(' True', null, $offset);
$this->assert->boolean($element->getContent())->isEqualTo(true);
$this->assert->integer($offset)->isEqualTo(5);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementBoolean::parse('true', null, $offset);
$this->assert->boolean($element->getContent())->isEqualTo(true);
$this->assert->integer($offset)->isEqualTo(4);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementBoolean::parse('False', null, $offset);
$this->assert->boolean($element->getContent())->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(5);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementBoolean::parse(" \n true ", null, $offset);
$this->assert->boolean($element->getContent())->isEqualTo(true);
$this->assert->integer($offset)->isEqualTo(7);
}
public function testGetContent()
{
$element = new \Smalot\PdfParser\Element\ElementBoolean('true');
$this->assert->boolean($element->getContent())->isEqualTo(true);
$element = new \Smalot\PdfParser\Element\ElementBoolean('false');
$this->assert->boolean($element->getContent())->isEqualTo(false);
}
public function testEquals()
{
$element = new \Smalot\PdfParser\Element\ElementBoolean('true');
$this->assert->boolean($element->equals(true))->isEqualTo(true);
$this->assert->boolean($element->equals(1))->isEqualTo(false);
$this->assert->boolean($element->equals(false))->isEqualTo(false);
$this->assert->boolean($element->equals(null))->isEqualTo(false);
$element = new \Smalot\PdfParser\Element\ElementBoolean('false');
$this->assert->boolean($element->equals(false))->isEqualTo(true);
$this->assert->boolean($element->equals(0))->isEqualTo(false);
$this->assert->boolean($element->equals(true))->isEqualTo(false);
$this->assert->boolean($element->equals(null))->isEqualTo(false);
}
public function testContains()
{
$element = new \Smalot\PdfParser\Element\ElementBoolean('true');
$this->assert->boolean($element->contains(true))->isEqualTo(true);
$this->assert->boolean($element->contains(false))->isEqualTo(false);
$this->assert->boolean($element->contains(1))->isEqualTo(false);
}
public function test__toString()
{
$element = new \Smalot\PdfParser\Element\ElementBoolean('true');
$this->assert->castToString($element)->isEqualTo('true');
$element = new \Smalot\PdfParser\Element\ElementBoolean('false');
$this->assert->castToString($element)->isEqualTo('false');
}
}

View file

@ -0,0 +1,164 @@
<?php
/**
* @file
* This file is part of the PdfParser library.
*
* @author Sébastien MALOT <sebastien@malot.fr>
* @date 2017-01-03
* @license LGPLv3
* @url <https://github.com/smalot/pdfparser>
*
* PdfParser is a pdf library written in PHP, extraction oriented.
* Copyright (C) 2017 - Sébastien MALOT <sebastien@malot.fr>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program.
* If not, see <http://www.pdfparser.org/sites/default/LICENSE.txt>.
*
*/
namespace Smalot\PdfParser\Tests\Units\Element;
use mageekguy\atoum;
/**
* Class ElementDate
*
* @package Smalot\PdfParser\Tests\Units\Element
*/
class ElementDate extends atoum\test
{
public function testParse()
{
// Skipped.
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementDate::parse('ABC', null, $offset);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementDate::parse(' [ (ABC) 5 6 ]', null, $offset);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementDate::parse(' << (invalid) >>', null, $offset);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementDate::parse(' / (FlateDecode) ', null, $offset);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementDate::parse(' 0 (FlateDecode) ', null, $offset);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementDate::parse(" 0 \n (FlateDecode) ", null, $offset);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
// Valid.
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementDate::parse(' (D:20130901235555+02\'00\') ', null, $offset);
$element->setFormat('c');
$this->assert->object($element->getContent())->isInstanceOf('\DateTime');
$this->assert->castToString($element)->isEqualTo('2013-09-01T23:55:55+02:00');
$this->assert->integer($offset)->isEqualTo(26);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementDate::parse(' (D:20130901235555+02\'00\') ', null, $offset);
$element->setFormat('c');
$this->assert->object($element->getContent())->isInstanceOf('\DateTime');
$this->assert->castToString($element)->isEqualTo('2013-09-01T23:55:55+02:00');
$this->assert->integer($offset)->isEqualTo(26);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementDate::parse(' (D:20130901235555+02\'00\')', null, $offset);
$element->setFormat('c');
$this->assert->object($element->getContent())->isInstanceOf('\DateTime');
$this->assert->castToString($element)->isEqualTo('2013-09-01T23:55:55+02:00');
$this->assert->integer($offset)->isEqualTo(26);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementDate::parse('(D:20130901235555+02\'00\')', null, $offset);
$element->setFormat('c');
$this->assert->object($element->getContent())->isInstanceOf('\DateTime');
$this->assert->castToString($element)->isEqualTo('2013-09-01T23:55:55+02:00');
$this->assert->integer($offset)->isEqualTo(25);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementDate::parse(" \n (D:20130901235555+02'00') ", null, $offset);
$element->setFormat('c');
$this->assert->object($element->getContent())->isInstanceOf('\DateTime');
$this->assert->castToString($element)->isEqualTo('2013-09-01T23:55:55+02:00');
$this->assert->integer($offset)->isEqualTo(28);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementDate::parse(" \n (D:20130901235555) ", null, $offset);
$element->setFormat('c');
$this->assert->object($element->getContent())->isInstanceOf('\DateTime');
$this->assert->boolean($element->equals(new \DateTime('2013-09-01T23:55:55')))->isEqualTo(true);
$this->assert->integer($offset)->isEqualTo(21);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementDate::parse("(D:20131206091846Z00'00')", null, $offset);
$element->setFormat('c');
$this->assert->object($element->getContent())->isInstanceOf('\DateTime');
//$this->assert->boolean($element->equals(new \DateTime('2013-09-01T23:55:55')))->isEqualTo(true);
$this->assert->integer($offset)->isEqualTo(25);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementDate::parse(" \n (D:1-23-2014, 19:02:15-03'00') ", null, $offset);
$element->setFormat('c');
$this->assert->object($element->getContent())->isInstanceOf('\DateTime');
$this->assert->castToString($element)->isEqualTo('2014-01-23T19:02:15-03:00');
$this->assert->integer($offset)->isEqualTo(33);
// Format invalid
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementDate::parse(" \n (D:2013+02'00') ", null, $offset);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
}
public function testGetContent()
{
$element = new \Smalot\PdfParser\Element\ElementDate(new \DateTime('2013-09-01 23:55:55+02:00'));
$this->assert->dateTime($element->getContent())->isEqualTo(new \DateTime('2013-09-01 21:55:55+00:00'));
try {
$element = new \Smalot\PdfParser\Element\ElementDate('2013-09-01 23:55:55+02:00');
$this->assert->boolean(false)->isEqualTo(true);
} catch (\Exception $e) {
$this->assert->exception($e)->hasMessage('DateTime required.');
}
}
public function testEquals()
{
$element = new \Smalot\PdfParser\Element\ElementDate(new \DateTime('2013-09-01 23:55:55+02:00'));
$element->setFormat('c');
$this->assert->boolean($element->equals('2013-09-01T23:55:55+02:00'))->isEqualTo(true);
$this->assert->boolean($element->equals('2013-09-01T23:55:55+01:00'))->isEqualTo(false);
$this->assert->boolean($element->equals(new \DateTime('2013-09-01T21:55:55+00:00')))->isEqualTo(true);
$this->assert->boolean($element->equals(new \DateTime('2013-09-01T23:55:55+01:00')))->isEqualTo(false);
$this->assert->boolean($element->equals('ABC'))->isEqualTo(false);
}
public function testContains()
{
$element = new \Smalot\PdfParser\Element\ElementDate(new \DateTime('2013-09-01 23:55:55+02:00'));
$this->assert->boolean($element->contains('2013-09-01T21:55:55+00:00'))->isEqualTo(true);
$this->assert->boolean($element->contains('2013-06-15'))->isEqualTo(false);
}
public function test__toString()
{
$element = new \Smalot\PdfParser\Element\ElementDate(new \DateTime('2013-09-01 23:55:55+02:00'));
$element->setFormat('c');
$this->assert->castToString($element)->isEqualTo('2013-09-01T23:55:55+02:00');
}
}

View file

@ -0,0 +1,106 @@
<?php
/**
* @file
* This file is part of the PdfParser library.
*
* @author Sébastien MALOT <sebastien@malot.fr>
* @date 2017-01-03
* @license LGPLv3
* @url <https://github.com/smalot/pdfparser>
*
* PdfParser is a pdf library written in PHP, extraction oriented.
* Copyright (C) 2017 - Sébastien MALOT <sebastien@malot.fr>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program.
* If not, see <http://www.pdfparser.org/sites/default/LICENSE.txt>.
*
*/
namespace Smalot\PdfParser\Tests\Units\Element;
use mageekguy\atoum;
/**
* Class ElementHexa
*
* @package Smalot\PdfParser\Tests\Units\Element
*/
class ElementHexa extends atoum\test
{
public function testParse()
{
// Skipped.
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementHexa::parse('ABC', null, $offset);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementHexa::parse(' [ <0020> 5 6 ]', null, $offset);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementHexa::parse(' << <0020> >>', null, $offset);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementHexa::parse(' / <0020> ', null, $offset);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementHexa::parse(' 0 <0020> ', null, $offset);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementHexa::parse(" 0 \n <0020> ", null, $offset);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
// Valid.
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementHexa::parse(' <0020> ', null, $offset);
$this->assert->string($element->getContent())->isEqualTo(' ');
$this->assert->integer($offset)->isEqualTo(7);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementHexa::parse(' <0020> ', null, $offset);
$this->assert->string($element->getContent())->isEqualTo(' ');
$this->assert->integer($offset)->isEqualTo(7);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementHexa::parse(' <0020>', null, $offset);
$this->assert->string($element->getContent())->isEqualTo(' ');
$this->assert->integer($offset)->isEqualTo(7);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementHexa::parse('<0020>', null, $offset);
$this->assert->string($element->getContent())->isEqualTo(' ');
$this->assert->integer($offset)->isEqualTo(6);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementHexa::parse(" \n <0020> ", null, $offset);
$this->assert->string($element->getContent())->isEqualTo(' ');
$this->assert->integer($offset)->isEqualTo(9);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementHexa::parse(" \n <5465616d204d616e6167656d656e742053797374656d73> ", null, $offset);
$this->assert->string($element->getContent())->isEqualTo('Team Management Systems');
$this->assert->integer($offset)->isEqualTo(51);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementHexa::parse(" \n <5265706f72744275696c646572> ", null, $offset);
$this->assert->object($element)->isInstanceOf('\Smalot\PdfParser\Element\ElementString');
$this->assert->string($element->getContent())->isEqualTo('ReportBuilder');
$this->assert->integer($offset)->isEqualTo(31);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementHexa::parse(" \n <443a3230313331323137313334303435303027303027> ", null, $offset);
$this->assert->object($element)->isInstanceOf('\Smalot\PdfParser\Element\ElementDate');
$this->assert->castToString($element)->isEqualTo('2013-12-17T13:40:45+00:00');
$this->assert->integer($offset)->isEqualTo(49);
}
}

View file

@ -0,0 +1,71 @@
<?php
/**
* @file
* This file is part of the PdfParser library.
*
* @author Sébastien MALOT <sebastien@malot.fr>
* @date 2017-01-03
* @license LGPLv3
* @url <https://github.com/smalot/pdfparser>
*
* PdfParser is a pdf library written in PHP, extraction oriented.
* Copyright (C) 2017 - Sébastien MALOT <sebastien@malot.fr>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program.
* If not, see <http://www.pdfparser.org/sites/default/LICENSE.txt>.
*
*/
namespace Smalot\PdfParser\Tests\Units\Element;
use mageekguy\atoum;
/**
* Class ElementMissing
*
* @package Smalot\PdfParser\Tests\Units\Element
*/
class ElementMissing extends atoum\test
{
public function testEquals()
{
$element = new \Smalot\PdfParser\Element\ElementMissing(null);
$this->assert->boolean($element->equals(null))->isEqualTo(false);
$this->assert->boolean($element->equals(true))->isEqualTo(false);
$this->assert->boolean($element->equals('A'))->isEqualTo(false);
$this->assert->boolean($element->equals(false))->isEqualTo(false);
}
public function testGetContent()
{
$element = new \Smalot\PdfParser\Element\ElementMissing(null);
$this->assert->boolean($element->getContent())->isEqualTo(false);
}
public function testContains()
{
$element = new \Smalot\PdfParser\Element\ElementMissing(null);
$this->assert->boolean($element->contains(null))->isEqualTo(false);
$this->assert->boolean($element->contains(true))->isEqualTo(false);
$this->assert->boolean($element->contains('A'))->isEqualTo(false);
$this->assert->boolean($element->contains(false))->isEqualTo(false);
}
public function test__toString()
{
$element = new \Smalot\PdfParser\Element\ElementMissing(null);
$this->assert->castToString($element)->isEqualTo('');
}
}

View file

@ -0,0 +1,157 @@
<?php
/**
* @file
* This file is part of the PdfParser library.
*
* @author Sébastien MALOT <sebastien@malot.fr>
* @date 2017-01-03
* @license LGPLv3
* @url <https://github.com/smalot/pdfparser>
*
* PdfParser is a pdf library written in PHP, extraction oriented.
* Copyright (C) 2017 - Sébastien MALOT <sebastien@malot.fr>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program.
* If not, see <http://www.pdfparser.org/sites/default/LICENSE.txt>.
*
*/
namespace Smalot\PdfParser\Tests\Units\Element;
use mageekguy\atoum;
/**
* Class ElementName
*
* @package Smalot\PdfParser\Tests\Units\Element
*/
class ElementName extends atoum\test
{
public function testParse()
{
// Skipped.
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementName::parse('ABC', null, $offset);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementName::parse(' [ /ABC 5 6 ]', null, $offset);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementName::parse(' << invalid >>', null, $offset);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementName::parse(' / FlateDecode ', null, $offset);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementName::parse(' 0 /FlateDecode ', null, $offset);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementName::parse(" 0 \n /FlateDecode ", null, $offset);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
// Valid.
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementName::parse(' /FlateDecode ', null, $offset);
$this->assert->string($element->getContent())->isEqualTo('FlateDecode');
$this->assert->integer($offset)->isEqualTo(13);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementName::parse(' /FlateDecode', null, $offset);
$this->assert->string($element->getContent())->isEqualTo('FlateDecode');
$this->assert->integer($offset)->isEqualTo(13);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementName::parse('/FlateDecode', null, $offset);
$this->assert->string($element->getContent())->isEqualTo('FlateDecode');
$this->assert->integer($offset)->isEqualTo(12);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementName::parse(" \n /FlateDecode ", null, $offset);
$this->assert->string($element->getContent())->isEqualTo('FlateDecode');
$this->assert->integer($offset)->isEqualTo(15);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementName::parse('/FlateDecode2', null, $offset);
$this->assert->string($element->getContent())->isEqualTo('FlateDecode2');
$this->assert->integer($offset)->isEqualTo(13);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementName::parse('/Flate-Decode2', null, $offset);
$this->assert->string($element->getContent())->isEqualTo('Flate-Decode2');
$this->assert->integer($offset)->isEqualTo(14);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementName::parse('/OJHCYD+Cambria', null, $offset);
$this->assert->string($element->getContent())->isEqualTo('OJHCYD+Cambria');
$this->assert->integer($offset)->isEqualTo(15);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementName::parse('/OJHCYD+Cambria,Bold', null, $offset);
$this->assert->string($element->getContent())->isEqualTo('OJHCYD+Cambria,Bold');
$this->assert->integer($offset)->isEqualTo(20);
//
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementName::parse('/Flate_Decode2', null, $offset);
$this->assert->string($element->getContent())->isEqualTo('Flate');
$this->assert->integer($offset)->isEqualTo(6);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementName::parse('/Flate.Decode2', null, $offset);
$this->assert->string($element->getContent())->isEqualTo('Flate.Decode2');
$this->assert->integer($offset)->isEqualTo(14);
}
public function testGetContent()
{
$element = new \Smalot\PdfParser\Element\ElementName('FlateDecode');
$this->assert->string($element->getContent())->isEqualTo('FlateDecode');
}
public function testEquals()
{
$element = new \Smalot\PdfParser\Element\ElementName('FlateDecode');
$this->assert->boolean($element->equals('FlateDecode'))->isEqualTo(true);
$this->assert->boolean($element->equals('Flatedecode'))->isEqualTo(false);
$element = new \Smalot\PdfParser\Element\ElementName('FlateDecode2');
$this->assert->boolean($element->equals('FlateDecode2'))->isEqualTo(true);
$this->assert->boolean($element->equals('FlateDecode3'))->isEqualTo(false);
$element = new \Smalot\PdfParser\Element\ElementName('Flate-Decode2');
$this->assert->boolean($element->equals('Flate-Decode2'))->isEqualTo(true);
$this->assert->boolean($element->equals('Flate-Decode3'))->isEqualTo(false);
}
public function testContains()
{
$element = new \Smalot\PdfParser\Element\ElementName('FlateDecode');
$this->assert->boolean($element->contains('FlateDecode'))->isEqualTo(true);
$this->assert->boolean($element->contains('Flatedecode'))->isEqualTo(false);
$element = new \Smalot\PdfParser\Element\ElementName('FlateDecode2');
$this->assert->boolean($element->contains('FlateDecode2'))->isEqualTo(true);
$this->assert->boolean($element->contains('FlateDecode3'))->isEqualTo(false);
$element = new \Smalot\PdfParser\Element\ElementName('Flate-Decode2');
$this->assert->boolean($element->contains('Flate-Decode2'))->isEqualTo(true);
$this->assert->boolean($element->contains('Flate-Decode3'))->isEqualTo(false);
}
public function test__toString()
{
$element = new \Smalot\PdfParser\Element\ElementName('FlateDecode');
$this->assert->castToString($element)->isEqualTo('FlateDecode');
}
}

View file

@ -0,0 +1,121 @@
<?php
/**
* @file
* This file is part of the PdfParser library.
*
* @author Sébastien MALOT <sebastien@malot.fr>
* @date 2017-01-03
* @license LGPLv3
* @url <https://github.com/smalot/pdfparser>
*
* PdfParser is a pdf library written in PHP, extraction oriented.
* Copyright (C) 2017 - Sébastien MALOT <sebastien@malot.fr>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program.
* If not, see <http://www.pdfparser.org/sites/default/LICENSE.txt>.
*
*/
namespace Smalot\PdfParser\Tests\Units\Element;
use mageekguy\atoum;
/**
* Class ElementNull
*
* @package Smalot\PdfParser\Tests\Units\Element
*/
class ElementNull extends atoum\test
{
public function testParse()
{
// Skipped.
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementNull::parse('ABC', null, $offset);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementNull::parse(' [ null ]', null, $offset);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementNull::parse(' << null >>', null, $offset);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementNull::parse(' / null ', null, $offset);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementNull::parse(' 0 null ', null, $offset);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementNull::parse(" 0 \n null ", null, $offset);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
// Valid.
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementNull::parse(' null ', null, $offset);
$this->assert->boolean(is_null($element->getContent()))->isEqualTo(true);
$this->assert->integer($offset)->isEqualTo(5);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementNull::parse(' null ', null, $offset);
$this->assert->boolean(is_null($element->getContent()))->isEqualTo(true);
$this->assert->integer($offset)->isEqualTo(5);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementNull::parse(' null', null, $offset);
$this->assert->boolean(is_null($element->getContent()))->isEqualTo(true);
$this->assert->integer($offset)->isEqualTo(5);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementNull::parse('null', null, $offset);
$this->assert->boolean(is_null($element->getContent()))->isEqualTo(true);
$this->assert->integer($offset)->isEqualTo(4);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementNull::parse(" \n null ", null, $offset);
$this->assert->boolean(is_null($element->getContent()))->isEqualTo(true);
$this->assert->integer($offset)->isEqualTo(7);
}
public function testGetContent()
{
$element = new \Smalot\PdfParser\Element\ElementNull('null');
$this->assert->boolean(is_null($element->getContent()))->isEqualTo(true);
}
public function testEquals()
{
$element = new \Smalot\PdfParser\Element\ElementNull('null');
$this->assert->boolean($element->equals(null))->isEqualTo(true);
$this->assert->boolean($element->equals(false))->isEqualTo(false);
$this->assert->boolean($element->equals(0))->isEqualTo(false);
$this->assert->boolean($element->equals(1))->isEqualTo(false);
}
public function testContains()
{
$element = new \Smalot\PdfParser\Element\ElementNull('null');
$this->assert->boolean($element->contains(null))->isEqualTo(true);
$this->assert->boolean($element->contains(false))->isEqualTo(false);
$this->assert->boolean($element->contains(0))->isEqualTo(false);
}
public function test__toString()
{
$element = new \Smalot\PdfParser\Element\ElementNull('null');
$this->assert->castToString($element)->isEqualTo('null');
}
}

View file

@ -0,0 +1,184 @@
<?php
/**
* @file
* This file is part of the PdfParser library.
*
* @author Sébastien MALOT <sebastien@malot.fr>
* @date 2017-01-03
* @license LGPLv3
* @url <https://github.com/smalot/pdfparser>
*
* PdfParser is a pdf library written in PHP, extraction oriented.
* Copyright (C) 2017 - Sébastien MALOT <sebastien@malot.fr>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program.
* If not, see <http://www.pdfparser.org/sites/default/LICENSE.txt>.
*
*/
namespace Smalot\PdfParser\Tests\Units\Element;
use mageekguy\atoum;
/**
* Class ElementNumeric
*
* @package Smalot\PdfParser\Tests\Units\Element
*/
class ElementNumeric extends atoum\test
{
public function testParse()
{
// Skipped.
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementNumeric::parse('ABC', null, $offset);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementNumeric::parse(' [ 2 ]', null, $offset);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementNumeric::parse(' /2', null, $offset);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementNumeric::parse(" /2 \n 2", null, $offset);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
// Valid.
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementNumeric::parse(' -2', null, $offset);
$this->assert->float($element->getContent())->isEqualTo(-2.0);
$this->assert->integer($offset)->isEqualTo(3);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementNumeric::parse('2BC', null, $offset);
$this->assert->float($element->getContent())->isEqualTo(2.0);
$this->assert->integer($offset)->isEqualTo(1);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementNumeric::parse(' 2BC', null, $offset);
$this->assert->float($element->getContent())->isEqualTo(2.0);
$this->assert->integer($offset)->isEqualTo(2);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementNumeric::parse(' -2BC', null, $offset);
$this->assert->float($element->getContent())->isEqualTo(-2.0);
$this->assert->integer($offset)->isEqualTo(3);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementNumeric::parse(' -2', null, $offset);
$this->assert->float($element->getContent())->isEqualTo(-2.0);
$this->assert->integer($offset)->isEqualTo(3);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementNumeric::parse(' 2 0 obj', null, $offset);
$this->assert->float($element->getContent())->isEqualTo(2.0);
$this->assert->integer($offset)->isEqualTo(2);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementNumeric::parse(" \n -2 ", null, $offset);
$this->assert->float($element->getContent())->isEqualTo(-2.0);
$this->assert->integer($offset)->isEqualTo(5);
}
public function testGetContent()
{
$element = new \Smalot\PdfParser\Element\ElementNumeric('B');
$this->assert->float($element->getContent())->isEqualTo(0.0);
$element = new \Smalot\PdfParser\Element\ElementNumeric('-2.5');
$this->assert->float($element->getContent())->isEqualTo(-2.5);
$element = new \Smalot\PdfParser\Element\ElementNumeric('-2');
$this->assert->float($element->getContent())->isEqualTo(-2.0);
$element = new \Smalot\PdfParser\Element\ElementNumeric(' -2');
$this->assert->float($element->getContent())->isEqualTo(-2.0);
$element = new \Smalot\PdfParser\Element\ElementNumeric('2.5');
$this->assert->float($element->getContent())->isEqualTo(2.5);
$element = new \Smalot\PdfParser\Element\ElementNumeric('2');
$this->assert->float($element->getContent())->isEqualTo(2.0);
}
public function testEquals()
{
$element = new \Smalot\PdfParser\Element\ElementNumeric('1');
$this->assert->boolean($element->equals('B'))->isEqualTo(false);
$element = new \Smalot\PdfParser\Element\ElementNumeric('1.5');
$this->assert->boolean($element->equals('B'))->isEqualTo(false);
$element = new \Smalot\PdfParser\Element\ElementNumeric('2');
$this->assert->boolean($element->equals('2'))->isEqualTo(true);
$element = new \Smalot\PdfParser\Element\ElementNumeric('2');
$this->assert->boolean($element->equals('3'))->isEqualTo(false);
$element = new \Smalot\PdfParser\Element\ElementNumeric('-2');
$this->assert->boolean($element->equals('-2'))->isEqualTo(true);
$element = new \Smalot\PdfParser\Element\ElementNumeric('-2');
$this->assert->boolean($element->equals('-3'))->isEqualTo(false);
$element = new \Smalot\PdfParser\Element\ElementNumeric('2.5');
$this->assert->boolean($element->equals('2.5'))->isEqualTo(true);
$element = new \Smalot\PdfParser\Element\ElementNumeric('2.5');
$this->assert->boolean($element->equals('3.5'))->isEqualTo(false);
$element = new \Smalot\PdfParser\Element\ElementNumeric('-2.5');
$this->assert->boolean($element->equals('-2.5'))->isEqualTo(true);
$element = new \Smalot\PdfParser\Element\ElementNumeric('-2.5');
$this->assert->boolean($element->equals('-3.5'))->isEqualTo(false);
}
public function testContains()
{
$element = new \Smalot\PdfParser\Element\ElementNumeric('1');
$this->assert->boolean($element->contains('B'))->isEqualTo(false);
$element = new \Smalot\PdfParser\Element\ElementNumeric('1.5');
$this->assert->boolean($element->contains('B'))->isEqualTo(false);
$element = new \Smalot\PdfParser\Element\ElementNumeric('2');
$this->assert->boolean($element->contains('2'))->isEqualTo(true);
$element = new \Smalot\PdfParser\Element\ElementNumeric('2');
$this->assert->boolean($element->contains('3'))->isEqualTo(false);
$element = new \Smalot\PdfParser\Element\ElementNumeric('-2');
$this->assert->boolean($element->contains('-2'))->isEqualTo(true);
$element = new \Smalot\PdfParser\Element\ElementNumeric('-2');
$this->assert->boolean($element->contains('-3'))->isEqualTo(false);
$element = new \Smalot\PdfParser\Element\ElementNumeric('2.5');
$this->assert->boolean($element->contains('2.5'))->isEqualTo(true);
$element = new \Smalot\PdfParser\Element\ElementNumeric('2.5');
$this->assert->boolean($element->contains('3.5'))->isEqualTo(false);
$element = new \Smalot\PdfParser\Element\ElementNumeric('-2.5');
$this->assert->boolean($element->contains('-2.5'))->isEqualTo(true);
$element = new \Smalot\PdfParser\Element\ElementNumeric('-2.5');
$this->assert->boolean($element->contains('-3.5'))->isEqualTo(false);
}
public function test__toString()
{
$element = new \Smalot\PdfParser\Element\ElementNumeric('B');
$this->assert->castToString($element)->isEqualTo('0');
$element = new \Smalot\PdfParser\Element\ElementNumeric('1B');
$this->assert->castToString($element)->isEqualTo('1');
$element = new \Smalot\PdfParser\Element\ElementNumeric('2');
$this->assert->castToString($element)->isEqualTo('2');
$element = new \Smalot\PdfParser\Element\ElementNumeric('-2');
$this->assert->castToString($element)->isEqualTo('-2');
$element = new \Smalot\PdfParser\Element\ElementNumeric('2.5');
$this->assert->castToString($element)->isEqualTo('2.5');
$element = new \Smalot\PdfParser\Element\ElementNumeric('-2.5');
$this->assert->castToString($element)->isEqualTo('-2.5');
}
}

View file

@ -0,0 +1,156 @@
<?php
/**
* @file
* This file is part of the PdfParser library.
*
* @author Sébastien MALOT <sebastien@malot.fr>
* @date 2017-01-03
* @license LGPLv3
* @url <https://github.com/smalot/pdfparser>
*
* PdfParser is a pdf library written in PHP, extraction oriented.
* Copyright (C) 2017 - Sébastien MALOT <sebastien@malot.fr>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program.
* If not, see <http://www.pdfparser.org/sites/default/LICENSE.txt>.
*
*/
namespace Smalot\PdfParser\Tests\Units\Element;
use mageekguy\atoum;
/**
* Class ElementString
*
* @package Smalot\PdfParser\Tests\Units\Element
*/
class ElementString extends atoum\test
{
public function testParse()
{
// Skipped.
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementString::parse('ABC', null, $offset);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementString::parse(' [ (ABC) 5 6 ]', null, $offset);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementString::parse(' << (invalid) >>', null, $offset);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementString::parse(' / (FlateDecode) ', null, $offset);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementString::parse(' 0 (FlateDecode) ', null, $offset);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementString::parse(" 0 \n (FlateDecode) ", null, $offset);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
// Valid.
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementString::parse(' (Copyright) ', null, $offset);
$this->assert->string($element->getContent())->isEqualTo('Copyright');
$this->assert->integer($offset)->isEqualTo(12);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementString::parse(' (Copyright) ', null, $offset);
$this->assert->string($element->getContent())->isEqualTo('Copyright');
$this->assert->integer($offset)->isEqualTo(12);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementString::parse(' (Copyright)', null, $offset);
$this->assert->string($element->getContent())->isEqualTo('Copyright');
$this->assert->integer($offset)->isEqualTo(12);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementString::parse('(Copyright)', null, $offset);
$this->assert->string($element->getContent())->isEqualTo('Copyright');
$this->assert->integer($offset)->isEqualTo(11);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementString::parse('(Copy-right2)', null, $offset);
$this->assert->string($element->getContent())->isEqualTo('Copy-right2');
$this->assert->integer($offset)->isEqualTo(13);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementString::parse(" \n (Copyright) ", null, $offset);
$this->assert->string($element->getContent())->isEqualTo('Copyright');
$this->assert->integer($offset)->isEqualTo(14);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementString::parse('()', null, $offset);
$this->assert->string($element->getContent())->isEqualTo('');
$this->assert->integer($offset)->isEqualTo(2);
// Complex study case : Unicode + octal.
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementString::parse("(ABC\\))", null, $offset);
$this->assert->string($element->getContent())->isEqualTo('ABC)');
$this->assert->integer($offset)->isEqualTo(7);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementString::parse("(\xFE\xFF\\000M)", null, $offset);
$this->assert->string($element->getContent())->isEqualTo('M');
$this->assert->integer($offset)->isEqualTo(9);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementString::parse("(<20>)", null, $offset);
$this->assert->string($element->getContent())->isEqualTo(' ');
$this->assert->integer($offset)->isEqualTo(6);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementString::parse("(Gutter\\ console\\ assembly)", null, $offset);
$this->assert->string($element->getContent())->isEqualTo('Gutter console assembly');
$this->assert->integer($offset)->isEqualTo(27);
}
public function testGetContent()
{
$element = new \Smalot\PdfParser\Element\ElementString('Copyright');
$this->assert->string($element->getContent())->isEqualTo('Copyright');
}
public function testEquals()
{
$element = new \Smalot\PdfParser\Element\ElementString('CopyRight');
$this->assert->boolean($element->equals('CopyRight'))->isEqualTo(true);
$this->assert->boolean($element->equals('Flatedecode'))->isEqualTo(false);
$element = new \Smalot\PdfParser\Element\ElementString('CopyRight2');
$this->assert->boolean($element->equals('CopyRight2'))->isEqualTo(true);
$this->assert->boolean($element->equals('CopyRight3'))->isEqualTo(false);
$element = new \Smalot\PdfParser\Element\ElementString('Flate-Decode2');
$this->assert->boolean($element->equals('Flate-Decode2'))->isEqualTo(true);
$this->assert->boolean($element->equals('Flate-Decode3'))->isEqualTo(false);
}
public function testContains()
{
$element = new \Smalot\PdfParser\Element\ElementString('CopyRight');
$this->assert->boolean($element->contains('CopyRight'))->isEqualTo(true);
$this->assert->boolean($element->contains('Copyright'))->isEqualTo(false);
$element = new \Smalot\PdfParser\Element\ElementString('CopyRight2');
$this->assert->boolean($element->contains('CopyRight2'))->isEqualTo(true);
$this->assert->boolean($element->contains('CopyRight3'))->isEqualTo(false);
}
public function test__toString()
{
$element = new \Smalot\PdfParser\Element\ElementString('CopyRight');
$this->assert->castToString($element)->isEqualTo('CopyRight');
}
}

View file

@ -0,0 +1,98 @@
<?php
/**
* @file
* This file is part of the PdfParser library.
*
* @author Sébastien MALOT <sebastien@malot.fr>
* @date 2017-01-03
* @license LGPLv3
* @url <https://github.com/smalot/pdfparser>
*
* PdfParser is a pdf library written in PHP, extraction oriented.
* Copyright (C) 2017 - Sébastien MALOT <sebastien@malot.fr>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program.
* If not, see <http://www.pdfparser.org/sites/default/LICENSE.txt>.
*
*/
namespace Smalot\PdfParser\Tests\Units\Element;
use mageekguy\atoum;
/**
* Class ElementStruct
*
* @package Smalot\PdfParser\Tests\Units\Element
*/
class ElementStruct extends atoum\test
{
public function testParse()
{
$document = new \Smalot\PdfParser\Document(array());
// Skipped.
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementStruct::parse('ABC', $document, $offset);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementStruct::parse(
' [ << /Filter /FlateDecode >> ]',
$document,
$offset
);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementStruct::parse(' / << /Filter /FlateDecode >> ', $document, $offset);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementStruct::parse(' 0 << /Filter /FlateDecode >> ', $document, $offset);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementStruct::parse(
" 0 \n << /Filter /FlateDecode >> ",
$document,
$offset
);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
// Valid.
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementStruct::parse(' << /Filter /FlateDecode >> ', $document, $offset);
$this->assert->object($element)->isInstanceOf('\Smalot\PdfParser\Header');
$this->assert->integer($offset)->isEqualTo(27);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementStruct::parse(' << /Filter /FlateDecode >>', $document, $offset);
$this->assert->object($element)->isInstanceOf('\Smalot\PdfParser\Header');
$this->assert->integer($offset)->isEqualTo(27);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementStruct::parse('<< /Filter /FlateDecode >>', $document, $offset);
$this->assert->object($element)->isInstanceOf('\Smalot\PdfParser\Header');
$this->assert->integer($offset)->isEqualTo(26);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementStruct::parse(
" \n << /Filter /FlateDecode >> ",
$document,
$offset
);
$this->assert->object($element)->isInstanceOf('\Smalot\PdfParser\Header');
$this->assert->integer($offset)->isEqualTo(29);
}
}

View file

@ -0,0 +1,126 @@
<?php
/**
* @file
* This file is part of the PdfParser library.
*
* @author Sébastien MALOT <sebastien@malot.fr>
* @date 2017-01-03
* @license LGPLv3
* @url <https://github.com/smalot/pdfparser>
*
* PdfParser is a pdf library written in PHP, extraction oriented.
* Copyright (C) 2017 - Sébastien MALOT <sebastien@malot.fr>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program.
* If not, see <http://www.pdfparser.org/sites/default/LICENSE.txt>.
*
*/
namespace Smalot\PdfParser\Tests\Units\Element;
use mageekguy\atoum;
/**
* Class ElementXRef
*
* @package Smalot\PdfParser\Tests\Units\Element
*/
class ElementXRef extends atoum\test
{
public function testParse()
{
// Skipped.
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementXRef::parse('ABC', null, $offset);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementXRef::parse(' [ 5 0 R ]', null, $offset);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementXRef::parse(' << 5 0 R >>', null, $offset);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementXRef::parse(' / 5 0 R ', null, $offset);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementXRef::parse(' 0 5 0 R ', null, $offset);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementXRef::parse(" 0 \n 5 0 R ", null, $offset);
$this->assert->boolean($element)->isEqualTo(false);
$this->assert->integer($offset)->isEqualTo(0);
// Valid.
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementXRef::parse(' 5 0 R ', null, $offset);
$this->assert->string($element->getContent())->isEqualTo('5_0');
$this->assert->integer($offset)->isEqualTo(6);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementXRef::parse(' 5 0 R ', null, $offset);
$this->assert->string($element->getContent())->isEqualTo('5_0');
$this->assert->integer($offset)->isEqualTo(6);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementXRef::parse(' 5 0 R', null, $offset);
$this->assert->string($element->getContent())->isEqualTo('5_0');
$this->assert->integer($offset)->isEqualTo(6);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementXRef::parse('5 0 R', null, $offset);
$this->assert->string($element->getContent())->isEqualTo('5_0');
$this->assert->integer($offset)->isEqualTo(5);
$offset = 0;
$element = \Smalot\PdfParser\Element\ElementXRef::parse(" \n 5 0 R ", null, $offset);
$this->assert->string($element->getContent())->isEqualTo('5_0');
$this->assert->integer($offset)->isEqualTo(8);
}
public function testGetContent()
{
$element = new \Smalot\PdfParser\Element\ElementXRef('5_0');
$this->assert->string($element->getContent())->isEqualTo('5_0');
}
public function testGetId()
{
$element = new \Smalot\PdfParser\Element\ElementXRef('5_0');
$this->assert->string($element->getId())->isEqualTo('5_0');
}
public function testEquals()
{
$element = new \Smalot\PdfParser\Element\ElementXRef('5_0');
$this->assert->boolean($element->equals(5))->isEqualTo(true);
$this->assert->boolean($element->equals(8))->isEqualTo(false);
$this->assert->boolean($element->equals($element))->isEqualTo(true);
}
public function testContains()
{
$element = new \Smalot\PdfParser\Element\ElementXRef('5_0');
$this->assert->boolean($element->contains(5))->isEqualTo(true);
$this->assert->boolean($element->contains(8))->isEqualTo(false);
$this->assert->boolean($element->contains($element))->isEqualTo(true);
}
public function test__toString()
{
$element = new \Smalot\PdfParser\Element\ElementXRef('5_0');
$this->assert->castToString($element)->isEqualTo('#Obj#5_0');
}
}

View file

@ -0,0 +1,322 @@
<?php
/**
* @file
* This file is part of the PdfParser library.
*
* @author Sébastien MALOT <sebastien@malot.fr>
* @date 2017-01-03
* @license LGPLv3
* @url <https://github.com/smalot/pdfparser>
*
* PdfParser is a pdf library written in PHP, extraction oriented.
* Copyright (C) 2017 - Sébastien MALOT <sebastien@malot.fr>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program.
* If not, see <http://www.pdfparser.org/sites/default/LICENSE.txt>.
*
*/
namespace Smalot\PdfParser\Tests\Units;
use mageekguy\atoum;
use Smalot\PdfParser\Header;
/**
* Class Font
*
* @package Smalot\PdfParser\Tests\Units
*/
class Font extends atoum\test
{
public function testGetName()
{
$filename = __DIR__ . '/../../../../../samples/Document1_pdfcreator_nocompressed.pdf';
$parser = new \Smalot\PdfParser\Parser();
$document = $parser->parseFile($filename);
$fonts = $document->getFonts();
$font = reset($fonts);
$this->assert->string($font->getName())->isEqualTo('OJHCYD+Cambria,Bold');
}
public function testGetType()
{
$filename = __DIR__ . '/../../../../../samples/Document1_pdfcreator_nocompressed.pdf';
$parser = new \Smalot\PdfParser\Parser();
$document = $parser->parseFile($filename);
$fonts = $document->getFonts();
$font = reset($fonts);
$this->assert->string($font->getType())->isEqualTo('TrueType');
}
public function testGetDetails()
{
$filename = __DIR__ . '/../../../../../samples/Document1_pdfcreator_nocompressed.pdf';
$parser = new \Smalot\PdfParser\Parser();
$document = $parser->parseFile($filename);
$fonts = $document->getFonts();
$font = reset($fonts);
$reference = array(
'Name' => 'OJHCYD+Cambria,Bold',
'Type' => 'TrueType',
'Encoding' => 'Ansi',
'BaseFont' => 'OJHCYD+Cambria,Bold',
'FontDescriptor' =>
array(
'Type' => 'FontDescriptor',
'FontName' => 'OJHCYD+Cambria,Bold',
'Flags' => 4,
'Ascent' => 699,
'CapHeight' => 699,
'Descent' => -7,
'ItalicAngle' => 0,
'StemV' => 128,
'MissingWidth' => 658,
),
'ToUnicode' =>
array(
'Filter' => 'FlateDecode',
'Length' => 219,
),
'FirstChar' => 1,
'LastChar' => 11,
'Widths' =>
array(
0 => 705,
1 => 569,
2 => 469,
3 => 597,
4 => 890,
5 => 531,
6 => 604,
7 => 365,
8 => 220,
9 => 314,
10 => 308,
),
'Subtype' => 'TrueType',
);
$this->assert->array($font->getDetails())->isEqualTo($reference);
}
public function testTranslateChar()
{
$filename = __DIR__ . '/../../../../../samples/Document1_pdfcreator_nocompressed.pdf';
$parser = new \Smalot\PdfParser\Parser();
$document = $parser->parseFile($filename);
$fonts = $document->getFonts();
/** @var \Smalot\PdfParser\Font $font */
$font = reset($fonts);
$this->assert->string($font->translateChar("\x01"))->isEqualTo('D');
$this->assert->string($font->translateChar("\x02"))->isEqualTo('o');
$this->assert->string($font->translateChar("\x03"))->isEqualTo('c');
$this->assert->string($font->translateChar("\x04"))->isEqualTo('u');
$this->assert->string($font->translateChar("\x99"))->isEqualTo(\Smalot\PdfParser\Font::MISSING);
}
public function testLoadTranslateTable()
{
$document = new \Smalot\PdfParser\Document();
$content = '<</Type/Font /Subtype /Type0 /ToUnicode 2 0 R>>';
$header = Header::parse($content, $document);
$font = new \Smalot\PdfParser\Font($document, $header);
$content = '/CIDInit /ProcSet findresource begin
14 dict begin
begincmap
/CIDSystemInfo
<< /Registry (Adobe)
/Ordering (UCS)
/Supplement 0
>> def
/CMapName /Adobe-Identity-UCS def
/CMapType 2 def
1 begincodespacerange
<0000> <FFFF>
endcodespacerange
3 beginbfchar
<0003> <0020>
<000F> <002C>
<0011> <002E>
endbfchar
2 beginbfrange
<0013> <0016> <0030>
<0018> <001C> <0035>
endbfrange
7 beginbfchar
<0023> <0040>
<0026> <0043>
<0028> <0045>
<0030> <004D>
<0033> <0050>
<0035> <0052>
<0039> <0056>
endbfchar
4 beginbfrange
<0044> <004C> <0061>
<004F> <0052> <006C>
<0054> <0059> <0071>
<005B> <005C> <0078>
endbfrange
4 beginbfchar
<0070> <00E9>
<00AB> <2026>
<00B0> <0153>
<00B6> <2019>
endbfchar
1 beginbfrange
<0084> <0086> [<0061> <0071> <0081>]
endbfrange
endcmap
CMapName currentdict /CMap defineresource pop
end
end';
$unicode = new \Smalot\PdfParser\Object($document, null, $content);
$document->setObjects(array('1_0' => $font, '2_0' => $unicode));
$font->init();
// Test reload
$table = $font->loadTranslateTable();
$this->assert->array($table)->hasSize(47);
// Test chars
$this->assert->string($table[3])->isEqualTo(' ');
$this->assert->string($table[15])->isEqualTo(',');
$this->assert->string($table[17])->isEqualTo('.');
$this->assert->string($table[35])->isEqualTo('@');
$this->assert->string($table[57])->isEqualTo('V');
// Test ranges
$this->assert->string($table[85])->isEqualTo('r');
$this->assert->string($table[92])->isEqualTo('y');
}
public function testDecodeHexadecimal()
{
$hexa = '<322041>';
$this->assert->string(\Smalot\PdfParser\Font::decodeHexadecimal($hexa))->isEqualTo("2 A");
$this->assert->string(\Smalot\PdfParser\Font::decodeHexadecimal($hexa, false))->isEqualTo("2 A");
$this->assert->string(\Smalot\PdfParser\Font::decodeHexadecimal($hexa, true))->isEqualTo("(2 A)");
$hexa = '<003200200041>';
$this->assert->string(\Smalot\PdfParser\Font::decodeHexadecimal($hexa))->isEqualTo("\x002\x00 \x00A");
$this->assert->string(\Smalot\PdfParser\Font::decodeHexadecimal($hexa, false))->isEqualTo("\x002\x00 \x00A");
$this->assert->string(\Smalot\PdfParser\Font::decodeHexadecimal($hexa, true))->isEqualTo("(\x002\x00 \x00A)");
$hexa = '<00320020> 8 <0041>';
$this->assert->string(\Smalot\PdfParser\Font::decodeHexadecimal($hexa))->isEqualTo("\x002\x00 8 \x00A");
$this->assert->string(\Smalot\PdfParser\Font::decodeHexadecimal($hexa, false))->isEqualTo("\x002\x00 8 \x00A");
$this->assert->string(\Smalot\PdfParser\Font::decodeHexadecimal($hexa, true))->isEqualTo(
"(\x002\x00 ) 8 (\x00A)"
);
$hexa = '<3220> 8 <41>';
$this->assert->string(\Smalot\PdfParser\Font::decodeHexadecimal($hexa))->isEqualTo("2 8 A");
$this->assert->string(\Smalot\PdfParser\Font::decodeHexadecimal($hexa, false))->isEqualTo("2 8 A");
$this->assert->string(\Smalot\PdfParser\Font::decodeHexadecimal($hexa, true))->isEqualTo("(2 ) 8 (A)");
$hexa = '<00320020005C>-10<0041>';
$this->assert->string(\Smalot\PdfParser\Font::decodeHexadecimal($hexa))->isEqualTo("\x002\x00 \x00\\-10\x00A");
$this->assert->string(\Smalot\PdfParser\Font::decodeHexadecimal($hexa, false))->isEqualTo(
"\x002\x00 \x00\\-10\x00A"
);
$this->assert->string(\Smalot\PdfParser\Font::decodeHexadecimal($hexa, true))->isEqualTo(
"(\x002\x00 \x00\\\\)-10(\x00A)"
);
}
public function testDecodeOctal()
{
$this->assert->string(\Smalot\PdfParser\Font::decodeOctal("\\101\\102\\040\\103"))->isEqualTo('AB C');
$this->assert->string(\Smalot\PdfParser\Font::decodeOctal("\\101\\102\\040\\103D"))->isEqualTo('AB CD');
}
public function testDecodeEntities()
{
$this->assert->string(\Smalot\PdfParser\Font::decodeEntities("File#20Type"))->isEqualTo('File Type');
$this->assert->string(\Smalot\PdfParser\Font::decodeEntities("File##20Ty#pe"))->isEqualTo('File# Ty#pe');
}
public function testDecodeUnicode()
{
$this->assert->string(\Smalot\PdfParser\Font::decodeUnicode("\xFE\xFF\x00A\x00B"))->isEqualTo('AB');
}
public function testDecodeText()
{
$filename = __DIR__ . '/../../../../../samples/Document1_pdfcreator_nocompressed.pdf';
$parser = new \Smalot\PdfParser\Parser();
$document = $parser->parseFile($filename);
$fonts = $document->getFonts();
/** @var \Smalot\PdfParser\Font $font */
// Cambria
$font = reset($fonts);
$commands = array(
array(
't' => '',
'c' => "\x01\x02",
),
array(
't' => 'n',
'c' => -10,
),
array(
't' => '',
'c' => "\x03",
),
array(
't' => '',
'c' => "\x04",
),
array(
't' => 'n',
'c' => -100,
),
array(
't' => '<',
'c' => "01020304",
),
);
$this->assert->string($font->decodeText($commands))->isEqualTo('Docu Docu');
//Check if ANSI/Unicode detection is working properly
$filename = __DIR__ . '/../../../../../samples/bugs/Issue95_ANSI.pdf';
$parser = new \Smalot\PdfParser\Parser();
$document = $parser->parseFile($filename);
$fonts = $document->getFonts();
/** @var \Smalot\PdfParser\Font $font */
$font = reset($fonts);
$commands = array(
array(
't' => '<',
'c' => "E6F6FC", //ANSI encoded string
),
);
$this->assert->string($font->decodeText($commands))->isEqualTo('æöü');
$commands = array(
array(
't' => '<',
'c' => "C3A6C3B6C3BC", //Unicode encoded string
),
);
$this->assert->string($font->decodeText($commands))->isEqualTo('æöü');
}
}

View file

@ -0,0 +1,145 @@
<?php
/**
* @file
* This file is part of the PdfParser library.
*
* @author Sébastien MALOT <sebastien@malot.fr>
* @date 2017-01-03
* @license LGPLv3
* @url <https://github.com/smalot/pdfparser>
*
* PdfParser is a pdf library written in PHP, extraction oriented.
* Copyright (C) 2017 - Sébastien MALOT <sebastien@malot.fr>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program.
* If not, see <http://www.pdfparser.org/sites/default/LICENSE.txt>.
*
*/
namespace Smalot\PdfParser\Tests\Units;
use mageekguy\atoum;
/**
* Class Header
*
* @package Smalot\PdfParser\Tests\Units
*/
class Header extends atoum\test
{
public function testParse()
{
$document = new \Smalot\PdfParser\Document();
$content = '<</Type/Page/SubType/Text>>foo';
$position = 0;
$header = \Smalot\PdfParser\Header::parse($content, $document, $position);
$this->assert->object($header)->isInstanceOf('\Smalot\PdfParser\Header');
$this->assert->integer($position)->isEqualTo(27);
$this->assert->array($header->getElements())->hasSize(2);
// No header to parse
$this->assert->castToString($header->get('Type'))->isEqualTo('Page');
$content = 'foo';
$position = 0;
$header = \Smalot\PdfParser\Header::parse($content, $document, $position);
$this->assert->object($header)->isInstanceOf('\Smalot\PdfParser\Header');
$this->assert->integer($position)->isEqualTo(0);
$this->assert->array($header->getElements())->hasSize(0);
$position = 0;
$content = "<</CreationDate(D:20100309184803+01'00')/Author(Utilisateur)/Creator(PScript5.dll Version 5.2.2)/Producer(Acrobat Distiller 7.0.5 \(Windows\))/ModDate(D:20100310104810+01'00')/Title(Microsoft Word - CLEMI.docx)>>";
$header = \Smalot\PdfParser\Header::parse($content, $document, $position);
$this->assert->integer($position)->isEqualTo(212);
$position = 0;
$content = '[5 0 R ] foo';
$header = \Smalot\PdfParser\Header::parse($content, $document, $position);
$this->assert->integer($position)->isEqualTo(8);
$this->assert->array($header->getElements())->hasSize(1);
}
public function testGetElements()
{
$document = new \Smalot\PdfParser\Document();
$content = '<</Type/Page/Subtype/Text>>foo';
$position = 0;
$header = \Smalot\PdfParser\Header::parse($content, $document, $position);
$this->assert->array($elements = $header->getElements())->hasSize(2);
$this->assert->object(current($elements))->isInstanceOf('\Smalot\PdfParser\Element\ElementName');
$types = $header->getElementTypes();
$this->assert->array($types);
$this->assert->string($types['Type'])->isEqualTo('Smalot\PdfParser\Element\ElementName');
$this->assert->string($types['Subtype'])->isEqualTo('Smalot\PdfParser\Element\ElementName');
}
public function testHas()
{
$document = new \Smalot\PdfParser\Document();
$content = '<</Type/Page/SubType/Text/Font 5 0 R>>foo';
$position = 0;
$header = \Smalot\PdfParser\Header::parse($content, $document, $position);
$this->assert->boolean($header->has('Type'))->isEqualTo(true);
$this->assert->boolean($header->has('SubType'))->isEqualTo(true);
$this->assert->boolean($header->has('Font'))->isEqualTo(true);
$this->assert->boolean($header->has('Text'))->isEqualTo(false);
}
public function testGet()
{
$document = new \Smalot\PdfParser\Document();
$content = '<</Type/Page/SubType/Text/Font 5 0 R/Resources 8 0 R>>foo';
$position = 0;
$header = \Smalot\PdfParser\Header::parse($content, $document, $position);
$object = new \Smalot\PdfParser\Page($document, $header);
$document->setObjects(array('5_0' => $object));
$this->assert->object($header->get('Type'))->isInstanceOf('\Smalot\PdfParser\Element\ElementName');
$this->assert->object($header->get('SubType'))->isInstanceOf('\Smalot\PdfParser\Element\ElementName');
$this->assert->object($header->get('Font'))->isInstanceOf('\Smalot\PdfParser\Page');
$this->assert->object($header->get('Image'))->isInstanceOf('\Smalot\PdfParser\Element\ElementMissing');
$resources = $header->get('Resources');
$this->assert->variable($resources)->isNull();
}
public function testResolveXRef()
{
$document = new \Smalot\PdfParser\Document();
$content = '<</Type/Page/SubType/Text/Font 5 0 R/Resources 8 0 R>>foo';
$position = 0;
$header = \Smalot\PdfParser\Header::parse($content, $document, $position);
$object = new \Smalot\PdfParser\Page($document, $header);
$document->setObjects(array('5_0' => $object));
$this->assert->object($header->get('Font'))->isInstanceOf('\Smalot\PdfParser\Object');
$header=$header->get('Resources');
try {
$this->assert->variable($header)->isInstanceOf('\Smalot\PdfParser\Element\ElementMissing');
$this->assert->boolean(true)->isEqualTo(false);
} catch (\Exception $e) {
$this->assert->variable($header)->isNull();
}
}
}

View file

@ -0,0 +1,310 @@
<?php
/**
* @file
* This file is part of the PdfParser library.
*
* @author Sébastien MALOT <sebastien@malot.fr>
* @date 2017-01-03
* @license LGPLv3
* @url <https://github.com/smalot/pdfparser>
*
* PdfParser is a pdf library written in PHP, extraction oriented.
* Copyright (C) 2017 - Sébastien MALOT <sebastien@malot.fr>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program.
* If not, see <http://www.pdfparser.org/sites/default/LICENSE.txt>.
*
*/
namespace Smalot\PdfParser\Tests\Units;
use mageekguy\atoum;
/**
* Class Object
*
* @package Smalot\PdfParser\Tests\Units
*/
class Object extends atoum\test
{
const TYPE = 't';
const OPERATOR = 'o';
const COMMAND = 'c';
public function testGetTextParts()
{
}
// public function testGetCommandsImage()
// {
// $content = "/CS/RGB
///W 22
///H 1
///BPC 8
///F/Fl
///DP<</Predictor 15
///Columns 22
///Colors 3>>
//ID \x00\x50c\x63
//EI Q
//q -124.774 124.127 5.64213 5.67154 930.307 4436.95 cm
//BI
//";
//
// $document = new \Smalot\PdfParser\Document();
// $object = new \Smalot\PdfParser\Object($document);
// $offset = 0;
// $parts = $object->getCommandsImage($content, $offset);
// $reference = array(
// array(
// self::TYPE => '/',
// self::OPERATOR => 'CS',
// self::COMMAND => 'RGB',
// ),
// array(
// self::TYPE => '/',
// self::OPERATOR => 'W',
// self::COMMAND => '22',
// ),
// array(
// self::TYPE => '/',
// self::OPERATOR => 'H',
// self::COMMAND => '1',
// ),
// array(
// self::TYPE => '/',
// self::OPERATOR => 'BPC',
// self::COMMAND => '8',
// ),
// array(
// self::TYPE => '/',
// self::OPERATOR => 'F',
// self::COMMAND => 'Fl',
// ),
// array(
// self::TYPE => 'struct',
// self::OPERATOR => 'DP',
// self::COMMAND => array(
// array(
// self::TYPE => '/',
// self::OPERATOR => 'Predictor',
// self::COMMAND => '15',
// ),
// array(
// self::TYPE => '/',
// self::OPERATOR => 'Columns',
// self::COMMAND => '22',
// ),
// array(
// self::TYPE => '/',
// self::OPERATOR => 'Colors',
// self::COMMAND => '3',
// ),
// ),
// ),
// array(
// self::TYPE => '',
// self::OPERATOR => 'ID',
// self::COMMAND => "\x00\x50c\x63",
// ),
// );
//
// $this->assert->array($parts)->isEqualTo($reference);
// $this->assert->integer($offset)->isEqualTo(83);
// }
public function testGetCommandsText()
{
$content = "/R14 30 Tf 0.999016 0 0 1 137.4
342.561 Tm
[(A)-168.854( BC D)-220.905(\\(E\\))20.905<20>]
TJ /R14 17.16 Tf <20> Tj
0.999014 0 0 1 336.84 319.161 Tm T* ( \x00m)Tj
/R14 20.04 Tf
ET Q
q -124.774 124.127 5.64213 5.67154 930.307 4436.95 cm
BI";
$document = new \Smalot\PdfParser\Document();
$object = new \Smalot\PdfParser\Object($document);
$offset = 0;
$parts = $object->getCommandsText($content, $offset);
$reference = array(
array(
self::TYPE => '/',
self::OPERATOR => 'Tf',
self::COMMAND => 'R14 30',
),
array(
self::TYPE => '',
self::OPERATOR => 'Tm',
self::COMMAND => "0.999016 0 0 1 137.4\n342.561",
),
array(
self::TYPE => '[',
self::OPERATOR => 'TJ',
self::COMMAND => array(
array(
self::TYPE => '(',
self::OPERATOR => '',
self::COMMAND => 'A',
),
array(
self::TYPE => 'n',
self::OPERATOR => '',
self::COMMAND => '-168.854',
),
array(
self::TYPE => '(',
self::OPERATOR => '',
self::COMMAND => ' BC D',
),
array(
self::TYPE => 'n',
self::OPERATOR => '',
self::COMMAND => '-220.905',
),
array(
self::TYPE => '(',
self::OPERATOR => '',
self::COMMAND => '\\(E\\)',
),
array(
self::TYPE => 'n',
self::OPERATOR => '',
self::COMMAND => '20.905',
),
array(
self::TYPE => '<',
self::OPERATOR => '',
self::COMMAND => '20',
),
),
),
array(
self::TYPE => '/',
self::OPERATOR => 'Tf',
self::COMMAND => 'R14 17.16',
),
array(
self::TYPE => '<',
self::OPERATOR => 'Tj',
self::COMMAND => '20',
),
array(
self::TYPE => '',
self::OPERATOR => 'Tm',
self::COMMAND => '0.999014 0 0 1 336.84 319.161',
),
array(
self::TYPE => '',
self::OPERATOR => 'T*',
self::COMMAND => '',
),
array(
self::TYPE => '(',
self::OPERATOR => 'Tj',
self::COMMAND => " \x00m",
),
array(
self::TYPE => '/',
self::OPERATOR => 'Tf',
self::COMMAND => 'R14 20.04',
),
);
$this->assert->array($parts)->isEqualTo($reference);
$this->assert->integer($offset)->isEqualTo(172);
}
public function testCleanContent()
{
$content = '/Shape <</MCID << /Font<8>>> BT >>BDC
Q
/CS0 cs 1 1 0 scn
1 i
/GS0 gs
BT
/TT0 1 Tf
0.0007 Tc 0.0018 Tw 0 Ts 100 Tz 0 Tr 24 0 0 24 51.3 639.26025 Tm
(Modificatio[ns] au \\(14\\) septembre 2009 ET 2010)Tj
EMC
(ABC) Tj
[ (a)-4.5(b)6(c)8.8 ( fsdfsdfsdf[]sd) ] TD
ET
/Shape <</MCID 2 >>BDC
q
0.03 841';
$expected = '_____________________________________
Q
/CS0 cs 1 1 0 scn
1 i
/GS0 gs
BT
/TT0 1 Tf
0.0007 Tc 0.0018 Tw 0 Ts 100 Tz 0 Tr 24 0 0 24 51.3 639.26025 Tm
(________________________________________________)Tj
___
(___) Tj
[_____________________________________] TD
ET
______________________
q
0.03 841';
$document = new \Smalot\PdfParser\Document();
$object = new \Smalot\PdfParser\Object($document);
$cleaned = $object->cleanContent($content, '_');
$this->assert->string($cleaned)->length->isEqualTo(strlen($content));
$this->assert->string($cleaned)->isEqualTo($expected);
}
public function testGetSectionText()
{
$content = '/Shape <</MCID 1 >>BDC
Q
/CS0 cs 1 1 0 scn
1 i
/GS0 gs
BT
/TT0 1 Tf
0.0007 Tc 0.0018 Tw 0 Ts 100 Tz 0 Tr 24 0 0 24 51.3 639.26025 Tm
(Mod BT atio[ns] au \\(14\\) septembre 2009 ET 2010)Tj
EMC
(ABC) Tj
[ (a)-4.5(b) 6(c)8.8 ( fsdfsdfsdf[ sd) ] TD
ET
/Shape <</MCID [BT] >>BDC BT /TT1 1.5 Tf (BT )Tj ET
q
0.03 841';
$document = new \Smalot\PdfParser\Document();
$object = new \Smalot\PdfParser\Object($document);
$sections = $object->getSectionsText($content);
// $this->assert->string($cleaned)->length->isEqualTo(strlen($content));
// $this->assert->string($cleaned)->isEqualTo($expected);
}
}

View file

@ -0,0 +1,112 @@
<?php
/**
* @file
* This file is part of the PdfParser library.
*
* @author Sébastien MALOT <sebastien@malot.fr>
* @date 2017-01-03
* @license LGPLv3
* @url <https://github.com/smalot/pdfparser>
*
* PdfParser is a pdf library written in PHP, extraction oriented.
* Copyright (C) 2017 - Sébastien MALOT <sebastien@malot.fr>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program.
* If not, see <http://www.pdfparser.org/sites/default/LICENSE.txt>.
*
*/
namespace Smalot\PdfParser\Tests\Units;
use mageekguy\atoum;
/**
* Class Page
*
* @package Smalot\PdfParser\Tests\Units
*/
class Page extends atoum\test
{
public function testGetFonts()
{
// Document with text.
$filename = __DIR__ . '/../../../../../samples/Document1_pdfcreator_nocompressed.pdf';
$parser = new \Smalot\PdfParser\Parser();
$document = $parser->parseFile($filename);
$pages = $document->getPages();
$page = $pages[0];
// the first to load data.
$fonts = $page->getFonts();
$this->assert->array($fonts)->isNotEmpty();
foreach ($fonts as $font) {
$this->assert->object($font)->isInstanceOf('\Smalot\PdfParser\Font');
}
// the second to use cache.
$fonts = $page->getFonts();
$this->assert->array($fonts)->isNotEmpty();
// ------------------------------------------------------
// Document without text.
$filename = __DIR__ . '/../../../../../samples/Document3_pdfcreator_nocompressed.pdf';
$document = $parser->parseFile($filename);
$pages = $document->getPages();
$page = $pages[0];
// the first to load data.
$fonts = $page->getFonts();
$this->assert->array($fonts)->isEmpty();
// the second to use cache.
$fonts = $page->getFonts();
$this->assert->array($fonts)->isEmpty();
}
public function testGetFont()
{
// Document with text.
$filename = __DIR__ . '/../../../../../samples/Document1_pdfcreator_nocompressed.pdf';
$parser = new \Smalot\PdfParser\Parser();
$document = $parser->parseFile($filename);
$pages = $document->getPages();
$page = $pages[0];
// the first to load data.
$font = $page->getFont('R7');
$this->assert->object($font)->isInstanceOf('\Smalot\PdfParser\Font');
$font = $page->getFont('ABC7');
$this->assert->object($font)->isInstanceOf('\Smalot\PdfParser\Font');
}
public function testGetText()
{
// Document with text.
$filename = __DIR__ . '/../../../../../samples/Document1_pdfcreator_nocompressed.pdf';
$parser = new \Smalot\PdfParser\Parser();
$document = $parser->parseFile($filename);
$pages = $document->getPages();
$page = $pages[0];
$text = $page->getText();
$this->assert->string($text)->hasLengthGreaterThan(150);
$this->assert->string($text)->contains('Document title');
$this->assert->string($text)->contains('Lorem ipsum');
$this->assert->string($text)->contains('Calibri');
$this->assert->string($text)->contains('Arial');
$this->assert->string($text)->contains('Times');
$this->assert->string($text)->contains('Courier New');
$this->assert->string($text)->contains('Verdana');
}
}

View file

@ -0,0 +1,67 @@
<?php
/**
* @file
* This file is part of the PdfParser library.
*
* @author Sébastien MALOT <sebastien@malot.fr>
* @date 2017-01-03
* @license LGPLv3
* @url <https://github.com/smalot/pdfparser>
*
* PdfParser is a pdf library written in PHP, extraction oriented.
* Copyright (C) 2017 - Sébastien MALOT <sebastien@malot.fr>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program.
* If not, see <http://www.pdfparser.org/sites/default/LICENSE.txt>.
*
*/
namespace Smalot\PdfParser\Tests\Units;
use mageekguy\atoum;
/**
* Class Parser
*
* @package Smalot\PdfParser\Tests\Units
*/
class Parser extends atoum\test
{
public function testParseFile()
{
$directory = getcwd() . '/samples/bugs';
if (is_dir($directory)) {
$files = scandir($directory);
$parser = new \Smalot\PdfParser\Parser();
foreach ($files as $file) {
if (preg_match('/^.*\.pdf$/i', $file)) {
try {
$document = $parser->parseFile($directory . '/' . $file);
$pages = $document->getPages();
$page = $pages[0];
$content = $page->getText();
$this->assert->string($content);
} catch (\Exception $e) {
if ($e->getMessage() != 'Secured pdf file are currently not supported.' && strpos($e->getMessage(), 'TCPDF_PARSER') != 0) {
throw $e;
}
}
}
}
}
}
}