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\Document; |
37
|
|
|
use Smalot\PdfParser\Parser; |
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
|
|
|
/** |
82
|
|
|
* Properly decode international unicode characters |
83
|
|
|
* |
84
|
|
|
* @todo the other languages in the test document need work because of issues with UTF-16 decoding (Chinese, Japanese) and missing right-to-left language support |
85
|
|
|
*/ |
86
|
|
|
public function testUnicodeDecoding() |
87
|
|
|
{ |
88
|
|
|
$filename = $this->rootDir.'/samples/InternationalChars.pdf'; |
89
|
|
|
|
90
|
|
|
$document = $this->fixture->parseFile($filename); |
91
|
|
|
|
92
|
|
|
$testString_cyrillic = "Лорем ипсум долор сит амет, еу сед либрис долорем инцоррупте. Ут лорем долоре граеце хис, модо \nаппареат сапиентем ут мел. Хис ат лаборе омнесяуе сигниферумяуе, тале анциллае ан еум, ех сед синт \nнобис. Сед модус вивендо цопиосае еа, сапиентем цонцептам хис не, яуандо сплендиде еум те."; |
93
|
|
|
$testString_greek = "Λορεμ ιπσθμ δολορ σιτ αμετ, τατιον cονστιτθαμ ομιτταντθρ εα σεα, αθδιαμ μανδαμθσ μελ τε. Διcο μθτατ \nινδοcτθμ εοσ ει, ει vιξ σονετ παρτιενδο ινcορρθπτε. Επιcθρι αντιοπαμ εθ νεc, ναμ λεγιμθσ γθβεργρεν ιν. \nVιξ σολετ ρεcτεqθε εα, ηασ νο αλιqθαμ μινιμθμ. Ιδ προ περcιπιτ περιcθλισ δετερρθισσετ, ιν νεc αππετερε \nομιτταντθρ ελοqθεντιαμ, ορατιο δοcτθσ ναμ αδ. Ετ σιτ σολθμ ρεcθσαβο, vιξ θτ λοβορτισ σπλενδιδε \nρεπθδιανδαε."; |
94
|
|
|
$testString_armenian = "լոռեմ իպսում դոլոռ սիթ ամեթ վիս ին իմպեդիթ ադմոդում ծու ապպառեաթ սծռիպսեռիթ մել մել եթ \nդոմինգ ծոնսեքուունթուռ ծիվիբուս վիվենդում պռոդեսսեթ ադ մեի թիբիքուե ապպառեաթ սիմիլիքուե թե \nվիմ վիխ ծասե սեմպեռ դոլոռեմ եխ եամ եա սթեթ մեդիոծռեմ ծոնսեթեթուռ ռաթիոնիբուս ինթելլեգամ \nմել թե"; |
95
|
|
|
$testString_georgean = "ლორემ იფსუმ დოლორ სით ამეთ ესთ ეთ სონეთ ზრილ მელიუს ელიგენდი თორყუათოს \nელოყუენთიამ ესთ ეხ უსუ ფალლი ალთერა ცეთეროს ინ ეთ ომითთამ თრაცთათოს ჰის ეუ ველ \nალთერუმ ვოლუფთათუმ მაზიმ ფერთინახ ჰენდრერით ინ ფრი ნეც ინ თემფორ ფეთენთიუმ ვერო \nფოსთულანთ ელოყუენთიამ უსუ ნე ან ყუი ლიბერ ეფიცური ასსუევერით იდ ნიბჰ ყუას ჰაბემუს სეა"; |
96
|
|
|
$testString_korean = "그 임기는 4년으로 하며. 이 경우 그 명령에 의하여 개정 또는 폐지되었던 법률은 그 명령이 승인을 얻지 못한 때부터 당연히 효력을 \n회복한다. 가부동수인 때에는 부결된 것으로 본다. 법률과 적법한 절차에 의하지 아니하고는 처벌·보안처분 또는 강제노역을 받지 \n아니한다."; |
97
|
|
|
$testString_western = 'ÄÖÜöäüßẞ Ññ¡¿ øÅå'; |
98
|
|
|
|
99
|
|
|
$this->assertStringContainsString($testString_cyrillic, $document->getText()); |
100
|
|
|
$this->assertStringContainsString($testString_greek, $document->getText()); |
101
|
|
|
$this->assertStringContainsString($testString_armenian, $document->getText()); |
102
|
|
|
$this->assertStringContainsString($testString_georgean, $document->getText()); |
103
|
|
|
$this->assertStringContainsString($testString_korean, $document->getText()); |
104
|
|
|
$this->assertStringContainsString($testString_western, $document->getText()); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Tests that xrefs with line breaks between id and position are parsed correctly |
109
|
|
|
* |
110
|
|
|
* @see https://github.com/smalot/pdfparser/issues/336 |
111
|
|
|
*/ |
112
|
|
|
public function testIssue19() |
113
|
|
|
{ |
114
|
|
|
$fixture = new ParserSub(); |
115
|
|
|
$structure = [ |
116
|
|
|
[ |
117
|
|
|
'<<', |
118
|
|
|
[ |
119
|
|
|
[ |
120
|
|
|
'/', |
121
|
|
|
'Type', |
122
|
|
|
7735, |
123
|
|
|
], |
124
|
|
|
[ |
125
|
|
|
'/', |
126
|
|
|
'ObjStm', |
127
|
|
|
7742, |
128
|
|
|
], |
129
|
|
|
], |
130
|
|
|
], |
131
|
|
|
[ |
132
|
|
|
'stream', |
133
|
|
|
'', |
134
|
|
|
7804, |
135
|
|
|
[ |
136
|
|
|
"17\n0", |
137
|
|
|
[], |
138
|
|
|
], |
139
|
|
|
], |
140
|
|
|
]; |
141
|
|
|
$document = new Document(); |
142
|
|
|
|
143
|
|
|
$fixture->exposedParseObject('19_0', $structure, $document); |
144
|
|
|
$objects = $fixture->getObjects(); |
145
|
|
|
|
146
|
|
|
$this->assertArrayHasKey('17_0', $objects); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Properly decode ANSI encodings without producing scrambled UTF-8 characters |
151
|
|
|
* |
152
|
|
|
* @see https://github.com/smalot/pdfparser/issues/202 |
153
|
|
|
* @see https://github.com/smalot/pdfparser/pull/257 |
154
|
|
|
*/ |
155
|
|
|
public function testIssue202() |
156
|
|
|
{ |
157
|
|
|
$filename = $this->rootDir.'/samples/bugs/Issue202.pdf'; |
158
|
|
|
|
159
|
|
|
$document = $this->fixture->parseFile($filename); |
160
|
|
|
|
161
|
|
|
$this->assertEquals('„fööbär“', $document->getText()); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Test that issue related pdf can now be parsed |
166
|
|
|
* |
167
|
|
|
* @see https://github.com/smalot/pdfparser/issues/267 |
168
|
|
|
*/ |
169
|
|
|
public function testIssue267() |
170
|
|
|
{ |
171
|
|
|
$filename = $this->rootDir.'/samples/bugs/Issue267_array_access_on_int.pdf'; |
172
|
|
|
|
173
|
|
|
$document = $this->fixture->parseFile($filename); |
174
|
|
|
|
175
|
|
|
$this->assertEquals(Image::class, \get_class($document->getObjectById('128_0'))); |
176
|
|
|
$this->assertStringContainsString('4 von 4', $document->getText()); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Test that issue related pdf can now be parsed: |
181
|
|
|
* Too many slashes were being stripped and resulted |
182
|
|
|
* in malformed encoding of parts of the text content. |
183
|
|
|
* |
184
|
|
|
* @see https://github.com/smalot/pdfparser/issues/322 |
185
|
|
|
*/ |
186
|
|
|
public function testIssue322() |
187
|
|
|
{ |
188
|
|
|
$filename = $this->rootDir.'/samples/bugs/Issue322.pdf'; |
189
|
|
|
|
190
|
|
|
$document = $this->fixture->parseFile($filename); |
191
|
|
|
|
192
|
|
|
$this->assertStringContainsString('this text isn’t working properly, I’ve edited it in Google Documents', $document->getText()); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Test that issue related pdf can now be parsed: |
197
|
|
|
* Too many slashes were being stripped and resulted |
198
|
|
|
* in malformed encoding of parts of the text content. |
199
|
|
|
* |
200
|
|
|
* License of the content taken from https://stackoverflow.com in the sample PDF: |
201
|
|
|
* CC BY-SA 2.5 https://creativecommons.org/licenses/by-sa/2.5/ |
202
|
|
|
* |
203
|
|
|
* @see https://github.com/smalot/pdfparser/issues/334 |
204
|
|
|
*/ |
205
|
|
|
public function testIssue334() |
206
|
|
|
{ |
207
|
|
|
$filename = $this->rootDir.'/samples/bugs/Issue334.pdf'; |
208
|
|
|
|
209
|
|
|
$document = $this->fixture->parseFile($filename); |
210
|
|
|
|
211
|
|
|
$this->assertStringContainsString('This question already has an answer here', $document->getText()); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Test that issue related pdf can now be parsed: |
216
|
|
|
* Glyphs not in the Postscript lookup table would cause "Notice: Undefined offset" |
217
|
|
|
* |
218
|
|
|
* @see https://github.com/smalot/pdfparser/issues/359 |
219
|
|
|
*/ |
220
|
|
|
public function testIssue359() |
221
|
|
|
{ |
222
|
|
|
$filename = $this->rootDir.'/samples/bugs/Issue359.pdf'; |
223
|
|
|
|
224
|
|
|
$document = $this->fixture->parseFile($filename); |
225
|
|
|
|
226
|
|
|
$this->assertStringContainsString('dnia 10 maja 2018 roku o ochronie danych osobowych', $document->getText()); |
227
|
|
|
$this->assertStringContainsString('sprawie ochrony osób fizycznych w związku', $document->getText()); |
228
|
|
|
/* |
229
|
|
|
* @todo Note that the "ł" in przepływu is decoded as a space character. This was already |
230
|
|
|
* the case before the PR that caused this issue and is not currently covered by this |
231
|
|
|
* test case. However, this issue should be addressed in the future and its fix can then |
232
|
|
|
* be incorporated into this test by uncommenting the following assertion. |
233
|
|
|
*/ |
234
|
|
|
// $this->assertStringContainsString('sprawie swobodnego przepływu takich danych oraz uchylenia dyrektywy', $document->getText()); |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
class ParserSub extends Parser |
239
|
|
|
{ |
240
|
|
|
public function exposedParseObject($id, $structure, $document) |
241
|
|
|
{ |
242
|
|
|
return $this->parseObject($id, $structure, $document); |
|
|
|
|
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
public function getObjects() |
246
|
|
|
{ |
247
|
|
|
return $this->objects; |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
|
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.