1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Text extractor that calls pdftotext to do the conversion. |
5
|
|
|
* @author mstephens |
6
|
|
|
* |
7
|
|
|
*/ |
8
|
|
|
class PDFTextExtractor extends FileTextExtractor |
|
|
|
|
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Set to bin path this extractor can execute |
12
|
|
|
* |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
private static $binary_location = null; |
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Used if binary_location isn't set. |
19
|
|
|
* List of locations to search for a given binary in |
20
|
|
|
* |
21
|
|
|
* @config |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private static $search_binary_locations = array( |
|
|
|
|
25
|
|
|
'/usr/bin', |
26
|
|
|
'/usr/local/bin', |
27
|
|
|
); |
28
|
|
|
|
29
|
|
|
public function isAvailable() |
30
|
|
|
{ |
31
|
|
|
$bin = $this->bin('pdftotext'); |
32
|
|
|
return $bin && file_exists($bin) && is_executable($bin); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function supportsExtension($extension) |
36
|
|
|
{ |
37
|
|
|
return strtolower($extension) === 'pdf'; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function supportsMime($mime) |
41
|
|
|
{ |
42
|
|
|
return in_array( |
43
|
|
|
strtolower($mime), |
44
|
|
|
array( |
45
|
|
|
'application/pdf', |
46
|
|
|
'application/x-pdf', |
47
|
|
|
'application/x-bzpdf', |
48
|
|
|
'application/x-gzpdf' |
49
|
|
|
) |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Accessor to get the location of the binary |
55
|
|
|
* |
56
|
|
|
* @param string $program Name of binary |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
protected function bin($program = '') |
60
|
|
|
{ |
61
|
|
|
// Get list of allowed search paths |
62
|
|
|
if ($location = $this->config()->binary_location) { |
63
|
|
|
$locations = array($location); |
64
|
|
|
} else { |
65
|
|
|
$locations = $this->config()->search_binary_locations; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// Find program in each path |
69
|
|
|
foreach($locations as $location) { |
70
|
|
|
$path = "{$location}/{$program}"; |
71
|
|
|
if(file_exists($path)) { |
72
|
|
|
return $path; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// Not found |
77
|
|
|
return null; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function getContent($path) |
81
|
|
|
{ |
82
|
|
|
if (!$path) { |
83
|
|
|
return ""; |
84
|
|
|
} // no file |
85
|
|
|
$content = $this->getRawOutput($path); |
86
|
|
|
return $this->cleanupLigatures($content); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Invoke pdftotext with the given path |
91
|
|
|
* |
92
|
|
|
* @param string $path |
93
|
|
|
* @return string Output |
94
|
|
|
* @throws FileTextExtractor_Exception |
95
|
|
|
*/ |
96
|
|
|
protected function getRawOutput($path) |
97
|
|
|
{ |
98
|
|
|
if(!$this->isAvailable()) { |
99
|
|
|
throw new FileTextExtractor_Exception("getRawOutput called on unavailable extractor"); |
100
|
|
|
} |
101
|
|
|
exec(sprintf('%s %s - 2>&1', $this->bin('pdftotext'), escapeshellarg($path)), $content, $err); |
102
|
|
|
if ($err) { |
|
|
|
|
103
|
|
|
throw new FileTextExtractor_Exception(sprintf( |
104
|
|
|
'PDFTextExtractor->getContent() failed for %s: %s', |
105
|
|
|
$path, |
106
|
|
|
implode('', $err) |
107
|
|
|
)); |
108
|
|
|
} |
109
|
|
|
return implode('', $content); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Removes utf-8 ligatures. |
114
|
|
|
* |
115
|
|
|
* @link http://en.wikipedia.org/wiki/Typographic_ligature#Computer_typesetting |
116
|
|
|
* |
117
|
|
|
* @param string $input |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
protected function cleanupLigatures($input) |
121
|
|
|
{ |
122
|
|
|
$mapping = array( |
123
|
|
|
'ff' => 'ff', |
124
|
|
|
'fi' => 'fi', |
125
|
|
|
'fl' => 'fl', |
126
|
|
|
'ffi' => 'ffi', |
127
|
|
|
'ffl' => 'ffl', |
128
|
|
|
'ſt' => 'ft', |
129
|
|
|
'st' => 'st' |
130
|
|
|
); |
131
|
|
|
return str_replace(array_keys($mapping), array_values($mapping), $input); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.