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 Smalot\PdfParser\Document; |
36
|
|
|
use Smalot\PdfParser\Element\ElementMissing; |
37
|
|
|
use Smalot\PdfParser\Font; |
38
|
|
|
use Smalot\PdfParser\Page; |
39
|
|
|
use Tests\Smalot\PdfParser\TestCase; |
40
|
|
|
|
41
|
|
|
class PageTest extends TestCase |
42
|
|
|
{ |
43
|
|
|
public function testGetFonts() |
44
|
|
|
{ |
45
|
|
|
// Document with text. |
46
|
|
|
$filename = $this->rootDir.'/samples/Document1_pdfcreator_nocompressed.pdf'; |
47
|
|
|
$parser = $this->getParserInstance(); |
48
|
|
|
$document = $parser->parseFile($filename); |
49
|
|
|
$pages = $document->getPages(); |
50
|
|
|
$page = $pages[0]; |
51
|
|
|
|
52
|
|
|
// the first to load data. |
53
|
|
|
$fonts = $page->getFonts(); |
54
|
|
|
$this->assertTrue(0 < \count($fonts)); |
55
|
|
|
foreach ($fonts as $font) { |
56
|
|
|
$this->assertTrue($font instanceof Font); |
57
|
|
|
} |
58
|
|
|
// the second to use cache. |
59
|
|
|
$fonts = $page->getFonts(); |
60
|
|
|
$this->assertTrue(0 < \count($fonts)); |
61
|
|
|
|
62
|
|
|
// ------------------------------------------------------ |
63
|
|
|
// Document without text. |
64
|
|
|
$filename = $this->rootDir.'/samples/Document3_pdfcreator_nocompressed.pdf'; |
65
|
|
|
$document = $parser->parseFile($filename); |
66
|
|
|
$pages = $document->getPages(); |
67
|
|
|
$page = $pages[0]; |
68
|
|
|
|
69
|
|
|
// the first to load data. |
70
|
|
|
$fonts = $page->getFonts(); |
71
|
|
|
$this->assertEquals(0, \count($fonts)); |
72
|
|
|
// the second to use cache. |
73
|
|
|
$fonts = $page->getFonts(); |
74
|
|
|
$this->assertEquals(0, \count($fonts)); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testGetFontsElementMissing() |
78
|
|
|
{ |
79
|
|
|
$headerResources = $this->getMockBuilder('Smalot\PdfParser\Header') |
80
|
|
|
->disableOriginalConstructor() |
81
|
|
|
->getMock(); |
82
|
|
|
|
83
|
|
|
$headerResources->expects($this->once()) |
84
|
|
|
->method('has') |
85
|
|
|
->willReturn(true); |
86
|
|
|
|
87
|
|
|
$headerResources->expects($this->once()) |
88
|
|
|
->method('get') |
89
|
|
|
->willReturn(new ElementMissing()); |
90
|
|
|
|
91
|
|
|
$header = $this->getMockBuilder('Smalot\PdfParser\Header') |
92
|
|
|
->disableOriginalConstructor() |
93
|
|
|
->getMock(); |
94
|
|
|
|
95
|
|
|
$header->expects($this->once()) |
96
|
|
|
->method('get') |
97
|
|
|
->willReturn($headerResources); |
98
|
|
|
|
99
|
|
|
$page = new Page(new Document(), $header); |
100
|
|
|
$fonts = $page->getFonts(); |
101
|
|
|
|
102
|
|
|
$this->assertEmpty($fonts); |
103
|
|
|
$this->assertEquals([], $fonts); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function testGetFont() |
107
|
|
|
{ |
108
|
|
|
// Document with text. |
109
|
|
|
$filename = $this->rootDir.'/samples/Document1_pdfcreator_nocompressed.pdf'; |
110
|
|
|
$parser = $this->getParserInstance(); |
111
|
|
|
$document = $parser->parseFile($filename); |
112
|
|
|
$pages = $document->getPages(); |
113
|
|
|
$page = $pages[0]; |
114
|
|
|
|
115
|
|
|
// the first to load data. |
116
|
|
|
$font = $page->getFont('R7'); |
117
|
|
|
$this->assertTrue($font instanceof Font); |
118
|
|
|
|
119
|
|
|
$font = $page->getFont('ABC7'); |
120
|
|
|
$this->assertTrue($font instanceof Font); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function testGetText() |
124
|
|
|
{ |
125
|
|
|
// Document with text. |
126
|
|
|
$filename = $this->rootDir.'/samples/Document1_pdfcreator_nocompressed.pdf'; |
127
|
|
|
$parser = $this->getParserInstance(); |
128
|
|
|
$document = $parser->parseFile($filename); |
129
|
|
|
$pages = $document->getPages(); |
130
|
|
|
$page = $pages[0]; |
131
|
|
|
$text = $page->getText(); |
132
|
|
|
|
133
|
|
|
$this->assertTrue(150 < \strlen($text)); |
134
|
|
|
$this->assertStringContainsString('Document title', $text); |
135
|
|
|
$this->assertStringContainsString('Lorem ipsum', $text); |
136
|
|
|
|
137
|
|
|
$this->assertStringContainsString('Calibri', $text); |
138
|
|
|
$this->assertStringContainsString('Arial', $text); |
139
|
|
|
$this->assertStringContainsString('Times', $text); |
140
|
|
|
$this->assertStringContainsString('Courier New', $text); |
141
|
|
|
$this->assertStringContainsString('Verdana', $text); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @see https://github.com/smalot/pdfparser/pull/457 |
146
|
|
|
*/ |
147
|
|
|
public function testGetTextPullRequest457() |
148
|
|
|
{ |
149
|
|
|
// Document with text. |
150
|
|
|
$filename = $this->rootDir.'/samples/bugs/PullRequest457.pdf'; |
151
|
|
|
$parser = $this->getParserInstance(); |
152
|
|
|
$document = $parser->parseFile($filename); |
153
|
|
|
$pages = $document->getPages(); |
154
|
|
|
$page = $pages[0]; |
155
|
|
|
$text = $page->getText(); |
156
|
|
|
|
157
|
|
|
$this->assertTrue(1000 < \strlen($text)); |
158
|
|
|
$this->assertStringContainsString('SUPER', $text); |
159
|
|
|
$this->assertStringContainsString('VOORDEEL', $text); |
160
|
|
|
$this->assertStringContainsString('KRANT', $text); |
161
|
|
|
$this->assertStringContainsString('DINSDAG', $text); |
162
|
|
|
$this->assertStringContainsString('Snelfilterkoffie', $text); |
163
|
|
|
$this->assertStringContainsString('AardappelenZak', $text); |
164
|
|
|
$this->assertStringContainsString('ALL', $text); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function testExtractRawData() |
168
|
|
|
{ |
169
|
|
|
// Document with text. |
170
|
|
|
$filename = $this->rootDir.'/samples/Document1_pdfcreator_nocompressed.pdf'; |
171
|
|
|
$parser = $this->getParserInstance(); |
172
|
|
|
$document = $parser->parseFile($filename); |
173
|
|
|
$pages = $document->getPages(); |
174
|
|
|
$page = $pages[0]; |
175
|
|
|
$extractedRawData = $page->extractRawData(); |
176
|
|
|
|
177
|
|
|
$btItem = $extractedRawData[0]; |
178
|
|
|
$this->assertCount(3, $btItem); |
179
|
|
|
$this->assertArrayHasKey('t', $btItem); |
180
|
|
|
$this->assertArrayHasKey('o', $btItem); |
181
|
|
|
$this->assertArrayHasKey('c', $btItem); |
182
|
|
|
|
183
|
|
|
$this->assertEquals('BT', $btItem['o']); |
184
|
|
|
|
185
|
|
|
$tmItem = $extractedRawData[2]; |
186
|
|
|
|
187
|
|
|
$this->assertcount(174, $extractedRawData); |
188
|
|
|
$this->assertCount(3, $tmItem); |
189
|
|
|
|
190
|
|
|
$this->assertArrayHasKey('t', $tmItem); |
191
|
|
|
$this->assertArrayHasKey('o', $tmItem); |
192
|
|
|
$this->assertArrayHasKey('c', $tmItem); |
193
|
|
|
|
194
|
|
|
$this->assertStringContainsString('Tm', $tmItem['o']); |
195
|
|
|
$this->assertStringContainsString('0.999429 0 0 1 201.96 720.68', $tmItem['c']); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
public function testExtractDecodedRawData() |
199
|
|
|
{ |
200
|
|
|
// Document with text. |
201
|
|
|
$filename = $this->rootDir.'/samples/Document1_pdfcreator_nocompressed.pdf'; |
202
|
|
|
$parser = $this->getParserInstance(); |
203
|
|
|
$document = $parser->parseFile($filename); |
204
|
|
|
$pages = $document->getPages(); |
205
|
|
|
$page = $pages[0]; |
206
|
|
|
$extractedDecodedRawData = $page->extractDecodedRawData(); |
207
|
|
|
$tmItem = $extractedDecodedRawData[2]; |
208
|
|
|
$this->assertCount(174, $extractedDecodedRawData); |
209
|
|
|
$this->assertCount(3, $tmItem); |
210
|
|
|
|
211
|
|
|
$this->assertArrayHasKey('t', $tmItem); |
212
|
|
|
$this->assertArrayHasKey('o', $tmItem); |
213
|
|
|
$this->assertArrayHasKey('c', $tmItem); |
214
|
|
|
|
215
|
|
|
$this->assertStringContainsString('Tm', $tmItem['o']); |
216
|
|
|
$this->assertStringContainsString('0.999429 0 0 1 201.96 720.68', $tmItem['c']); |
217
|
|
|
|
218
|
|
|
$this->assertCount(3, $tmItem); |
219
|
|
|
$this->assertArrayHasKey('t', $tmItem); |
220
|
|
|
$this->assertArrayHasKey('o', $tmItem); |
221
|
|
|
$this->assertArrayHasKey('c', $tmItem); |
222
|
|
|
|
223
|
|
|
$tjItem = $extractedDecodedRawData[3]; |
224
|
|
|
$this->assertStringContainsString('TJ', $tjItem['o']); |
225
|
|
|
$this->assertStringContainsString('(', $tjItem['c'][0]['t']); |
226
|
|
|
$this->assertStringContainsString('D', $tjItem['c'][0]['c']); |
227
|
|
|
$this->assertStringContainsString('n', $tjItem['c'][1]['t']); |
228
|
|
|
$this->assertStringContainsString('0.325008', $tjItem['c'][1]['c']); |
229
|
|
|
$this->assertStringContainsString('(', $tjItem['c'][2]['t']); |
230
|
|
|
$this->assertStringContainsString('o', $tjItem['c'][2]['c']); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
public function testExtractRawDataWithCorruptedPdf() |
234
|
|
|
{ |
235
|
|
|
$this->expectException(\Exception::class); |
236
|
|
|
$this->expectExceptionMessage('Unable to find xref (PDF corrupted?)'); |
237
|
|
|
|
238
|
|
|
$this |
239
|
|
|
->getParserInstance() |
240
|
|
|
->parseFile($this->rootDir.'/samples/corrupted.pdf') |
241
|
|
|
->getPages(); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
public function testGetDataCommands() |
245
|
|
|
{ |
246
|
|
|
// Document with text. |
247
|
|
|
$filename = $this->rootDir.'/samples/Document1_pdfcreator_nocompressed.pdf'; |
248
|
|
|
$parser = $this->getParserInstance(); |
249
|
|
|
$document = $parser->parseFile($filename); |
250
|
|
|
$pages = $document->getPages(); |
251
|
|
|
$page = $pages[0]; |
252
|
|
|
$dataCommands = $page->getDataCommands(); |
253
|
|
|
$this->assertCount(168, $dataCommands); |
254
|
|
|
|
255
|
|
|
$tmItem = $dataCommands[1]; |
256
|
|
|
$this->assertCount(3, $tmItem); |
257
|
|
|
$this->assertArrayHasKey('t', $tmItem); |
258
|
|
|
$this->assertArrayHasKey('o', $tmItem); |
259
|
|
|
$this->assertArrayHasKey('c', $tmItem); |
260
|
|
|
|
261
|
|
|
$this->assertStringContainsString('Tm', $tmItem['o']); |
262
|
|
|
$this->assertStringContainsString('0.999429 0 0 1 201.96 720.68', $tmItem['c']); |
263
|
|
|
|
264
|
|
|
$tjItem = $dataCommands[2]; |
265
|
|
|
$this->assertCount(3, $tjItem); |
266
|
|
|
$this->assertArrayHasKey('t', $tjItem); |
267
|
|
|
$this->assertArrayHasKey('o', $tjItem); |
268
|
|
|
$this->assertArrayHasKey('c', $tjItem); |
269
|
|
|
|
270
|
|
|
$this->assertStringContainsString('TJ', $tjItem['o']); |
271
|
|
|
$this->assertStringContainsString('(', $tjItem['c'][0]['t']); |
272
|
|
|
$this->assertStringContainsString('D', $tjItem['c'][0]['c']); |
273
|
|
|
$this->assertStringContainsString('n', $tjItem['c'][1]['t']); |
274
|
|
|
$this->assertStringContainsString('0.325008', $tjItem['c'][1]['c']); |
275
|
|
|
$this->assertStringContainsString('(', $tjItem['c'][2]['t']); |
276
|
|
|
$this->assertStringContainsString('o', $tjItem['c'][2]['c']); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
public function testGetDataTm() |
280
|
|
|
{ |
281
|
|
|
// Document with text. |
282
|
|
|
$filename = $this->rootDir.'/samples/Document1_pdfcreator_nocompressed.pdf'; |
283
|
|
|
$parser = $this->getParserInstance(); |
284
|
|
|
$document = $parser->parseFile($filename); |
285
|
|
|
$pages = $document->getPages(); |
286
|
|
|
$page = $pages[0]; |
287
|
|
|
|
288
|
|
|
$dataTm = $page->getDataTm(); |
289
|
|
|
$this->assertCount(81, $dataTm); |
290
|
|
|
|
291
|
|
|
$item = $dataTm[0]; |
292
|
|
|
$this->assertCount(2, $item); |
293
|
|
|
$this->assertCount(6, $item[0]); |
294
|
|
|
$this->assertEquals( |
295
|
|
|
[ |
296
|
|
|
'0.999429', |
297
|
|
|
'0', |
298
|
|
|
'0', |
299
|
|
|
'1', |
300
|
|
|
'201.96', |
301
|
|
|
'720.68', |
302
|
|
|
], |
303
|
|
|
$item[0] |
304
|
|
|
); |
305
|
|
|
|
306
|
|
|
$this->assertStringContainsString('Document title', $item[1]); |
307
|
|
|
$item = $dataTm[2]; |
308
|
|
|
$this->assertEquals( |
309
|
|
|
[ |
310
|
|
|
'0.999402', |
311
|
|
|
'0', |
312
|
|
|
'0', |
313
|
|
|
'1', |
314
|
|
|
'70.8', |
315
|
|
|
'673.64', |
316
|
|
|
], |
317
|
|
|
$item[0] |
318
|
|
|
); |
319
|
|
|
|
320
|
|
|
$this->assertStringContainsString('Calibri : Lorem ipsum dolor sit amet, consectetur a', $item[1]); |
321
|
|
|
|
322
|
|
|
$item = $dataTm[80]; |
323
|
|
|
$this->assertEquals( |
324
|
|
|
[ |
325
|
|
|
'0.999402', |
326
|
|
|
'0', |
327
|
|
|
'0', |
328
|
|
|
'1', |
329
|
|
|
'343.003', |
330
|
|
|
'81.44', |
331
|
|
|
], |
332
|
|
|
$item[0] |
333
|
|
|
); |
334
|
|
|
$this->assertStringContainsString('nenatis.', $item[1]); |
335
|
|
|
|
336
|
|
|
// ------------------------------------------------------ |
337
|
|
|
// Document is a form |
338
|
|
|
$filename = $this->rootDir.'/samples/SimpleInvoiceFilledExample1.pdf'; |
339
|
|
|
$document = $parser->parseFile($filename); |
340
|
|
|
$pages = $document->getPages(); |
341
|
|
|
$page = $pages[0]; |
342
|
|
|
$dataTm = $page->getDataTm(); |
343
|
|
|
$item = $dataTm[2]; |
344
|
|
|
$this->assertCount(105, $dataTm); |
345
|
|
|
$this->assertCount(2, $item); |
346
|
|
|
$this->assertCount(6, $item[0]); |
347
|
|
|
$this->assertEquals( |
348
|
|
|
[ |
349
|
|
|
'1', |
350
|
|
|
'0', |
351
|
|
|
'0', |
352
|
|
|
'1', |
353
|
|
|
'167.3', |
354
|
|
|
'894.58', |
355
|
|
|
], |
356
|
|
|
$item[0] |
357
|
|
|
); |
358
|
|
|
$this->assertStringContainsString('MyName MyLastName', $item[1]); |
359
|
|
|
|
360
|
|
|
$item = $dataTm[6]; |
361
|
|
|
$this->assertEquals( |
362
|
|
|
[ |
363
|
|
|
'1', |
364
|
|
|
'0', |
365
|
|
|
'0', |
366
|
|
|
'1', |
367
|
|
|
'681.94', |
368
|
|
|
'877.42', |
369
|
|
|
], |
370
|
|
|
$item[0] |
371
|
|
|
); |
372
|
|
|
$this->assertStringContainsString('1/1/2020', $item[1]); |
373
|
|
|
|
374
|
|
|
$item = $dataTm[8]; |
375
|
|
|
$this->assertEquals( |
376
|
|
|
[ |
377
|
|
|
'1', |
378
|
|
|
'0', |
379
|
|
|
'0', |
380
|
|
|
'1', |
381
|
|
|
'174.86', |
382
|
|
|
'827.14', |
383
|
|
|
], |
384
|
|
|
$item[0] |
385
|
|
|
); |
386
|
|
|
$this->assertStringContainsString('Purchase 1', $item[1]); |
387
|
|
|
|
388
|
|
|
// ------------------------------------------------------ |
389
|
|
|
// Document is another form of the same type |
390
|
|
|
$filename = $this->rootDir.'/samples/SimpleInvoiceFilledExample2.pdf'; |
391
|
|
|
$document = $parser->parseFile($filename); |
392
|
|
|
$pages = $document->getPages(); |
393
|
|
|
$page = $pages[0]; |
394
|
|
|
$dataTm = $page->getDataTm(); |
395
|
|
|
|
396
|
|
|
$item = $dataTm[2]; |
397
|
|
|
$this->assertCount(105, $dataTm); |
398
|
|
|
$this->assertCount(2, $item); |
399
|
|
|
$this->assertCount(6, $item[0]); |
400
|
|
|
$this->assertEquals( |
401
|
|
|
[ |
402
|
|
|
'1', |
403
|
|
|
'0', |
404
|
|
|
'0', |
405
|
|
|
'1', |
406
|
|
|
'167.3', |
407
|
|
|
'894.58', |
408
|
|
|
], |
409
|
|
|
$item[0] |
410
|
|
|
); |
411
|
|
|
$this->assertStringContainsString("Other'sName Other'sLastName", $item[1]); |
412
|
|
|
|
413
|
|
|
$item = $dataTm[6]; |
414
|
|
|
$this->assertEquals( |
415
|
|
|
[ |
416
|
|
|
'1', |
417
|
|
|
'0', |
418
|
|
|
'0', |
419
|
|
|
'1', |
420
|
|
|
'681.94', |
421
|
|
|
'877.42', |
422
|
|
|
], |
423
|
|
|
$item[0] |
424
|
|
|
); |
425
|
|
|
$this->assertStringContainsString('2/2/2020', $item[1]); |
426
|
|
|
|
427
|
|
|
$item = $dataTm[8]; |
428
|
|
|
$this->assertEquals( |
429
|
|
|
[ |
430
|
|
|
'1', |
431
|
|
|
'0', |
432
|
|
|
'0', |
433
|
|
|
'1', |
434
|
|
|
'174.86', |
435
|
|
|
'827.14', |
436
|
|
|
], |
437
|
|
|
$item[0] |
438
|
|
|
); |
439
|
|
|
$this->assertStringContainsString('Purchase 2', $item[1]); |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
/** |
443
|
|
|
* Tests getDataTm with hexadecimal encoded document text. |
444
|
|
|
* |
445
|
|
|
* @see https://github.com/smalot/pdfparser/issues/336 |
446
|
|
|
*/ |
447
|
|
|
public function testGetDataTmIssue336() |
448
|
|
|
{ |
449
|
|
|
$filename = $this->rootDir.'/samples/bugs/Issue336_decode_hexadecimal.pdf'; |
450
|
|
|
$document = $this->getParserInstance()->parseFile($filename); |
451
|
|
|
$pages = $document->getPages(); |
452
|
|
|
$page = $pages[0]; |
453
|
|
|
$dataTm = $page->getDataTm(); |
454
|
|
|
|
455
|
|
|
$item = $dataTm[2]; |
456
|
|
|
$this->assertCount(13, $dataTm); |
457
|
|
|
$this->assertCount(2, $item); |
458
|
|
|
$this->assertCount(6, $item[0]); |
459
|
|
|
$this->assertEquals( |
460
|
|
|
[ |
461
|
|
|
'1', |
462
|
|
|
'0', |
463
|
|
|
'0', |
464
|
|
|
'1', |
465
|
|
|
'318.185', |
466
|
|
|
'665.044', |
467
|
|
|
], |
468
|
|
|
$item[0] |
469
|
|
|
); |
470
|
|
|
$this->assertEquals('Lorem', $item[1]); |
471
|
|
|
} |
472
|
|
|
|
473
|
|
|
/** |
474
|
|
|
* Tests that getPages() only returns Page objects |
475
|
|
|
* |
476
|
|
|
* @see https://github.com/smalot/pdfparser/issues/331 |
477
|
|
|
* |
478
|
|
|
* Sample pdf file provided by @Reqrefusion, see |
479
|
|
|
* https://github.com/smalot/pdfparser/pull/350#issuecomment-703195220 |
480
|
|
|
*/ |
481
|
|
|
public function testGetPages() |
482
|
|
|
{ |
483
|
|
|
$filename = $this->rootDir.'/samples/bugs/Issue331.pdf'; |
484
|
|
|
$document = $this->getParserInstance()->parseFile($filename); |
485
|
|
|
$pages = $document->getPages(); |
486
|
|
|
|
487
|
|
|
// This should actually be 3 pages, but as long as the cause for issue #331 |
488
|
|
|
// has not been found and the issue is not fixed, we'll settle for 2 here. |
489
|
|
|
// We still test for the count, so in case the bug should be fixed |
490
|
|
|
// unknowingly, we don't forget to resolve the issue as well and make sure |
491
|
|
|
// this assertion is present. |
492
|
|
|
$this->assertCount(2, $pages); |
493
|
|
|
|
494
|
|
|
foreach ($pages as $page) { |
495
|
|
|
$this->assertTrue($page instanceof Page); |
496
|
|
|
} |
497
|
|
|
} |
498
|
|
|
|
499
|
|
|
public function testGetTextXY() |
500
|
|
|
{ |
501
|
|
|
// Document with text. |
502
|
|
|
$filename = $this->rootDir.'/samples/Document1_pdfcreator_nocompressed.pdf'; |
503
|
|
|
$parser = $this->getParserInstance(); |
504
|
|
|
$document = $parser->parseFile($filename); |
505
|
|
|
$pages = $document->getPages(); |
506
|
|
|
$page = $pages[0]; |
507
|
|
|
$result = $page->getTextXY(201.96, 720.68); |
508
|
|
|
$this->assertCount(1, $result); |
509
|
|
|
$this->assertCount(2, $result[0]); |
510
|
|
|
$this->assertEquals( |
511
|
|
|
[ |
512
|
|
|
'0.999429', |
513
|
|
|
'0', |
514
|
|
|
'0', |
515
|
|
|
'1', |
516
|
|
|
'201.96', |
517
|
|
|
'720.68', |
518
|
|
|
], |
519
|
|
|
$result[0][0] |
520
|
|
|
); |
521
|
|
|
$this->assertStringContainsString('Document title', $result[0][1]); |
522
|
|
|
|
523
|
|
|
$result = $page->getTextXY(201, 720); |
524
|
|
|
$this->assertCount(0, $result); |
525
|
|
|
|
526
|
|
|
$result = $page->getTextXY(201, 720, 1, 1); |
527
|
|
|
$this->assertCount(1, $result); |
528
|
|
|
$this->assertCount(2, $result[0]); |
529
|
|
|
$this->assertEquals( |
530
|
|
|
[ |
531
|
|
|
'0.999429', |
532
|
|
|
'0', |
533
|
|
|
'0', |
534
|
|
|
'1', |
535
|
|
|
'201.96', |
536
|
|
|
'720.68', |
537
|
|
|
], |
538
|
|
|
$result[0][0] |
539
|
|
|
); |
540
|
|
|
$this->assertStringContainsString('Document title', $result[0][1]); |
541
|
|
|
|
542
|
|
|
// ------------------------------------------------------ |
543
|
|
|
// Document is a form |
544
|
|
|
$filename = $this->rootDir.'/samples/SimpleInvoiceFilledExample1.pdf'; |
545
|
|
|
$document = $parser->parseFile($filename); |
546
|
|
|
$pages = $document->getPages(); |
547
|
|
|
$page = $pages[0]; |
548
|
|
|
$result = $page->getTextXY(167, 894, 1, 1); |
549
|
|
|
$this->assertCount(1, $result); |
550
|
|
|
$this->assertCount(2, $result[0]); |
551
|
|
|
$this->assertEquals( |
552
|
|
|
[ |
553
|
|
|
'1', |
554
|
|
|
'0', |
555
|
|
|
'0', |
556
|
|
|
'1', |
557
|
|
|
'167.3', |
558
|
|
|
'894.58', |
559
|
|
|
], |
560
|
|
|
$result[0][0] |
561
|
|
|
); |
562
|
|
|
$this->assertStringContainsString('MyName MyLastName', $result[0][1]); |
563
|
|
|
|
564
|
|
|
$result = $page->getTextXY(681, 877, 1, 1); |
565
|
|
|
$this->assertStringContainsString('1/1/2020', $result[0][1]); |
566
|
|
|
|
567
|
|
|
$result = $page->getTextXY(174, 827, 1, 1); |
568
|
|
|
$this->assertStringContainsString('Purchase 1', $result[0][1]); |
569
|
|
|
|
570
|
|
|
// ------------------------------------------------------ |
571
|
|
|
// Document is another form of the same type |
572
|
|
|
$filename = $this->rootDir.'/samples/SimpleInvoiceFilledExample2.pdf'; |
573
|
|
|
$document = $parser->parseFile($filename); |
574
|
|
|
$pages = $document->getPages(); |
575
|
|
|
$page = $pages[0]; |
576
|
|
|
$result = $page->getTextXY(167, 894, 1, 1); |
577
|
|
|
$this->assertEquals( |
578
|
|
|
[ |
579
|
|
|
'1', |
580
|
|
|
'0', |
581
|
|
|
'0', |
582
|
|
|
'1', |
583
|
|
|
'167.3', |
584
|
|
|
'894.58', |
585
|
|
|
], |
586
|
|
|
$result[0][0] |
587
|
|
|
); |
588
|
|
|
$this->assertStringContainsString("Other'sName Other'sLastName", $result[0][1]); |
589
|
|
|
|
590
|
|
|
$result = $page->getTextXY(681, 877, 1, 1); |
591
|
|
|
$this->assertStringContainsString('2/2/2020', $result[0][1]); |
592
|
|
|
|
593
|
|
|
$result = $page->getTextXY(174, 827, 1, 1); |
594
|
|
|
$this->assertStringContainsString('Purchase 2', $result[0][1]); |
595
|
|
|
} |
596
|
|
|
|
597
|
|
|
public function testExtractDecodedRawDataIssue450() |
598
|
|
|
{ |
599
|
|
|
$filename = $this->rootDir.'/samples/bugs/Issue450.pdf'; |
600
|
|
|
$parser = $this->getParserInstance(); |
601
|
|
|
$document = $parser->parseFile($filename); |
602
|
|
|
$pages = $document->getPages(); |
603
|
|
|
$page = $pages[0]; |
604
|
|
|
$extractedDecodedRawData = $page->extractDecodedRawData(); |
605
|
|
|
$this->assertIsArray($extractedDecodedRawData); |
606
|
|
|
$this->assertGreaterThan(3, \count($extractedDecodedRawData)); |
607
|
|
|
$this->assertIsArray($extractedDecodedRawData[3]); |
608
|
|
|
$this->assertEquals('TJ', $extractedDecodedRawData[3]['o']); |
609
|
|
|
$this->assertIsArray($extractedDecodedRawData[3]['c']); |
610
|
|
|
$this->assertIsArray($extractedDecodedRawData[3]['c'][0]); |
611
|
|
|
$this->assertEquals(3, \count($extractedDecodedRawData[3]['c'][0])); |
612
|
|
|
$this->assertEquals('{signature:signer505906:Please+Sign+Here}', $extractedDecodedRawData[3]['c'][0]['c']); |
613
|
|
|
} |
614
|
|
|
|
615
|
|
|
public function testGetDataTmIssue450() |
616
|
|
|
{ |
617
|
|
|
$filename = $this->rootDir.'/samples/bugs/Issue450.pdf'; |
618
|
|
|
$parser = $this->getParserInstance(); |
619
|
|
|
$document = $parser->parseFile($filename); |
620
|
|
|
$pages = $document->getPages(); |
621
|
|
|
$page = $pages[0]; |
622
|
|
|
$dataTm = $page->getDataTm(); |
623
|
|
|
$this->assertIsArray($dataTm); |
624
|
|
|
$this->assertEquals(1, \count($dataTm)); |
625
|
|
|
$this->assertIsArray($dataTm[0]); |
626
|
|
|
$this->assertEquals(2, \count($dataTm[0])); |
627
|
|
|
$this->assertIsArray($dataTm[0][0]); |
628
|
|
|
$this->assertEquals(6, \count($dataTm[0][0])); |
629
|
|
|
$this->assertEquals(1, $dataTm[0][0][0]); |
630
|
|
|
$this->assertEquals(0, $dataTm[0][0][1]); |
631
|
|
|
$this->assertEquals(0, $dataTm[0][0][2]); |
632
|
|
|
$this->assertEquals(1, $dataTm[0][0][3]); |
633
|
|
|
$this->assertEquals(67.5, $dataTm[0][0][4]); |
634
|
|
|
$this->assertEquals(756.25, $dataTm[0][0][5]); |
635
|
|
|
$this->assertEquals('{signature:signer505906:Please+Sign+Here}', $dataTm[0][1]); |
636
|
|
|
} |
637
|
|
|
|
638
|
|
|
public function testIsFpdf() |
639
|
|
|
{ |
640
|
|
|
$filename = $this->rootDir.'/samples/Document1_foxitreader.pdf'; |
641
|
|
|
$parser = $this->getParserInstance(); |
642
|
|
|
$document = $parser->parseFile($filename); |
643
|
|
|
$pages = $document->getPages(); |
644
|
|
|
$page = $pages[0]; |
645
|
|
|
$this->assertFalse($page->isFpdf()); |
646
|
|
|
$filename = $this->rootDir.'/samples/bugs/Issue454.pdf'; |
647
|
|
|
$document = $parser->parseFile($filename); |
648
|
|
|
$pages = $document->getPages(); |
649
|
|
|
$page = $pages[0]; |
650
|
|
|
$this->assertTrue($page->isFpdf()); |
651
|
|
|
} |
652
|
|
|
|
653
|
|
|
public function testGetPageNumber() |
654
|
|
|
{ |
655
|
|
|
$filename = $this->rootDir.'/samples/Document1_foxitreader.pdf'; |
656
|
|
|
$parser = $this->getParserInstance(); |
657
|
|
|
$document = $parser->parseFile($filename); |
658
|
|
|
$pages = $document->getPages(); |
659
|
|
|
$page = $pages[0]; |
660
|
|
|
$this->assertEquals(0, $page->getPageNumber()); |
661
|
|
|
$filename = $this->rootDir.'/samples/Document1_pdfcreator.pdf'; |
662
|
|
|
$document = $parser->parseFile($filename); |
663
|
|
|
$pages = $document->getPages(); |
664
|
|
|
$page = $pages[0]; |
665
|
|
|
$this->assertEquals(0, $page->getPageNumber()); |
666
|
|
|
$filename = $this->rootDir.'/samples/Document2_pdfcreator_nocompressed.pdf'; |
667
|
|
|
$document = $parser->parseFile($filename); |
668
|
|
|
$pages = $document->getPages(); |
669
|
|
|
$page = $pages[0]; |
670
|
|
|
$this->assertEquals(0, $page->getPageNumber()); |
671
|
|
|
$filename = $this->rootDir.'/samples/InternationalChars.pdf'; |
672
|
|
|
$document = $parser->parseFile($filename); |
673
|
|
|
$pages = $document->getPages(); |
674
|
|
|
$page = $pages[0]; |
675
|
|
|
$this->assertEquals(0, $page->getPageNumber()); |
676
|
|
|
$filename = $this->rootDir.'/samples/SimpleInvoiceFilledExample1.pdf'; |
677
|
|
|
$document = $parser->parseFile($filename); |
678
|
|
|
$pages = $document->getPages(); |
679
|
|
|
$page = $pages[0]; |
680
|
|
|
$this->assertEquals(0, $page->getPageNumber()); |
681
|
|
|
$filename = $this->rootDir.'/samples/bugs/Issue454.pdf'; |
682
|
|
|
$document = $parser->parseFile($filename); |
683
|
|
|
$pages = $document->getPages(); |
684
|
|
|
$page = $pages[0]; |
685
|
|
|
$this->assertEquals(0, $page->getPageNumber()); |
686
|
|
|
$page = $pages[1]; |
687
|
|
|
$this->assertEquals(1, $page->getPageNumber()); |
688
|
|
|
$page = $pages[2]; |
689
|
|
|
$this->assertEquals(2, $page->getPageNumber()); |
690
|
|
|
$page = $pages[3]; |
691
|
|
|
$this->assertEquals(3, $page->getPageNumber()); |
692
|
|
|
} |
693
|
|
|
|
694
|
|
|
public function testIssue454() |
695
|
|
|
{ |
696
|
|
|
$filename = $this->rootDir.'/samples/bugs/Issue454.pdf'; |
697
|
|
|
$parser = $this->getParserInstance(); |
698
|
|
|
$document = $parser->parseFile($filename); |
699
|
|
|
$pages = $document->getPages(); |
700
|
|
|
$page = $pages[0]; |
701
|
|
|
$dataTm = $page->getDataTm(); |
702
|
|
|
$this->assertIsArray($dataTm); |
703
|
|
|
$this->assertGreaterThan(0, \count($dataTm)); |
704
|
|
|
$this->assertIsArray($dataTm[0]); |
705
|
|
|
$this->assertEquals(2, \count($dataTm[0])); |
706
|
|
|
$this->assertIsArray($dataTm[0][0]); |
707
|
|
|
$this->assertEquals(6, \count($dataTm[0][0])); |
708
|
|
|
$this->assertEquals(201.96, $dataTm[0][0][4]); |
709
|
|
|
$this->assertEquals(720.68, $dataTm[0][0][5]); |
710
|
|
|
$this->assertStringContainsString('Document title', $dataTm[0][1]); |
711
|
|
|
$textData = $page->getTextXY(201.96, 720.68); |
712
|
|
|
$this->assertStringContainsString('Document title', $textData[0][1]); |
713
|
|
|
$page = $pages[2]; |
714
|
|
|
$dataTm = $page->getDataTm(); |
715
|
|
|
$this->assertIsArray($dataTm); |
716
|
|
|
$this->assertGreaterThan(0, \count($dataTm)); |
717
|
|
|
$this->assertIsArray($dataTm[0]); |
718
|
|
|
$this->assertEquals(2, \count($dataTm[0])); |
719
|
|
|
$this->assertIsArray($dataTm[0][0]); |
720
|
|
|
$this->assertEquals(6, \count($dataTm[0][0])); |
721
|
|
|
$this->assertEquals(67.5, $dataTm[0][0][4]); |
722
|
|
|
$this->assertEquals(756.25, $dataTm[0][0][5]); |
723
|
|
|
$this->assertStringContainsString('{signature:signer505906:Please+Sign+Here}', $dataTm[0][1]); |
724
|
|
|
$textData = $page->getTextXY(67.5, 756.25); |
725
|
|
|
$this->assertStringContainsString('{signature:signer505906:Please+Sign+Here}', $textData[0][1]); |
726
|
|
|
} |
727
|
|
|
} |
728
|
|
|
|