PageTest   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 913
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 621
c 2
b 1
f 0
dl 0
loc 913
rs 9.9789
wmc 25

22 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetText() 0 19 1
A testExtractRawData() 0 29 1
A testGetTextPullRequest457() 0 18 1
A testExtractDecodedRawData() 0 33 1
A testExtractRawDataWithCorruptedPdf() 0 9 1
A testGetFontsElementMissing() 0 27 1
A testGetFont() 0 15 1
A testGetFonts() 0 32 2
B testGetTextXY() 0 110 1
A testIssue629WithoutDataTmFontInfo() 0 13 1
A testIssue629WithDataTmFontInfo() 0 14 1
A testCmCommandInPdfs() 0 30 1
A testExtractDecodedRawDataIssue450() 0 16 1
A testIsFpdf() 0 13 1
A testGetDataTmIssue336() 0 24 1
A testIssue454() 0 32 1
B testGetDataTm() 0 252 1
A testGetDataCommands() 0 33 1
A testDataTmFontInfoHasToBeIncluded() 0 37 2
A testGetPages() 0 20 2
A testGetPageNumber() 0 39 1
A testGetDataTmIssue450() 0 21 1
1
<?php
2
3
/**
4
 * @file This file is part of the PdfParser library.
5
 *
6
 * @author  Konrad Abicht <[email protected]>
7
 *
8
 * @date    2020-06-01
9
 *
10
 * @author  Sébastien MALOT <[email protected]>
11
 *
12
 * @date    2017-01-03
13
 *
14
 * @license LGPLv3
15
 *
16
 * @url     <https://github.com/smalot/pdfparser>
17
 *
18
 *  PdfParser is a pdf library written in PHP, extraction oriented.
19
 *  Copyright (C) 2017 - Sébastien MALOT <[email protected]>
20
 *
21
 *  This program is free software: you can redistribute it and/or modify
22
 *  it under the terms of the GNU Lesser General Public License as published by
23
 *  the Free Software Foundation, either version 3 of the License, or
24
 *  (at your option) any later version.
25
 *
26
 *  This program is distributed in the hope that it will be useful,
27
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
28
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29
 *  GNU Lesser General Public License for more details.
30
 *
31
 *  You should have received a copy of the GNU Lesser General Public License
32
 *  along with this program.
33
 *  If not, see <http://www.pdfparser.org/sites/default/LICENSE.txt>.
34
 */
