Test Failed
Push — fix/failing-tests ( 0c7645 )
by Konrad
03:03
created

PageTest::testGetFonts()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 19
nc 2
nop 0
dl 0
loc 32
rs 9.6333
c 0
b 0
f 0
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('AardappelenZak', $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[0];
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[2];
192
193
        $this->assertcount(174, $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[2];
214
        $this->assertCount(174, $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[3];
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(174, $dataCommands);
260
261
        $tmItem = $dataCommands[2];
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[3];
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
            $item[0]
311
        );
312
        $this->assertStringContainsString('Document title', $item[1]);
313
314
        $item = $dataTm[2];
315
        $this->assertEquals(
316
            [
317
                '0.999402',
318
                '0',
319
                '0',
320
                '1',
321
                '70.8',
322
                '673.64',
323
            ],
324
            $item[0]
325
        );
326
        $this->assertStringContainsString('Calibri : Lorem ipsum dolor sit amet, consectetur a', $item[1]);
327
328
        $item = $dataTm[80];
329
        $this->assertEquals(
330
            [
331
                '0.999402',
332
                '0',
333
                '0',
334
                '1',
335
                '342.840222606',
336
                '81.44',
337
            ],
338
            $item[0]
339
        );
340
        $this->assertStringContainsString('nenatis.', $item[1]);
341
342
        // ------------------------------------------------------
343
        // Document is a form
344
        $filename = $this->rootDir.'/samples/SimpleInvoiceFilledExample1.pdf';
345
        $document = $parser->parseFile($filename);
346
        $pages = $document->getPages();
347
        $page = $pages[0];
348
        $dataTm = $page->getDataTm();
349
        $item = $dataTm[2];
350
        $this->assertCount(105, $dataTm);
351
        $this->assertCount(2, $item);
352
        $this->assertCount(6, $item[0]);
353
        $this->assertEquals(
354
            [
355
                '1',
356
                '0',
357
                '0',
358
                '1',
359
                '167.3',
360
                '894.58',
361
            ],
362
            $item[0]
363
        );
364
        $this->assertStringContainsString('MyName  MyLastName', $item[1]);
365
366
        $item = $dataTm[6];
367
        $this->assertEquals(
368
            [
369
                '1',
370
                '0',
371
                '0',
372
                '1',
373
                '681.94',
374
                '877.42',
375
            ],
376
            $item[0]
377
        );
378
        $this->assertStringContainsString('1/1/2020', $item[1]);
379
380
        $item = $dataTm[8];
381
        $this->assertEquals(
382
            [
383
                '1',
384
                '0',
385
                '0',
386
                '1',
387
                '174.86',
388
                '827.14',
389
            ],
390
            $item[0]
391
        );
392
        $this->assertStringContainsString('Purchase 1', $item[1]);
393
394
        // ------------------------------------------------------
395
        // Document is another form of the same type
396
        $filename = $this->rootDir.'/samples/SimpleInvoiceFilledExample2.pdf';
397
        $document = $parser->parseFile($filename);
398
        $pages = $document->getPages();
399
        $page = $pages[0];
400
        $dataTm = $page->getDataTm();
401
402
        $item = $dataTm[2];
403
        $this->assertCount(105, $dataTm);
404
        $this->assertCount(2, $item);
405
        $this->assertCount(6, $item[0]);
406
        $this->assertEquals(
407
            [
408
                '1',
409
                '0',
410
                '0',
411
                '1',
412
                '167.3',
413
                '894.58',
414
            ],
415
            $item[0]
416
        );
417
        $this->assertStringContainsString("Other'sName  Other'sLastName", $item[1]);
418
419
        $item = $dataTm[6];
420
        $this->assertEquals(
421
            [
422
                '1',
423
                '0',
424
                '0',
425
                '1',
426
                '681.94',
427
                '877.42',
428
            ],
429
            $item[0]
430
        );
431
        $this->assertStringContainsString('2/2/2020', $item[1]);
432
433
        $item = $dataTm[8];
434
        $this->assertEquals(
435
            [
436
                '1',
437
                '0',
438
                '0',
439
                '1',
440
                '174.86',
441
                '827.14',
442
            ],
443
            $item[0]
444
        );
445
        $this->assertStringContainsString('Purchase 2', $item[1]);
446
447
        // test if scaling by fontSize (Tf, Tfs) and test matrix (Tm) are taken into account
448
        $dataCommands = [
449
            ['t' => '', 'o' => 'BT', 'c' => ''], // begin text
450
            ['t' => '/', 'o' => 'Tf', 'c' => 'TT0 1'], // set font and scale font by 1 pt
451
            ['t' => '', 'o' => 'Tm', 'c' => '7.5 -0 0 8.5 45.36 791.52'], // additionally scale by 7.5 pt
452
            ['t' => '', 'o' => 'Td', 'c' => '0.568 0'], // move 0.568 * 7.5 pts (7.5 is horizontal scaling) to the right
453
            ['t' => '(', 'o' => 'Tj', 'c' => 'test'], // print "test"
454
            ['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
455
            ['t' => '(', 'o' => 'Tj', 'c' => 'another test'], // print "another test"
456
            ['t' => '', 'o' => '\'', 'c' => 'again a test'], // go to next line and print "again a test"
457
            ['t' => '', 'o' => 'TL', 'c' => '5'], // set text leading by TL
458
            ['t' => '', 'o' => '\'', 'c' => 'the next line'], // go to next line and print "the next line"
459
        ];
460
461
        // verify scaling is taken into account for Td
462
        $dataTm = $page->getDataTm($dataCommands);
463
        $item = $dataTm[0];
464
        $this->assertEquals(
465
            [
466
                '7.5',
467
                '-0',
468
                '0',
469
                '8.5',
470
                '49.62',
471
                '791.52',
472
            ],
473
            $item[0]
474
        );
475
476
        // verify scaling is taken into account for TD
477
        $item = $dataTm[1];
478
        $this->assertEquals(
479
            [
480
                '7.5',
481
                '-0',
482
                '0',
483
                '8.5',
484
                '23.37',
485
                '780.5465',
486
            ],
487
            $item[0]
488
        );
489
490
        // verify scaling is taken into account for text leading set by TD
491
        $item = $dataTm[2];
492
        $this->assertEquals(
493
            [
494
                '7.5',
495
                '-0',
496
                '0',
497
                '8.5',
498
                '23.37',
499
                '769.573',
500
            ],
501
            $item[0]
502
        );
503
504
        // verify scaling is taken into account for text leading set by TL
505
        $item = $dataTm[3];
506
        $this->assertEquals(
507
            [
508
                '7.5',
509
                '-0',
510
                '0',
511
                '8.5',
512
                '23.37',
513
                '727.073',
514
            ],
515
            $item[0]
516
        );
517
    }
518
519
    public function testDataTmFontInfoHasToBeIncluded(): void
520
    {
521
        $config = new Config();
522
        $config->setDataTmFontInfoHasToBeIncluded(true);
523
524
        $filename = $this->rootDir.'/samples/Document1_pdfcreator_nocompressed.pdf';
525
        $parser = $this->getParserInstance($config);
526
        $document = $parser->parseFile($filename);
527
        $pages = $document->getPages();
528
        $page = $pages[0];
529
        $dataTm = $page->getDataTm();
530
        $fonts = $page->getFonts();
531
532
        $item = $dataTm[0];
533
        $this->assertCount(4, $item);
534
        $this->assertEquals($item[2], 'R7');
535
        $this->assertEquals($item[3], '27.96');
536
        $this->assertArrayHasKey('R7', $fonts);
537
        $item = $dataTm[80];
538
        $this->assertCount(4, $item);
539
        $this->assertEquals($item[2], 'R14');
540
        $this->assertEquals($item[3], '11.04');
541
        $this->assertArrayHasKey('R7', $fonts);
542
543
        $filename = $this->rootDir.'/samples/InternationalChars.pdf';
544
        $document = $parser->parseFile($filename);
545
        $pages = $document->getPages();
546
        $page = $pages[0];
547
        $dataTm = $page->getDataTm();
548
        $fonts = $page->getFonts();
549
550
        $item = $dataTm[88];
551
        $this->assertEquals($item[2], 'C2_0');
552
        $this->assertEquals($item[3], '1');
553
        $this->assertArrayHasKey('C2_0', $fonts);
554
        foreach ($dataTm as $item) {
555
            $this->assertCount(4, $item);
556
        }
557
    }
558
559
    /**
560
     * Tests getDataTm with hexadecimal encoded document text.
561
     *
562
     * @see https://github.com/smalot/pdfparser/issues/336
563
     */
564
    public function testGetDataTmIssue336(): void
565
    {
566
        $filename = $this->rootDir.'/samples/bugs/Issue336_decode_hexadecimal.pdf';
567
        $document = $this->getParserInstance()->parseFile($filename);
568
        $pages = $document->getPages();
569
        $page = $pages[0];
570
        $dataTm = $page->getDataTm();
571
572
        $item = $dataTm[2];
573
        $this->assertCount(13, $dataTm);
574
        $this->assertCount(2, $item);
575
        $this->assertCount(6, $item[0]);
576
        $this->assertEquals(
577
            [
578
                '1',
579
                '0',
580
                '0',
581
                '1',
582
                '318.185',
583
                '665.044',
584
            ],
585
            $item[0]
586
        );
587
        $this->assertEquals('Lorem', $item[1]);
588
    }
589
590
    /**
591
     * Tests that getPages() only returns Page objects
592
     *
593
     * @see https://github.com/smalot/pdfparser/issues/331
594
     *
595
     * Sample pdf file provided by @Reqrefusion, see
596
     * https://github.com/smalot/pdfparser/pull/350#issuecomment-703195220
597
     */
598
    public function testGetPages(): void
599
    {
600
        $filename = $this->rootDir.'/samples/bugs/Issue331.pdf';
601
        $document = $this->getParserInstance()->parseFile($filename);
602
        $pages = $document->getPages();
603
604
        /*
605
         * The problem of issue #331 is fixed by the pull request of the issue #479.
606
         * The original Issue331.pdf was modified so for the updated version (actual
607
         * version) a new xref was added and now the valid /Index has the following value:
608
         *    [1 1 3 1 7 1 175 1 178 1 219 2]
609
         * This means, that there a 6 pairs containing the values for 'first object id'
610
         * and 'number of objects'. Till now only the first entry was used and so the
611
         * objects of all following entries gots a wrong id.
612
         * By the fix of issue #479 now the expected number of pages is counted.
613
         */
614
        $this->assertCount(3, $pages);
615
616
        foreach ($pages as $page) {
617
            $this->assertTrue($page instanceof Page);
618
        }
619
    }
620
621
    public function testGetTextXY(): void
622
    {
623
        // Document with text.
624
        $filename = $this->rootDir.'/samples/Document1_pdfcreator_nocompressed.pdf';
625
        $parser = $this->getParserInstance();
626
        $document = $parser->parseFile($filename);
627
        $pages = $document->getPages();
628
        $page = $pages[0];
629
        $result = $page->getTextXY(201.96, 720.68);
630
        $this->assertCount(1, $result);
631
        $this->assertCount(2, $result[0]);
632
        $this->assertEquals(
633
            [
634
                '0.999429',
635
                '0',
636
                '0',
637
                '1',
638
                '201.96',
639
                '720.68',
640
            ],
641
            $result[0][0]
642
        );
643
        $this->assertStringContainsString('Document title', $result[0][1]);
644
645
        $result = $page->getTextXY(201, 720);
646
        $this->assertCount(0, $result);
647
648
        $result = $page->getTextXY(201, 720, 1, 1);
649
        $this->assertCount(1, $result);
650
        $this->assertCount(2, $result[0]);
651
        $this->assertEquals(
652
            [
653
                '0.999429',
654
                '0',
655
                '0',
656
                '1',
657
                '201.96',
658
                '720.68',
659
            ],
660
            $result[0][0]
661
        );
662
        $this->assertStringContainsString('Document title', $result[0][1]);
663
664
        // ------------------------------------------------------
665
        // Document is a form
666
        $filename = $this->rootDir.'/samples/SimpleInvoiceFilledExample1.pdf';
667
        $document = $parser->parseFile($filename);
668
        $pages = $document->getPages();
669
        $page = $pages[0];
670
        $result = $page->getTextXY(167, 894, 1, 1);
671
        $this->assertCount(1, $result);
672
        $this->assertCount(2, $result[0]);
673
        $this->assertEquals(
674
            [
675
                '1',
676
                '0',
677
                '0',
678
                '1',
679
                '167.3',
680
                '894.58',
681
            ],
682
            $result[0][0]
683
        );
684
        $this->assertStringContainsString('MyName  MyLastName', $result[0][1]);
685
686
        $result = $page->getTextXY(681, 877, 1, 1);
687
        $this->assertStringContainsString('1/1/2020', $result[0][1]);
688
689
        $result = $page->getTextXY(174, 827, 1, 1);
690
        $this->assertStringContainsString('Purchase 1', $result[0][1]);
691
692
        // ------------------------------------------------------
693
        // Document is another form of the same type
694
        $filename = $this->rootDir.'/samples/SimpleInvoiceFilledExample2.pdf';
695
        $document = $parser->parseFile($filename);
696
        $pages = $document->getPages();
697
        $page = $pages[0];
698
        $result = $page->getTextXY(167, 894, 1, 1);
699
        $this->assertEquals(
700
            [
701
                '1',
702
                '0',
703
                '0',
704
                '1',
705
                '167.3',
706
                '894.58',
707
            ],
708
            $result[0][0]
709
        );
710
        $this->assertStringContainsString("Other'sName  Other'sLastName", $result[0][1]);
711
712
        $result = $page->getTextXY(681, 877, 1, 1);
713
        $this->assertStringContainsString('2/2/2020', $result[0][1]);
714
715
        $result = $page->getTextXY(174, 827, 1, 1);
716
        $this->assertStringContainsString('Purchase 2', $result[0][1]);
717
    }
718
719
    public function testExtractDecodedRawDataIssue450(): void
720
    {
721
        $filename = $this->rootDir.'/samples/bugs/Issue450.pdf';
722
        $parser = $this->getParserInstance();
723
        $document = $parser->parseFile($filename);
724
        $pages = $document->getPages();
725
        $page = $pages[0];
726
        $extractedDecodedRawData = $page->extractDecodedRawData();
727
        $this->assertIsArray($extractedDecodedRawData);
728
        $this->assertGreaterThan(3, \count($extractedDecodedRawData));
729
        $this->assertIsArray($extractedDecodedRawData[3]);
730
        $this->assertEquals('TJ', $extractedDecodedRawData[3]['o']);
731
        $this->assertIsArray($extractedDecodedRawData[3]['c']);
732
        $this->assertIsArray($extractedDecodedRawData[3]['c'][0]);
733
        $this->assertEquals(3, \count($extractedDecodedRawData[3]['c'][0]));
734
        $this->assertEquals('{signature:signer505906:Please+Sign+Here}', $extractedDecodedRawData[3]['c'][0]['c']);
735
    }
736
737
    public function testGetDataTmIssue450(): void
738
    {
739
        $filename = $this->rootDir.'/samples/bugs/Issue450.pdf';
740
        $parser = $this->getParserInstance();
741
        $document = $parser->parseFile($filename);
742
        $pages = $document->getPages();
743
        $page = $pages[0];
744
        $dataTm = $page->getDataTm();
745
        $this->assertIsArray($dataTm);
746
        $this->assertEquals(1, \count($dataTm));
747
        $this->assertIsArray($dataTm[0]);
748
        $this->assertEquals(2, \count($dataTm[0]));
749
        $this->assertIsArray($dataTm[0][0]);
750
        $this->assertEquals(6, \count($dataTm[0][0]));
751
        $this->assertEquals(1, $dataTm[0][0][0]);
752
        $this->assertEquals(0, $dataTm[0][0][1]);
753
        $this->assertEquals(0, $dataTm[0][0][2]);
754
        $this->assertEquals(1, $dataTm[0][0][3]);
755
        $this->assertEquals(67.5, $dataTm[0][0][4]);
756
        $this->assertEquals(756.25, $dataTm[0][0][5]);
757
        $this->assertEquals('{signature:signer505906:Please+Sign+Here}', $dataTm[0][1]);
758
    }
759
760
    public function testIsFpdf(): void
761
    {
762
        $filename = $this->rootDir.'/samples/Document1_foxitreader.pdf';
763
        $parser = $this->getParserInstance();
764
        $document = $parser->parseFile($filename);
765
        $pages = $document->getPages();
766
        $page = $pages[0];
767
        $this->assertFalse($page->isFpdf());
768
        $filename = $this->rootDir.'/samples/bugs/Issue454.pdf';
769
        $document = $parser->parseFile($filename);
770
        $pages = $document->getPages();
771
        $page = $pages[0];
772
        $this->assertTrue($page->isFpdf());
773
    }
774
775
    public function testGetPageNumber(): void
776
    {
777
        $filename = $this->rootDir.'/samples/Document1_foxitreader.pdf';
778
        $parser = $this->getParserInstance();
779
        $document = $parser->parseFile($filename);
780
        $pages = $document->getPages();
781
        $page = $pages[0];
782
        $this->assertEquals(0, $page->getPageNumber());
783
        $filename = $this->rootDir.'/samples/Document1_pdfcreator.pdf';
784
        $document = $parser->parseFile($filename);
785
        $pages = $document->getPages();
786
        $page = $pages[0];
787
        $this->assertEquals(0, $page->getPageNumber());
788
        $filename = $this->rootDir.'/samples/Document2_pdfcreator_nocompressed.pdf';
789
        $document = $parser->parseFile($filename);
790
        $pages = $document->getPages();
791
        $page = $pages[0];
792
        $this->assertEquals(0, $page->getPageNumber());
793
        $filename = $this->rootDir.'/samples/InternationalChars.pdf';
794
        $document = $parser->parseFile($filename);
795
        $pages = $document->getPages();
796
        $page = $pages[0];
797
        $this->assertEquals(0, $page->getPageNumber());
798
        $filename = $this->rootDir.'/samples/SimpleInvoiceFilledExample1.pdf';
799
        $document = $parser->parseFile($filename);
800
        $pages = $document->getPages();
801
        $page = $pages[0];
802
        $this->assertEquals(0, $page->getPageNumber());
803
        $filename = $this->rootDir.'/samples/bugs/Issue454.pdf';
804
        $document = $parser->parseFile($filename);
805
        $pages = $document->getPages();
806
        $page = $pages[0];
807
        $this->assertEquals(0, $page->getPageNumber());
808
        $page = $pages[1];
809
        $this->assertEquals(1, $page->getPageNumber());
810
        $page = $pages[2];
811
        $this->assertEquals(2, $page->getPageNumber());
812
        $page = $pages[3];
813
        $this->assertEquals(3, $page->getPageNumber());
814
    }
815
816
    public function testIssue454(): void
817
    {
818
        $filename = $this->rootDir.'/samples/bugs/Issue454.pdf';
819
        $parser = $this->getParserInstance();
820
        $document = $parser->parseFile($filename);
821
        $pages = $document->getPages();
822
        $page = $pages[0];
823
        $dataTm = $page->getDataTm();
824
        $this->assertIsArray($dataTm);
825
        $this->assertGreaterThan(0, \count($dataTm));
826
        $this->assertIsArray($dataTm[0]);
827
        $this->assertEquals(2, \count($dataTm[0]));
828
        $this->assertIsArray($dataTm[0][0]);
829
        $this->assertEquals(6, \count($dataTm[0][0]));
830
        $this->assertEquals(201.96, $dataTm[0][0][4]);
831
        $this->assertEquals(720.68, $dataTm[0][0][5]);
832
        $this->assertStringContainsString('Document title', $dataTm[0][1]);
833
        $textData = $page->getTextXY(201.96, 720.68);
834
        $this->assertStringContainsString('Document title', $textData[0][1]);
835
        $page = $pages[2];
836
        $dataTm = $page->getDataTm();
837
        $this->assertIsArray($dataTm);
838
        $this->assertGreaterThan(0, \count($dataTm));
839
        $this->assertIsArray($dataTm[0]);
840
        $this->assertEquals(2, \count($dataTm[0]));
841
        $this->assertIsArray($dataTm[0][0]);
842
        $this->assertEquals(6, \count($dataTm[0][0]));
843
        $this->assertEquals(67.5, $dataTm[0][0][4]);
844
        $this->assertEquals(756.25, $dataTm[0][0][5]);
845
        $this->assertStringContainsString('{signature:signer505906:Please+Sign+Here}', $dataTm[0][1]);
846
        $textData = $page->getTextXY(67.5, 756.25);
847
        $this->assertStringContainsString('{signature:signer505906:Please+Sign+Here}', $textData[0][1]);
848
    }
849
}
850