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
|
13 |
|
public function __construct(string $source, string $target = '', $executable = '') |
36
|
|
|
{ |
37
|
13 |
|
$this->executable = $executable ? $executable : 'pdftoppm'; |
38
|
13 |
|
$this->format('jpeg'); |
39
|
13 |
|
$this->extension('jpg'); |
40
|
13 |
|
$this->page(1); |
41
|
13 |
|
$this->scaleTo(150); |
42
|
|
|
|
43
|
13 |
|
$this->source($source); |
44
|
13 |
|
if (!is_file($source)) { |
45
|
1 |
|
throw new FileNotFound("could not find pdf {$source}"); |
46
|
|
|
} |
47
|
12 |
|
if (!$target) { |
48
|
11 |
|
$this->target = pathinfo($this->source, PATHINFO_DIRNAME).'/'.pathinfo($this->source, PATHINFO_FILENAME); |
49
|
|
|
} else { |
50
|
1 |
|
$this->target($target); |
51
|
|
|
} |
52
|
|
|
|
53
|
12 |
|
} |
54
|
|
|
|
55
|
12 |
|
public static function create(string $source, string $target = '', string $executable = '') |
56
|
|
|
{ |
57
|
12 |
|
return (new static($source, $target, $executable)); |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
public function executable(string $executable) |
61
|
|
|
{ |
62
|
1 |
|
$this->executable = $executable; |
63
|
1 |
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
13 |
|
protected function source(string $source) |
67
|
|
|
{ |
68
|
13 |
|
$this->source = $source; |
69
|
13 |
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
3 |
|
public function target(string $target = null) |
73
|
|
|
{ |
74
|
3 |
|
$this->target = pathinfo($target, PATHINFO_DIRNAME) . '/' . pathinfo($target, PATHINFO_FILENAME); |
75
|
3 |
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
13 |
|
public function scaleTo(int $scaleTo) |
79
|
|
|
{ |
80
|
13 |
|
$this->options['scale-to'] = '-scale-to ' . (string) $scaleTo; |
81
|
13 |
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
13 |
|
public function firstPage(int $firstPage) |
85
|
|
|
{ |
86
|
13 |
|
$this->options['firstPage'] = '-f ' . (string) $firstPage; |
87
|
13 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
13 |
|
public function lastPage(int $lastPage) |
91
|
|
|
{ |
92
|
13 |
|
$this->options['lastPage'] = '-l ' . (string) $lastPage; |
93
|
13 |
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
13 |
|
public function page(int $page) |
97
|
|
|
{ |
98
|
13 |
|
$this->firstPage($page); |
99
|
13 |
|
$this->lastPage($page); |
100
|
13 |
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
13 |
|
public function format(string $format) |
104
|
|
|
{ |
105
|
13 |
|
$this->options['format'] = '-' . $this->normalizeFormat($format); |
106
|
13 |
|
$this->actualizeExtension(); |
107
|
|
|
|
108
|
13 |
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
13 |
|
protected function actualizeExtension() |
112
|
|
|
{ |
113
|
13 |
|
$this->options['format'] == '-jpeg' && $this->extension('jpg'); |
114
|
13 |
|
$this->options['format'] == '-tiff' && $this->extension('tif'); |
115
|
13 |
|
$this->options['format'] == '-png' && $this->extension('png'); |
116
|
13 |
|
} |
117
|
|
|
|
118
|
13 |
|
protected function normalizeFormat(string $format) |
119
|
|
|
{ |
120
|
13 |
|
$format = strtolower($format); |
121
|
13 |
|
$format = $format == 'jpg' ? 'jpeg' : $format; |
122
|
13 |
|
$format = $format == 'tif' ? 'tiff' : $format; |
123
|
|
|
|
124
|
13 |
|
$formats = ['jpeg', 'png', 'tiff']; |
125
|
|
|
|
126
|
13 |
|
if (!in_array($format, $formats)) { |
127
|
1 |
|
throw new FileFormatNotAllowed("fileformat not allowed {$format}"); |
128
|
|
|
} |
129
|
13 |
|
return $format; |
130
|
|
|
} |
131
|
|
|
|
132
|
13 |
|
protected function extension($extension = null) |
133
|
|
|
{ |
134
|
13 |
|
$this->extension = $extension; |
135
|
13 |
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
1 |
|
public function addOption(string $options) |
139
|
|
|
{ |
140
|
1 |
|
$this->options[] = $options; |
141
|
1 |
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
1 |
|
public function setOptions(string $options) |
145
|
|
|
{ |
146
|
1 |
|
$this->options = [$options]; |
147
|
1 |
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
11 |
|
public function command() |
151
|
|
|
{ |
152
|
11 |
|
$options = join(' ', $this->options); |
153
|
11 |
|
$command = "{$this->executable} {$options} '{$this->source}' > '{$this->target}.{$this->extension}'"; |
154
|
11 |
|
return $command; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* extract text |
159
|
|
|
* @return string |
160
|
|
|
*/ |
161
|
8 |
|
public function convert() |
162
|
|
|
{ |
163
|
8 |
|
$process = Process::fromShellCommandline($this->command()); |
164
|
8 |
|
$process->run(); |
165
|
|
|
|
166
|
8 |
|
if (!$process->isSuccessful()) { |
167
|
1 |
|
throw new CouldNotConvertPdf($process); |
168
|
|
|
} |
169
|
|
|
|
170
|
7 |
|
return $process->getExitCode(); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|