35
36
namespace PHPUnitTests\Integration;
37
38
use PHPUnitTests\TestCase;
39
use Smalot\PdfParser\Config;
40
use Smalot\PdfParser\Document;
41
use Smalot\PdfParser\Element\ElementMissing;
42
use Smalot\PdfParser\Font;
43
use Smalot\PdfParser\Page;
44
45
class PageTest extends TestCase
46
{
47
    public function testGetFonts(): void
48
    {
49
        // Document with text.
50
        $filename = $this->rootDir.'/samples/Document1_pdfcreator_nocompressed.pdf';
51
        $parser = $this->getParserInstance();
52
        $document = $parser->parseFile($filename);
53
        $pages = $document->getPages();
54
        $page = $pages[0];
55
56
        // the first to load data.
57
        $fonts = $page->getFonts();
58
        $this->assertTrue(0 < \count($fonts));
59
        foreach ($fonts as $font) {
60
            $this->assertTrue($font instanceof Font);
61
        }
62
        // the second to use cache.
63
        $fonts = $page->getFonts();
64
        $this->assertTrue(0 < \count($fonts));
65
66
        // ------------------------------------------------------
67
        // Document without text.
68
        $filename = $this->rootDir.'/samples/Document3_pdfcreator_nocompressed.pdf';
69
        $document = $parser->parseFile($filename);
70
        $pages = $document->getPages();
71
        $page = $pages[0];
72
73
        // the first to load data.
74
        $fonts = $page->getFonts();
75
        $this->assertEquals(0, \count($fonts));
76
        // the second to use cache.
77
        $fonts = $page->getFonts();
78
        $this->assertEquals(0, \count($fonts));
79
    }
80
81
    public function testGetFontsElementMissing(): void
82
    {
83
        $headerResources = $this->getMockBuilder('Smalot\PdfParser\Header')
84
            ->disableOriginalConstructor()
85
            ->getMock();
86
87
        $headerResources->expects($this->once())
88
            ->method('has')
89
            ->willReturn(true);
90
91
        $headerResources->expects($this->once())
92
            ->method('get')
93
            ->willReturn(new ElementMissing());
94
95
        $header = $this->getMockBuilder('Smalot\PdfParser\Header')
96
            ->disableOriginalConstructor()
97
            ->getMock();
98
99
        $header->expects($this->once())
100
            ->method('get')
101
            ->willReturn($headerResources);
102
103
        $page = new Page(new Document(), $header);
104
        $fonts = $page->getFonts();
105
106
        $this->assertEmpty($fonts);
107
        $this->assertEquals([], $fonts);
108
    }
109
110
    public function testGetFont(): void
111
    {
112
        // Document with text.
113
        $filename = $this->rootDir.'/samples/Document1_pdfcreator_nocompressed.pdf';
114
        $parser = $this->getParserInstance();
115
        $document = $parser->parseFile($filename);
116
        $pages = $document->getPages();
117
        $page = $pages[0];
118
119
        // the first to load data.
120
        $font = $page->getFont('R7');
121
        $this->assertTrue($font instanceof Font);
122
123
        $font = $page->getFont('ABC7');
124
        $this->assertTrue($font instanceof Font);
125
    }
126
127
    public function testGetText(): void
128
    {
129
        // Document with text.
130
        $filename = $this->rootDir.'/samples/Document1_pdfcreator_nocompressed.pdf';
131
        $parser = $this->getParserInstance();
132
        $document = $parser->parseFile($filename);
133
        $pages = $document->getPages();
134
        $page = $pages[0];
135
        $text = $page->getText();
136
137
        $this->assertTrue(150 < \strlen($text));
138
        $this->assertStringContainsString('Document title', $text);
139
        $this->assertStringContainsString('Lorem ipsum', $text);
140
141
        $this->assertStringContainsString('Calibri', $text);
142
        $this->assertStringContainsString('Arial', $text);
143
        $this->assertStringContainsString('Times', $text);
144
        $this->assertStringContainsString('Courier New', $text);
145
        $this->assertStringContainsString('Verdana', $text);
146
    }
147
148
    /**
149
     * @group memory-heavy
150
     *
151
     * @see https://github.com/smalot/pdfparser/pull/457
152
     */
153
    public function testGetTextPullRequest457(): void
154
    {
155
        // Document with text.
156
        $filename = $this->rootDir.'/samples/bugs/PullRequest457.pdf';
157
        $parser = $this->getParserInstance();
158
        $document = $parser->parseFile($filename);
159
        $pages = $document->getPages();
160
        $page = $pages[0];
161
        $text = $page->getText();
162
163
        $this->assertTrue(1000 < \strlen($text));
164
        $this->assertStringContainsString('SUPER', $text);
165
        $this->assertStringContainsString('VOORDEEL', $text);
166
        $this->assertStringContainsString('KRANT', $text);
167
        $this->assertStringContainsString('DINSDAG', $text);
168
        $this->assertStringContainsString('Snelfilterkoffie', $text);
169
        $this->assertStringContainsString('Aardappelen'."\n".'Zak', $text);
170
        $this->assertStringContainsString('ALL', $text);
171
    }
172
173
    public function testExtractRawData(): void
174
    {
175
        // Document with text.
176
        $filename = $this->rootDir.'/samples/Document1_pdfcreator_nocompressed.pdf';
177
        $parser = $this->getParserInstance();
178
        $document = $parser->parseFile($filename);
179
        $pages = $document->getPages();
180
        $page = $pages[0];
181
        $extractedRawData = $page->extractRawData();
182
183
        $btItem = $extractedRawData[4];
184
        $this->assertCount(3, $btItem);
185
        $this->assertArrayHasKey('t', $btItem);
186
        $this->assertArrayHasKey('o', $btItem);
187
        $this->assertArrayHasKey('c', $btItem);
188
189
        $this->assertEquals('BT', $btItem['o']);
190
191
        $tmItem = $extractedRawData[6];
192
193
        $this->assertcount(185, $extractedRawData);
194
        $this->assertCount(3, $tmItem);
195
196
        $this->assertArrayHasKey('t', $tmItem);
197
        $this->assertArrayHasKey('o', $tmItem);
198
        $this->assertArrayHasKey('c', $tmItem);
199
200
        $this->assertStringContainsString('Tm', $tmItem['o']);
201
        $this->assertStringContainsString('0.999429 0 0 1 201.96 720.68', $tmItem['c']);
202
    }
203
204
    public function testExtractDecodedRawData(): void
205
    {
206
        // Document with text.
207
        $filename = $this->rootDir.'/samples/Document1_pdfcreator_nocompressed.pdf';
208
        $parser = $this->getParserInstance();
209
        $document = $parser->parseFile($filename);
210
        $pages = $document->getPages();
211
        $page = $pages[0];
212
        $extractedDecodedRawData = $page->extractDecodedRawData();
213
        $tmItem = $extractedDecodedRawData[6];
214
        $this->assertCount(185, $extractedDecodedRawData);
215
        $this->assertCount(3, $tmItem);
216
217
        $this->assertArrayHasKey('t', $tmItem);
218
        $this->assertArrayHasKey('o', $tmItem);
219
        $this->assertArrayHasKey('c', $tmItem);
220
221
        $this->assertStringContainsString('Tm', $tmItem['o']);
222
        $this->assertStringContainsString('0.999429 0 0 1 201.96 720.68', $tmItem['c']);
223
224
        $this->assertCount(3, $tmItem);
225
        $this->assertArrayHasKey('t', $tmItem);
226
        $this->assertArrayHasKey('o', $tmItem);
227
        $this->assertArrayHasKey('c', $tmItem);
228
229
        $tjItem = $extractedDecodedRawData[7];
230
        $this->assertStringContainsString('TJ', $tjItem['o']);
231
        $this->assertStringContainsString('(', $tjItem['c'][0]['t']);
232
        $this->assertStringContainsString('D', $tjItem['c'][0]['c']);
233
        $this->assertStringContainsString('n', $tjItem['c'][1]['t']);
234
        $this->assertStringContainsString('0.325008', $tjItem['c'][1]['c']);
235
        $this->assertStringContainsString('(', $tjItem['c'][2]['t']);
236
        $this->assertStringContainsString('o', $tjItem['c'][2]['c']);
237
    }
238
239
    public function testExtractRawDataWithCorruptedPdf(): void
240
    {
241
        $this->expectException(\Exception::class);
242
        $this->expectExceptionMessage('Unable to find xref (PDF corrupted?)');
243
244
        $this
245
            ->getParserInstance()
246
            ->parseFile($this->rootDir.'/samples/corrupted.pdf')
247
            ->getPages();
248
    }
249
250
    public function testGetDataCommands(): void
251
    {
252
        // Document with text.
253
        $filename = $this->rootDir.'/samples/Document1_pdfcreator_nocompressed.pdf';
254
        $parser = $this->getParserInstance();
255
        $document = $parser->parseFile($filename);
256
        $pages = $document->getPages();
257
        $page = $pages[0];
258
        $dataCommands = $page->getDataCommands();
259
        $this->assertCount(185, $dataCommands);
260
261
        $tmItem = $dataCommands[6];
262
        $this->assertCount(3, $tmItem);
263
        $this->assertArrayHasKey('t', $tmItem);
264
        $this->assertArrayHasKey('o', $tmItem);
265
        $this->assertArrayHasKey('c', $tmItem);
266
267
        $this->assertStringContainsString('Tm', $tmItem['o']);
268
        $this->assertStringContainsString('0.999429 0 0 1 201.96 720.68', $tmItem['c']);
269
270
        $tjItem = $dataCommands[7];
271
        $this->assertCount(3, $tjItem);
272
        $this->assertArrayHasKey('t', $tjItem);
273
        $this->assertArrayHasKey('o', $tjItem);
274
        $this->assertArrayHasKey('c', $tjItem);
275
276
        $this->assertStringContainsString('TJ', $tjItem['o']);
277
        $this->assertStringContainsString('(', $tjItem['c'][0]['t']);
278
        $this->assertStringContainsString('D', $tjItem['c'][0]['c']);
279
        $this->assertStringContainsString('n', $tjItem['c'][1]['t']);
280
        $this->assertStringContainsString('0.325008', $tjItem['c'][1]['c']);
281
        $this->assertStringContainsString('(', $tjItem['c'][2]['t']);
282
        $this->assertStringContainsString('o', $tjItem['c'][2]['c']);
283
    }
284
285
    public function testGetDataTm(): void
286
    {
287
        // Document with text.
288
        $filename = $this->rootDir.'/samples/Document1_pdfcreator_nocompressed.pdf';
289
        $parser = $this->getParserInstance();
290
        $document = $parser->parseFile($filename);
291
        $pages = $document->getPages();
292
        $page = $pages[0];
293
294
        $dataTm = $page->getDataTm();
295
296
        $this->assertCount(81, $dataTm);
297
298
        $item = $dataTm[0];
299
        $this->assertCount(2, $item);
300
        $this->assertCount(6, $item[0]);
301
        $this->assertEquals(
302
            [
303
                '0.999429',
304
                '0',
305
                '0',
306
                '1',
307
                '201.96',
308
                '720.68',
309
            ],
310
            [
311
                round($item[0][0], 6),
312
                round($item[0][1], 6),
313
                round($item[0][2], 6),
314
                round($item[0][3], 6),
315
                round($item[0][4], 2),
316
                round($item[0][5], 2),
317
            ]
318
        );
319
        $this->assertStringContainsString('Document title', $item[1]);
320
321
        $item = $dataTm[2];
322
        $this->assertEquals(
323
            [
324
                '0.999402',
325
                '0',
326
                '0',
327
                '1',
328
                '70.8',
329
                '673.64',
330
            ],
331
            [
332
                round($item[0][0], 6),
333
                round($item[0][1], 6),
334
                round($item[0][2], 6),
335
                round($item[0][3], 6),
336
                round($item[0][4], 2),
337
                round($item[0][5], 2),
338
            ]
339
        );
340
        $this->assertStringContainsString('Calibri : Lorem ipsum dolor sit amet, consectetur a', $item[1]);
341
342
        $item = $dataTm[80];
343
        $this->assertEquals(
344
            [
345
                '0.999402',
346
                '0',
347
                '0',
348
                '1',
349
                '342.84',
350
                '81.44',
351
            ],
352
            [
353
                round($item[0][0], 6),
354
                round($item[0][1], 6),
355
                round($item[0][2], 6),
356
                round($item[0][3], 6),
357
                round($item[0][4], 2),
358
                round($item[0][5], 2),
359
            ]
360
        );
361
        $this->assertStringContainsString('nenatis.', $item[1]);
362
363
        // ------------------------------------------------------
364
        // Document is a form
365
        $filename = $this->rootDir.'/samples/SimpleInvoiceFilledExample1.pdf';
366
        $document = $parser->parseFile($filename);
367
        $pages = $document->getPages();
368
        $page = $pages[0];
369
        $dataTm = $page->getDataTm();
370
        $item = $dataTm[2];
371
        $this->assertCount(105, $dataTm);
372
        $this->assertCount(2, $item);
373
        $this->assertCount(6, $item[0]);
374
        $this->assertEquals(
375
            [
376
                '1',
377
                '0',
378
                '0',
379
                '1',
380
                '167.3',
381
                '894.58',
382
            ],
383
            $item[0]
384
        );
385
        $this->assertStringContainsString('MyName  MyLastName', $item[1]);
386
387
        $item = $dataTm[6];
388
        $this->assertEquals(
389
            [
390
                '1',
391
                '0',
392
                '0',
393
                '1',
394
                '681.94',
395
                '877.42',
396
            ],
397
            $item[0]
398
        );
399
        $this->assertStringContainsString('1/1/2020', $item[1]);
400
401
        $item = $dataTm[8];
402
        $this->assertEquals(
403
            [
404
                '1',
405
                '0',
406
                '0',
407
                '1',
408
                '174.86',
409
                '827.14',
410
            ],
411
            $item[0]
412
        );
413
        $this->assertStringContainsString('Purchase 1', $item[1]);
414
415
        // ------------------------------------------------------
416
        // Document is another form of the same type
417
        $filename = $this->rootDir.'/samples/SimpleInvoiceFilledExample2.pdf';
418
        $document = $parser->parseFile($filename);
419
        $pages = $document->getPages();
420
        $page = $pages[0];
421
        $dataTm = $page->getDataTm();
422
423
        $item = $dataTm[2];
424
        $this->assertCount(105, $dataTm);
425
        $this->assertCount(2, $item);
426
        $this->assertCount(6, $item[0]);
427
        $this->assertEquals(
428
            [
429
                '1',
430
                '0',
431
                '0',
432
                '1',
433
                '167.3',
434
                '894.58',
435
            ],
436
            $item[0]
437
        );
438
        $this->assertStringContainsString("Other'sName  Other'sLastName", $item[1]);
439
440
        $item = $dataTm[6];
441
        $this->assertEquals(
442
            [
443
                '1',
444
                '0',
445
                '0',
446
                '1',
447
                '681.94',
448
                '877.42',
449
            ],
450
            $item[0]
451
        );
452
        $this->assertStringContainsString('2/2/2020', $item[1]);
453
454
        $item = $dataTm[8];
455
        $this->assertEquals(
456
            [
457
                '1',
458
                '0',
459
                '0',
460
                '1',
461
                '174.86',
462
                '827.14',
463
            ],
464
            $item[0]
465
        );
466
        $this->assertStringContainsString('Purchase 2', $item[1]);
467
468
        // test if scaling by fontSize (Tf, Tfs) and test matrix (Tm) are taken into account
469
        $dataCommands = [
470
            ['t' => '', 'o' => 'BT', 'c' => ''], // begin text
471
            ['t' => '/', 'o' => 'Tf', 'c' => 'TT0 1'], // set font and scale font by 1 pt
472
            ['t' => '', 'o' => 'Tm', 'c' => '7.5 -0 0 8.5 45.36 791.52'], // additionally scale by 7.5 pt
473
            ['t' => '', 'o' => 'Td', 'c' => '0.568 0'], // move 0.568 * 7.5 pts (7.5 is horizontal scaling) to the right
474
            ['t' => '(', 'o' => 'Tj', 'c' => 'test'], // print "test"
475
            ['t' => '', 'o' => 'TD', 'c' => '-3.5 -1.291'], // move 3.5 * 7.5 pts left, 1.291 * 8.5 (vertical scaling) pts down and set text leading to 9.464
476
            ['t' => '(', 'o' => 'Tj', 'c' => 'another test'], // print "another test"
477
            ['t' => '', 'o' => '\'', 'c' => 'again a test'], // go to next line and print "again a test"
478
            ['t' => '', 'o' => 'TL', 'c' => '5'], // set text leading by TL
479
            ['t' => '', 'o' => '\'', 'c' => 'the next line'], // go to next line and print "the next line"
480
        ];
481
482
        // verify scaling is taken into account for Td
483
        $dataTm = $page->getDataTm($dataCommands);
484
        $item = $dataTm[0];
485
        $this->assertEquals(
486
            [
487
                '7.5',
488
                '-0',
489
                '0',
490
                '8.5',
491
                '49.62',
492
                '791.52',
493
            ],
494
            $item[0]
495
        );
496
497
        // verify scaling is taken into account for TD
498
        $item = $dataTm[1];
499
        $this->assertEquals(
500
            [
501
                '7.5',
502
                '-0',
503
                '0',
504
                '8.5',
505
                '23.37',
506
                '780.5465',
507
            ],
508
            $item[0]
509
        );
510
511
        // verify scaling is taken into account for text leading set by TD
512
        $item = $dataTm[2];
513
        $this->assertEquals(
514
            [
515
                '7.5',
516
                '-0',
517
                '0',
518
                '8.5',
519
                '23.37',
520
                '769.573',
521
            ],
522
            $item[0]
523
        );
524
525
        // verify scaling is taken into account for text leading set by TL
526
        $item = $dataTm[3];
527
        $this->assertEquals(
528
            [
529
                '7.5',
530
                '-0',
531
                '0',
532
                '8.5',
533
                '23.37',
534
                '727.073',
535
            ],
536
            $item[0]
537
        );
538
    }
539
540
    public function testDataTmFontInfoHasToBeIncluded(): void
541
    {
542
        $config = new Config();
543
        $config->setDataTmFontInfoHasToBeIncluded(true);
544
545
        $filename = $this->rootDir.'/samples/Document1_pdfcreator_nocompressed.pdf';
546
        $parser = $this->getParserInstance($config);
547
        $document = $parser->parseFile($filename);
548
        $pages = $document->getPages();
549
        $page = $pages[0];
550
        $dataTm = $page->getDataTm();
551
        $fonts = $page->getFonts();
552
553
        $item = $dataTm[0];
554
        $this->assertCount(4, $item);
555
        $this->assertEquals($item[2], 'R7');
556
        $this->assertEquals($item[3], '27.96');
557
        $this->assertArrayHasKey('R7', $fonts);
558
        $item = $dataTm[80];
559
        $this->assertCount(4, $item);
560
        $this->assertEquals($item[2], 'R14');
561
        $this->assertEquals($item[3], '11.04');
562
        $this->assertArrayHasKey('R7', $fonts);
563
564
        $filename = $this->rootDir.'/samples/InternationalChars.pdf';
565
        $document = $parser->parseFile($filename);
566
        $pages = $document->getPages();
567
        $page = $pages[0];
568
        $dataTm = $page->getDataTm();
569
        $fonts = $page->getFonts();
570
571
        $item = $dataTm[88];
572
        $this->assertEquals($item[2], 'C2_0');
573
        $this->assertEquals($item[3], '1');
574
        $this->assertArrayHasKey('C2_0', $fonts);
575
        foreach ($dataTm as $item) {
576
            $this->assertCount(4, $item);
577
        }
578
    }
579
580
    /**
581
     * Tests getDataTm with hexadecimal encoded document text.
582
     *
583
     * @see https://github.com/smalot/pdfparser/issues/336
584
     */
585
    public function testGetDataTmIssue336(): void
586
    {
587
        $filename = $this->rootDir.'/samples/bugs/Issue336_decode_hexadecimal.pdf';
588
        $document = $this->getParserInstance()->parseFile($filename);
589
        $pages = $document->getPages();
590
        $page = $pages[0];
591
        $dataTm = $page->getDataTm();
592
593
        $item = $dataTm[2];
594
        $this->assertCount(13, $dataTm);
595
        $this->assertCount(2, $item);
596
        $this->assertCount(6, $item[0]);
597
        $this->assertEquals(
598
            [
599
                '1',
600
                '0',
601
                '0',
602
                '1',
603
                '318.185',
604
                '665.044',
605
            ],
606
            $item[0]
607
        );
608
        $this->assertEquals('Lorem', $item[1]);
609
    }
610
611
    /**
612
     * Tests that getPages() only returns Page objects
613
     *
614
     * @see https://github.com/smalot/pdfparser/issues/331
615
     *
616
     * Sample pdf file provided by @Reqrefusion, see
617
     * https://github.com/smalot/pdfparser/pull/350#issuecomment-703195220
618
     */
619
    public function testGetPages(): void
620
    {
621
        $filename = $this->rootDir.'/samples/bugs/Issue331.pdf';
622
        $document = $this->getParserInstance()->parseFile($filename);
623
        $pages = $document->getPages();
624
625
        /*
626
         * The problem of issue #331 is fixed by the pull request of the issue #479.
627
         * The original Issue331.pdf was modified so for the updated version (actual
628
         * version) a new xref was added and now the valid /Index has the following value:
629
         *    [1 1 3 1 7 1 175 1 178 1 219 2]
630
         * This means, that there a 6 pairs containing the values for 'first object id'
631
         * and 'number of objects'. Till now only the first entry was used and so the
632
         * objects of all following entries gots a wrong id.
633
         * By the fix of issue #479 now the expected number of pages is counted.
634
         */
635
        $this->assertCount(3, $pages);
636
637
        foreach ($pages as $page) {
638
            $this->assertTrue($page instanceof Page);
639
        }
640
    }
641
642
    public function testGetTextXY(): void
643
    {
644
        // Document with text.
645
        $filename = $this->rootDir.'/samples/Document1_pdfcreator_nocompressed.pdf';
646
        $parser = $this->getParserInstance();
647
        $document = $parser->parseFile($filename);
648
        $pages = $document->getPages();
649
        $page = $pages[0];
650
        $result = $page->getTextXY(201.96, 720.68, 0.01, 0.01);
651
        $this->assertCount(1, $result);
652
        $this->assertCount(2, $result[0]);
653
        $this->assertEquals(
654
            [
655
                '0.999429',
656
                '0',
657
                '0',
658
                '1',
659
                '201.96',
660
                '720.68',
661
            ],
662
            [
663
                round($result[0][0][0], 6),
664
                round($result[0][0][1], 6),
665
                round($result[0][0][2], 6),
666
                round($result[0][0][3], 6),
667
                round($result[0][0][4], 2),
668
                round($result[0][0][5], 2),
669
            ]
670
        );
671
        $this->assertStringContainsString('Document title', $result[0][1]);
672
673
        $result = $page->getTextXY(201, 720);
674
        $this->assertCount(0, $result);
675
676
        $result = $page->getTextXY(201, 720, 1, 1);
677
        $this->assertCount(1, $result);
678
        $this->assertCount(2, $result[0]);
679
        $this->assertEquals(
680
            [
681
                '0.999429',
682
                '0',
683
                '0',
684
                '1',
685
                '201.96',
686
                '720.68',
687
            ],
688
            [
689
                round($result[0][0][0], 6),
690
                round($result[0][0][1], 6),
691
                round($result[0][0][2], 6),
692
                round($result[0][0][3], 6),
693
                round($result[0][0][4], 2),
694
                round($result[0][0][5], 2),
695
            ]
696
        );
697
        $this->assertStringContainsString('Document title', $result[0][1]);
698
699
        // ------------------------------------------------------
700
        // Document is a form
701
        $filename = $this->rootDir.'/samples/SimpleInvoiceFilledExample1.pdf';
702
        $document = $parser->parseFile($filename);
703
        $pages = $document->getPages();
704
        $page = $pages[0];
705
        $result = $page->getTextXY(167, 894, 1, 1);
706
        $this->assertCount(1, $result);
707
        $this->assertCount(2, $result[0]);
708
        $this->assertEquals(
709
            [
710
                '1',
711
                '0',
712
                '0',
713
                '1',
714
                '167.3',
715
                '894.58',
716
            ],
717
            $result[0][0]
718
        );
719
        $this->assertStringContainsString('MyName  MyLastName', $result[0][1]);
720
721
        $result = $page->getTextXY(681, 877, 1, 1);
722
        $this->assertStringContainsString('1/1/2020', $result[0][1]);
723
724
        $result = $page->getTextXY(174, 827, 1, 1);
725
        $this->assertStringContainsString('Purchase 1', $result[0][1]);
726
727
        // ------------------------------------------------------
728
        // Document is another form of the same type
729
        $filename = $this->rootDir.'/samples/SimpleInvoiceFilledExample2.pdf';
730
        $document = $parser->parseFile($filename);
731
        $pages = $document->getPages();
732
        $page = $pages[0];
733
        $result = $page->getTextXY(167, 894, 1, 1);
734
        $this->assertEquals(
735
            [
736
                '1',
737
                '0',
738
                '0',
739
                '1',
740
                '167.3',
741
                '894.58',
742
            ],
743
            $result[0][0]
744
        );
745
        $this->assertStringContainsString("Other'sName  Other'sLastName", $result[0][1]);
746
747
        $result = $page->getTextXY(681, 877, 1, 1);
748
        $this->assertStringContainsString('2/2/2020', $result[0][1]);
749
750
        $result = $page->getTextXY(174, 827, 1, 1);
751
        $this->assertStringContainsString('Purchase 2', $result[0][1]);
752
    }
753
754
    public function testExtractDecodedRawDataIssue450(): void
755
    {
756
        $filename = $this->rootDir.'/samples/bugs/Issue450.pdf';
757
        $parser = $this->getParserInstance();
758
        $document = $parser->parseFile($filename);
759
        $pages = $document->getPages();
760
        $page = $pages[0];
761
        $extractedDecodedRawData = $page->extractDecodedRawData();
762
        $this->assertIsArray($extractedDecodedRawData);
763
        $this->assertGreaterThan(3, \count($extractedDecodedRawData));
764
        $this->assertIsArray($extractedDecodedRawData[3]);
765
        $this->assertEquals('TJ', $extractedDecodedRawData[3]['o']);
766
        $this->assertIsArray($extractedDecodedRawData[3]['c']);
767
        $this->assertIsArray($extractedDecodedRawData[3]['c'][0]);
768
        $this->assertEquals(3, \count($extractedDecodedRawData[3]['c'][0]));
769
        $this->assertEquals('{signature:signer505906:Please+Sign+Here}', $extractedDecodedRawData[3]['c'][0]['c']);
770
    }
771
772
    public function testGetDataTmIssue450(): void
773
    {
774
        $filename = $this->rootDir.'/samples/bugs/Issue450.pdf';
775
        $parser = $this->getParserInstance();
776
        $document = $parser->parseFile($filename);
777
        $pages = $document->getPages();
778
        $page = $pages[0];
779
        $dataTm = $page->getDataTm();
780
        $this->assertIsArray($dataTm);
781
        $this->assertEquals(1, \count($dataTm));
782
        $this->assertIsArray($dataTm[0]);
783
        $this->assertEquals(2, \count($dataTm[0]));
784
        $this->assertIsArray($dataTm[0][0]);
785
        $this->assertEquals(6, \count($dataTm[0][0]));
786
        $this->assertEquals(1, $dataTm[0][0][0]);
787
        $this->assertEquals(0, $dataTm[0][0][1]);
788
        $this->assertEquals(0, $dataTm[0][0][2]);
789
        $this->assertEquals(1, $dataTm[0][0][3]);
790
        $this->assertEquals(67.5, $dataTm[0][0][4]);
791
        $this->assertEquals(756.25, $dataTm[0][0][5]);
792
        $this->assertEquals('{signature:signer505906:Please+Sign+Here}', $dataTm[0][1]);
793
    }
794
795
    public function testIsFpdf(): void
796
    {
797
        $filename = $this->rootDir.'/samples/Document1_foxitreader.pdf';
798
        $parser = $this->getParserInstance();
799
        $document = $parser->parseFile($filename);
800
        $pages = $document->getPages();
801
        $page = $pages[0];
802
        $this->assertFalse($page->isFpdf());
803
        $filename = $this->rootDir.'/samples/bugs/Issue454.pdf';
804
        $document = $parser->parseFile($filename);
805
        $pages = $document->getPages();
806
        $page = $pages[0];
807
        $this->assertTrue($page->isFpdf());
808
    }
809
810
    public function testGetPageNumber(): void
811
    {
812
        $filename = $this->rootDir.'/samples/Document1_foxitreader.pdf';
813
        $parser = $this->getParserInstance();
814
        $document = $parser->parseFile($filename);
815
        $pages = $document->getPages();
816
        $page = $pages[0];
817
        $this->assertEquals(0, $page->getPageNumber());
818
        $filename = $this->rootDir.'/samples/Document1_pdfcreator.pdf';
819
        $document = $parser->parseFile($filename);
820
        $pages = $document->getPages();
821
        $page = $pages[0];
822
        $this->assertEquals(0, $page->getPageNumber());
823
        $filename = $this->rootDir.'/samples/Document2_pdfcreator_nocompressed.pdf';
824
        $document = $parser->parseFile($filename);
825
        $pages = $document->getPages();
826
        $page = $pages[0];
827
        $this->assertEquals(0, $page->getPageNumber());
828
        $filename = $this->rootDir.'/samples/InternationalChars.pdf';
829
        $document = $parser->parseFile($filename);
830
        $pages = $document->getPages();
831
        $page = $pages[0];
832
        $this->assertEquals(0, $page->getPageNumber());
833
        $filename = $this->rootDir.'/samples/SimpleInvoiceFilledExample1.pdf';
834
        $document = $parser->parseFile($filename);
835
        $pages = $document->getPages();
836
        $page = $pages[0];
837
        $this->assertEquals(0, $page->getPageNumber());
838
        $filename = $this->rootDir.'/samples/bugs/Issue454.pdf';
839
        $document = $parser->parseFile($filename);
840
        $pages = $document->getPages();
841
        $page = $pages[0];
842
        $this->assertEquals(0, $page->getPageNumber());
843
        $page = $pages[1];
844
        $this->assertEquals(1, $page->getPageNumber());
845
        $page = $pages[2];
846
        $this->assertEquals(2, $page->getPageNumber());
847
        $page = $pages[3];
848
        $this->assertEquals(3, $page->getPageNumber());
849
    }
850
851
    public function testIssue454(): void
852
    {
853
        $filename = $this->rootDir.'/samples/bugs/Issue454.pdf';
854
        $parser = $this->getParserInstance();
855
        $document = $parser->parseFile($filename);
856
        $pages = $document->getPages();
857
        $page = $pages[0];
858
        $dataTm = $page->getDataTm();
859
        $this->assertIsArray($dataTm);
860
        $this->assertGreaterThan(0, \count($dataTm));
861
        $this->assertIsArray($dataTm[0]);
862
        $this->assertEquals(2, \count($dataTm[0]));
863
        $this->assertIsArray($dataTm[0][0]);
864
        $this->assertEquals(6, \count($dataTm[0][0]));
865
        $this->assertEquals(201.96, round($dataTm[0][0][4], 2));
866
        $this->assertEquals(720.68, round($dataTm[0][0][5], 2));
867
        $this->assertStringContainsString('Document title', $dataTm[0][1]);
868
        $textData = $page->getTextXY(201.96, 720.68, 0.01, 0.01);
869
        $this->assertStringContainsString('Document title', $textData[0][1]);
870
        $page = $pages[2];
871
        $dataTm = $page->getDataTm();
872
        $this->assertIsArray($dataTm);
873
        $this->assertGreaterThan(0, \count($dataTm));
874
        $this->assertIsArray($dataTm[0]);
875
        $this->assertEquals(2, \count($dataTm[0]));
876
        $this->assertIsArray($dataTm[0][0]);
877
        $this->assertEquals(6, \count($dataTm[0][0]));
878
        $this->assertEquals(67.5, $dataTm[0][0][4]);
879
        $this->assertEquals(756.25, $dataTm[0][0][5]);
880
        $this->assertStringContainsString('{signature:signer505906:Please+Sign+Here}', $dataTm[0][1]);
881
        $textData = $page->getTextXY(67.5, 756.25);
882
        $this->assertStringContainsString('{signature:signer505906:Please+Sign+Here}', $textData[0][1]);
883
    }
884
885
    /**
886
     * Check that BT and ET do not reset the font.
887
     *
888
     * Data TM font info is included.
889
     *
890
     * @see https://github.com/smalot/pdfparser/pull/630
891
     */
892
    public function testIssue629WithDataTmFontInfo(): void
893
    {
894
        $config = new Config();
895
        $config->setDataTmFontInfoHasToBeIncluded(true);
896
897
        $filename = $this->rootDir.'/samples/bugs/Issue629.pdf';
898
        $parser = $this->getParserInstance($config);
899
        $document = $parser->parseFile($filename);
900
        $pages = $document->getPages();
901
        $page = end($pages);
902
        $dataTm = $page->getDataTm();
903
904
        $this->assertCount(4, $dataTm[0]);
905
        $this->assertEquals('F2', $dataTm[0][2]);
906
    }
907
908
    /**
909
     * Data TM font info is NOT included.
910
     *
911
     * @see https://github.com/smalot/pdfparser/pull/630
912
     */
913
    public function testIssue629WithoutDataTmFontInfo(): void
914
    {
915
        $config = new Config();
916
917
        $filename = $this->rootDir.'/samples/bugs/Issue629.pdf';
918
        $parser = $this->getParserInstance($config);
919
        $document = $parser->parseFile($filename);
920
        $pages = $document->getPages();
921
        $page = end($pages);
922
        $dataTm = $page->getDataTm();
923
924
        $this->assertCount(2, $dataTm[0]);
925
        $this->assertFalse(isset($dataTm[0][2]));
926
    }
927
928
    public function testCmCommandInPdfs(): void
929
    {
930
        $config = new Config();
931
        $parser = $this->getParserInstance($config);
932
        $filename = $this->rootDir.'/samples/Document-Word-Landscape-printedaspdf.pdf';
933
        $document = $parser->parseFile($filename);
934
        $pages = $document->getPages();
935
        $page = $pages[0];
936
        $dataTm = $page->getDataTm();
937
        $item = $dataTm[2];
938
        $this->assertCount(6, $dataTm);
939
        $this->assertCount(2, $item);
940
        $this->assertCount(6, $item[0]);
941
        $this->assertEquals('This is just a test', trim($item[1]));
942
        $this->assertEquals(
943
            [
944
                '0.75',
945
                '0.0',
946
                '0.0',
947
                '0.75',
948
                '59.16',
949
                '500.4',
950
            ],
951
            [
952
                round($item[0][0], 6),
953
                round($item[0][1], 6),
954
                round($item[0][2], 6),
955
                round($item[0][3], 6),
956
                round($item[0][4], 2),
957
                round($item[0][5], 2),
958
            ]
959
        );
960
    }
961
}
962