1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @file This file is part of the PdfParser library. |
5
|
|
|
* |
6
|
|
|
* @author Konrad Abicht <[email protected]> |
7
|
|
|
* @date 2020-06-02 |
8
|
|
|
* |
9
|
|
|
* @author Sébastien MALOT <[email protected]> |
10
|
|
|
* @date 2017-01-03 |
11
|
|
|
* |
12
|
|
|
* @license LGPLv3 |
13
|
|
|
* @url <https://github.com/smalot/pdfparser> |
14
|
|
|
* |
15
|
|
|
* PdfParser is a pdf library written in PHP, extraction oriented. |
16
|
|
|
* Copyright (C) 2017 - Sébastien MALOT <[email protected]> |
17
|
|
|
* |
18
|
|
|
* This program is free software: you can redistribute it and/or modify |
19
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
20
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
21
|
|
|
* (at your option) any later version. |
22
|
|
|
* |
23
|
|
|
* This program is distributed in the hope that it will be useful, |
24
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
25
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
26
|
|
|
* GNU Lesser General Public License for more details. |
27
|
|
|
* |
28
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
29
|
|
|
* along with this program. |
30
|
|
|
* If not, see <http://www.pdfparser.org/sites/default/LICENSE.txt>. |
31
|
|
|
*/ |
32
|
|
|
|
33
|
|
|
namespace Tests\Smalot\PdfParser\Integration\Element; |
34
|
|
|
|
35
|
|
|
use Smalot\PdfParser\Document; |
36
|
|
|
use Smalot\PdfParser\Element\ElementArray; |
37
|
|
|
use Smalot\PdfParser\Element\ElementNumeric; |
38
|
|
|
use Smalot\PdfParser\Header; |
39
|
|
|
use Smalot\PdfParser\Page; |
40
|
|
|
use Test\Smalot\PdfParser\TestCase; |
41
|
|
|
|
42
|
|
|
class ElementArrayTest extends TestCase |
43
|
|
|
{ |
44
|
|
|
public function testParse() |
45
|
|
|
{ |
46
|
|
|
$document = $this->getDocumentInstance(); |
47
|
|
|
|
48
|
|
|
// Skipped. |
49
|
|
|
$offset = 0; |
50
|
|
|
$element = ElementArray::parse('ABC', $document, $offset); |
51
|
|
|
$this->assertFalse($element); |
|
|
|
|
52
|
|
|
$this->assertEquals(0, $offset); |
53
|
|
|
|
54
|
|
|
$offset = 0; |
55
|
|
|
$element = ElementArray::parse(' / [ 4 2 ] ', $document, $offset); |
56
|
|
|
$this->assertFalse($element); |
57
|
|
|
$this->assertEquals(0, $offset); |
58
|
|
|
|
59
|
|
|
$offset = 0; |
60
|
|
|
$element = ElementArray::parse(' 0 [ 4 2 ] ', $document, $offset); |
61
|
|
|
$this->assertFalse($element); |
62
|
|
|
$this->assertEquals(0, $offset); |
63
|
|
|
|
64
|
|
|
$offset = 0; |
65
|
|
|
$element = ElementArray::parse(" 0 \n [ 4 2 ] ", $document, $offset); |
66
|
|
|
$this->assertFalse($element); |
67
|
|
|
$this->assertEquals(0, $offset); |
68
|
|
|
|
69
|
|
|
// Valid. |
70
|
|
|
$offset = 0; |
71
|
|
|
$element = ElementArray::parse(' [ 4 2 ] ', $document, $offset); |
72
|
|
|
$this->assertTrue($element->contains(2)); |
73
|
|
|
$this->assertTrue($element->contains(4)); |
74
|
|
|
$this->assertFalse($element->contains(8)); |
75
|
|
|
$this->assertEquals(8, $offset); |
76
|
|
|
|
77
|
|
|
$offset = 0; |
78
|
|
|
$element = ElementArray::parse(' [ 4 2 ]', $document, $offset); |
79
|
|
|
$this->assertTrue($element->contains(2)); |
80
|
|
|
$this->assertTrue($element->contains(4)); |
81
|
|
|
$this->assertFalse($element->contains(8)); |
82
|
|
|
$this->assertEquals(8, $offset); |
83
|
|
|
|
84
|
|
|
$offset = 0; |
85
|
|
|
$element = ElementArray::parse('[ 4 2 ]', $document, $offset); |
86
|
|
|
$this->assertTrue($element->contains(2)); |
87
|
|
|
$this->assertTrue($element->contains(4)); |
88
|
|
|
$this->assertFalse($element->contains(8)); |
89
|
|
|
$this->assertEquals(7, $offset); |
90
|
|
|
|
91
|
|
|
$offset = 0; |
92
|
|
|
$element = ElementArray::parse(" \n [ 4 2 ] ", $document, $offset); |
93
|
|
|
$this->assertTrue($element->contains(2)); |
94
|
|
|
$this->assertTrue($element->contains(4)); |
95
|
|
|
$this->assertFalse($element->contains(8)); |
96
|
|
|
$this->assertEquals(10, $offset); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function testGetContent() |
100
|
|
|
{ |
101
|
|
|
$val_4 = new ElementNumeric('4'); |
102
|
|
|
$val_2 = new ElementNumeric('2'); |
103
|
|
|
$element = new ElementArray([$val_4, $val_2]); |
|
|
|
|
104
|
|
|
|
105
|
|
|
$content = $element->getContent(); |
106
|
|
|
$this->assertCount(2, $content); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function testContains() |
110
|
|
|
{ |
111
|
|
|
$val_4 = new ElementNumeric('4'); |
112
|
|
|
$val_2 = new ElementNumeric('2'); |
113
|
|
|
$element = new ElementArray([$val_4, $val_2]); |
|
|
|
|
114
|
|
|
|
115
|
|
|
$this->assertTrue($element->contains(2)); |
116
|
|
|
$this->assertTrue($element->contains(4)); |
117
|
|
|
|
118
|
|
|
$this->assertFalse($element->contains(8)); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function testResolveXRef() |
122
|
|
|
{ |
123
|
|
|
// Document with text. |
124
|
|
|
$filename = $this->rootDir.'/samples/Document1_pdfcreator_nocompressed.pdf'; |
125
|
|
|
$parser = $this->getParserInstance(); |
126
|
|
|
$document = $parser->parseFile($filename); |
127
|
|
|
$object = $document->getObjectById('3_0'); |
128
|
|
|
$kids = $object->get('Kids'); |
129
|
|
|
|
130
|
|
|
$this->assertTrue($kids instanceof ElementArray); |
131
|
|
|
$this->assertCount(1, $kids->getContent()); |
132
|
|
|
|
133
|
|
|
$pages = $kids->getContent(); |
134
|
|
|
$this->assertTrue(reset($pages) instanceof Page); |
|
|
|
|
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function testGetDetails() |
138
|
|
|
{ |
139
|
|
|
$document = $this->getDocumentInstance(); |
140
|
|
|
$content = '<</Type/Page/Types[8]/Sizes[1 2 3 4 5 <</Subtype/XObject>> [8 [9 <</FontSize 10>>]]]>>'; |
141
|
|
|
$details_reference = [ |
142
|
|
|
'Type' => 'Page', |
143
|
|
|
'Types' => [ |
144
|
|
|
8, |
145
|
|
|
], |
146
|
|
|
'Sizes' => [ |
147
|
|
|
1, |
148
|
|
|
2, |
149
|
|
|
3, |
150
|
|
|
4, |
151
|
|
|
5, |
152
|
|
|
[ |
153
|
|
|
'Subtype' => 'XObject', |
154
|
|
|
], |
155
|
|
|
[ |
156
|
|
|
8, |
157
|
|
|
[ |
158
|
|
|
9, |
159
|
|
|
[ |
160
|
|
|
'FontSize' => 10, |
161
|
|
|
], |
162
|
|
|
], |
163
|
|
|
], |
164
|
|
|
], |
165
|
|
|
]; |
166
|
|
|
$header = Header::parse($content, $document); |
167
|
|
|
$details = $header->getDetails(); |
168
|
|
|
|
169
|
|
|
$this->assertCount(3, $details); |
170
|
|
|
$this->assertEquals($details_reference, $details); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function test__toString() |
174
|
|
|
{ |
175
|
|
|
$val_4 = new ElementNumeric('4'); |
176
|
|
|
$val_2 = new ElementNumeric('2'); |
177
|
|
|
$element = new ElementArray([$val_4, $val_2]); |
|
|
|
|
178
|
|
|
$this->assertEquals('4,2', (string) $element); |
179
|
|
|
|
180
|
|
|
$document = $this->getDocumentInstance(); |
181
|
|
|
$element = ElementArray::parse(' [ 4 2 ]', $document); |
182
|
|
|
$this->assertEquals('4,2', (string) $element); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|