1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ottosmops\Pdftothumb; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Process\Process; |
6
|
|
|
|
7
|
|
|
use Ottosmops\Pdftothumb\Exceptions\CouldNotConvertPdf; |
8
|
|
|
use Ottosmops\Pdftothumb\Exceptions\FileNotFound; |
9
|
|
|
use Ottosmops\Pdftothumb\Exceptions\BinaryNotFound; |
10
|
|
|
use Ottosmops\Pdftothumb\Exceptions\FileFormatNotAllowed; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
class Converter |
14
|
|
|
{ |
15
|
|
|
public $executable = ''; |
16
|
|
|
|
17
|
|
|
protected $options = []; |
18
|
|
|
|
19
|
|
|
protected $source = ''; |
20
|
|
|
|
21
|
|
|
protected $target = ''; |
22
|
|
|
|
23
|
|
|
protected $extension = ''; |
24
|
|
|
|
25
|
|
|
// cf https://www.systutorials.com/docs/linux/man/1-pdftoppm/ |
26
|
|
|
public $exitCodes = [ |
27
|
|
|
0 => 'No error.', |
28
|
|
|
1 => 'Error opening a PDF file.', |
29
|
|
|
2 => 'Error opening an output file.', |
30
|
|
|
3 => 'Error related to PDF permissions.', |
31
|
|
|
99 => 'Other error.' |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
public function __construct(string $source, string $target = '', $executable = '') |
36
|
|
|
{ |
37
|
|
|
$this->executable = $executable ? $executable : 'pdftoppm'; |
38
|
|
|
$this->format('jpeg'); |
39
|
|
|
$this->extension('jpg'); |
40
|
|
|
$this->page(1); |
41
|
|
|
$this->scaleTo(150); |
42
|
|
|
|
43
|
|
|
$this->source($source); |
44
|
|
|
if (!is_file($source)) { |
45
|
|
|
throw new FileNotFound("could not find pdf {$source}"); |
46
|
|
|
} |
47
|
|
|
if (!$target) { |
48
|
|
|
$this->target = pathinfo($this->source, PATHINFO_DIRNAME).'/'.pathinfo($this->source, PATHINFO_FILENAME); |
49
|
|
|
} else { |
50
|
|
|
$this->target($target); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public static function create(string $source, string $target = '', string $executable = '') |
56
|
|
|
{ |
57
|
|
|
return (new static($source, $target, $executable)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function executable(string $executable) |
61
|
|
|
{ |
62
|
|
|
$this->executable = $executable; |
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
protected function source(string $source) |
67
|
|
|
{ |
68
|
|
|
$this->source = $source; |
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function target(string $target = null) |
73
|
|
|
{ |
74
|
|
|
$this->target = pathinfo($target, PATHINFO_DIRNAME) . '/' . pathinfo($target, PATHINFO_FILENAME); |
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function scaleTo(int $scaleTo) |
79
|
|
|
{ |
80
|
|
|
$this->options['scale-to'] = '-scale-to ' . (string) $scaleTo; |
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function firstPage(int $firstPage) |
85
|
|
|
{ |
86
|
|
|
$this->options['firstPage'] = '-f ' . (string) $firstPage; |
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function lastPage(int $lastPage) |
91
|
|
|
{ |
92
|
|
|
$this->options['lastPage'] = '-l ' . (string) $lastPage; |
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function page(int $page) |
97
|
|
|
{ |
98
|
|
|
$this->firstPage($page); |
99
|
|
|
$this->lastPage($page); |
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function format(string $format) |
104
|
|
|
{ |
105
|
|
|
$this->options['format'] = '-' . $this->normalizeFormat($format); |
106
|
|
|
$this->setExtensionFromFormat($format); |
107
|
|
|
|
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
protected function setExtensionFromFormat(string $format) |
|
|
|
|
112
|
|
|
{ |
113
|
|
|
$this->options['format'] == '-jpeg' && $this->extension('jpg'); |
114
|
|
|
$this->options['format'] == '-tiff' && $this->extension('tif'); |
115
|
|
|
$this->options['format'] == '-png' && $this->extension('png'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
protected function normalizeFormat(string $format) |
119
|
|
|
{ |
120
|
|
|
$format = strtolower($format); |
121
|
|
|
$format = $format == 'jpg' ? 'jpeg' : $format; |
122
|
|
|
$format = $format == 'tif' ? 'tiff' : $format; |
123
|
|
|
|
124
|
|
|
$formats = ['jpeg', 'png', 'tiff']; |
125
|
|
|
|
126
|
|
|
if (!in_array($format, $formats)) { |
127
|
|
|
throw new FileFormatNotAllowed("fileformat not allowed {$format}"); |
128
|
|
|
} |
129
|
|
|
return $format; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
protected function extension($extension = null) |
133
|
|
|
{ |
134
|
|
|
$this->extension = $extension; |
135
|
|
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function addOption(string $options) |
139
|
|
|
{ |
140
|
|
|
$this->options[] = $options; |
141
|
|
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function setOptions(string $options) |
145
|
|
|
{ |
146
|
|
|
$this->options = [$options]; |
147
|
|
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function command() |
151
|
|
|
{ |
152
|
|
|
$options = join(' ', $this->options); |
153
|
|
|
$command = "{$this->executable} {$options} '{$this->source}' > '{$this->target}.{$this->extension}'"; |
154
|
|
|
return $command; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* extract text |
159
|
|
|
* @return string |
160
|
|
|
*/ |
161
|
|
|
public function convert() |
162
|
|
|
{ |
163
|
|
|
$process = new Process($this->command()); |
164
|
|
|
$process->run(); |
165
|
|
|
|
166
|
|
|
if (!$process->isSuccessful()) { |
167
|
|
|
throw new CouldNotConvertPdf($process); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return $process->getExitCode(); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.