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-01 |
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; |
34
|
|
|
|
35
|
|
|
use Exception; |
36
|
|
|
use Smalot\PdfParser\Parser; |
37
|
|
|
use Smalot\PdfParser\Document; |
38
|
|
|
use Smalot\PdfParser\XObject\Image; |
39
|
|
|
use Tests\Smalot\PdfParser\TestCase; |
40
|
|
|
|
41
|
|
|
class ParserTest extends TestCase |
42
|
|
|
{ |
43
|
|
|
public function setUp() |
44
|
|
|
{ |
45
|
|
|
parent::setUp(); |
46
|
|
|
|
47
|
|
|
$this->fixture = new Parser(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testParseFile() |
51
|
|
|
{ |
52
|
|
|
$directory = $this->rootDir.'/samples/bugs'; |
53
|
|
|
|
54
|
|
|
if (is_dir($directory)) { |
55
|
|
|
$files = scandir($directory); |
56
|
|
|
|
57
|
|
|
foreach ($files as $file) { |
58
|
|
|
if (preg_match('/^.*\.pdf$/i', $file)) { |
59
|
|
|
try { |
60
|
|
|
$document = $this->fixture->parseFile($directory.'/'.$file); |
61
|
|
|
$pages = $document->getPages(); |
62
|
|
|
$this->assertTrue(0 < \count($pages)); |
63
|
|
|
|
64
|
|
|
foreach ($pages as $page) { |
65
|
|
|
$content = $page->getText(); |
66
|
|
|
$this->assertTrue(0 < \strlen($content)); |
67
|
|
|
} |
68
|
|
|
} catch (Exception $e) { |
69
|
|
|
if ( |
70
|
|
|
'Secured pdf file are currently not supported.' !== $e->getMessage() |
71
|
|
|
&& 0 != strpos($e->getMessage(), 'TCPDF_PARSER') |
72
|
|
|
) { |
73
|
|
|
throw $e; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function testIssue19() |
82
|
|
|
{ |
83
|
|
|
$fixture = new ParserSub(); |
84
|
|
|
$structure = [ |
85
|
|
|
[ |
86
|
|
|
'<<', |
87
|
|
|
[ |
88
|
|
|
[ |
89
|
|
|
'/', |
90
|
|
|
'Type', |
91
|
|
|
7735, |
92
|
|
|
], |
93
|
|
|
[ |
94
|
|
|
'/', |
95
|
|
|
'ObjStm', |
96
|
|
|
7742, |
97
|
|
|
] |
98
|
|
|
] |
99
|
|
|
], |
100
|
|
|
[ |
101
|
|
|
'stream', |
102
|
|
|
'', |
103
|
|
|
7804, |
104
|
|
|
[ |
105
|
|
|
"17\n0", |
106
|
|
|
[] |
107
|
|
|
] |
108
|
|
|
] |
109
|
|
|
]; |
110
|
|
|
$document = new Document(); |
111
|
|
|
$this->catchAllErrors(); |
112
|
|
|
$fixture->exposedParseObject('19_0', $structure, $document); |
113
|
|
|
// reset the error handler here, so we can catch the exception following |
114
|
|
|
restore_error_handler(); |
115
|
|
|
|
116
|
|
|
// we could use |
117
|
|
|
// $this->expectNotToPerformAssertions(); |
118
|
|
|
// here, but it's not supported by the PHPUnit version used by PHP 5.6 |
119
|
|
|
// and will cause the CI builds to fail in that environment. |
120
|
|
|
// Therefore, we try to get the document text, which will fail expectedly, |
121
|
|
|
// as the test data does not represent a complete PDF file. |
122
|
|
|
// |
123
|
|
|
// TODO: Once PHP 5 support is dropped from the library, remove this workaround |
124
|
|
|
$this->expectExceptionMessage('Missing catalog.'); |
125
|
|
|
$document->getText(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Test that issue related pdf can now be parsed |
130
|
|
|
* |
131
|
|
|
* @see https://github.com/smalot/pdfparser/issues/267 |
132
|
|
|
*/ |
133
|
|
|
public function testIssue267() |
134
|
|
|
{ |
135
|
|
|
$filename = $this->rootDir.'/samples/bugs/Issue267_array_access_on_int.pdf'; |
136
|
|
|
|
137
|
|
|
$document = $this->fixture->parseFile($filename); |
138
|
|
|
|
139
|
|
|
$this->assertEquals(Image::class, \get_class($document->getObjectById('128_0'))); |
140
|
|
|
$this->assertStringContainsString('4 von 4', $document->getText()); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
class ParserSub extends Parser { |
145
|
|
|
public function exposedParseObject($id, $structure, $document) { |
146
|
|
|
return $this->parseObject($id, $structure, $document); |
|
|
|
|
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.