|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @file |
|
5
|
|
|
* This file is part of the PdfParser library. |
|
6
|
|
|
* |
|
7
|
|
|
* @author Sébastien MALOT <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* @date 2017-01-03 |
|
10
|
|
|
* |
|
11
|
|
|
* @license LGPLv3 |
|
12
|
|
|
* |
|
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 Smalot\PdfParser; |
|
34
|
|
|
|
|
35
|
|
|
use Smalot\PdfParser\Element\ElementArray; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Class Pages |
|
39
|
|
|
*/ |
|
40
|
|
|
class Pages extends PDFObject |
|
41
|
|
|
{ |
|
42
|
|
|
/** |
|
43
|
|
|
* @var array<\Smalot\PdfParser\Font>|null |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $fonts; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @todo Objects other than Pages or Page might need to be treated specifically |
|
49
|
|
|
* in order to get Page objects out of them. |
|
50
|
|
|
* |
|
51
|
|
|
* @see https://github.com/smalot/pdfparser/issues/331 |
|
52
|
|
|
*/ |
|
53
|
66 |
|
public function getPages(bool $deep = false): array |
|
54
|
|
|
{ |
|
55
|
66 |
|
if (!$this->has('Kids')) { |
|
56
|
|
|
return []; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** @var ElementArray $kidsElement */ |
|
60
|
66 |
|
$kidsElement = $this->get('Kids'); |
|
61
|
|
|
|
|
62
|
66 |
|
if (!$deep) { |
|
63
|
|
|
return $kidsElement->getContent(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
// Prepare to apply the Pages' object's fonts to each page |
|
67
|
66 |
|
if (false === \is_array($this->fonts)) { |
|
68
|
65 |
|
$this->setupFonts(); |
|
69
|
|
|
} |
|
70
|
66 |
|
$fontsAvailable = 0 < \count($this->fonts); |
|
|
|
|
|
|
71
|
|
|
|
|
72
|
66 |
|
$kids = $kidsElement->getContent(); |
|
73
|
66 |
|
$pages = []; |
|
74
|
|
|
|
|
75
|
66 |
|
foreach ($kids as $kid) { |
|
76
|
66 |
|
if ($kid instanceof self) { |
|
77
|
4 |
|
$pages = array_merge($pages, $kid->getPages(true)); |
|
78
|
66 |
|
} elseif ($kid instanceof Page) { |
|
79
|
66 |
|
if ($fontsAvailable) { |
|
80
|
9 |
|
$kid->setFonts($this->fonts); |
|
81
|
|
|
} |
|
82
|
66 |
|
$pages[] = $kid; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
66 |
|
return $pages; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Gathers information about fonts and collects them in a list. |
|
91
|
|
|
* |
|
92
|
|
|
* @return void |
|
93
|
|
|
* |
|
94
|
|
|
* @internal |
|
95
|
|
|
*/ |
|
96
|
65 |
|
protected function setupFonts() |
|
97
|
|
|
{ |
|
98
|
65 |
|
$resources = $this->get('Resources'); |
|
99
|
|
|
|
|
100
|
65 |
|
if (method_exists($resources, 'has') && $resources->has('Font')) { |
|
101
|
|
|
// no fonts available, therefore stop here |
|
102
|
8 |
|
if ($resources->get('Font') instanceof Element\ElementMissing) { |
|
|
|
|
|
|
103
|
|
|
return; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
8 |
|
if ($resources->get('Font') instanceof Header) { |
|
107
|
5 |
|
$fonts = $resources->get('Font')->getElements(); |
|
108
|
|
|
} else { |
|
109
|
3 |
|
$fonts = $resources->get('Font')->getHeader()->getElements(); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
8 |
|
$table = []; |
|
113
|
|
|
|
|
114
|
8 |
|
foreach ($fonts as $id => $font) { |
|
115
|
8 |
|
if ($font instanceof Font) { |
|
116
|
8 |
|
$table[$id] = $font; |
|
117
|
|
|
|
|
118
|
|
|
// Store too on cleaned id value (only numeric) |
|
119
|
8 |
|
$id = preg_replace('/[^0-9\.\-_]/', '', $id); |
|
120
|
8 |
|
if ('' != $id) { |
|
121
|
8 |
|
$table[$id] = $font; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
8 |
|
$this->fonts = $table; |
|
127
|
|
|
} else { |
|
128
|
57 |
|
$this->fonts = []; |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|