1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Ghostscript package. |
4
|
|
|
* |
5
|
|
|
* @author Daniel Schröder <[email protected]> |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace GravityMedia\Ghostscript; |
9
|
|
|
|
10
|
|
|
use GravityMedia\Ghostscript\Device\BoundingBoxInfo; |
11
|
|
|
use GravityMedia\Ghostscript\Device\Inkcov; |
12
|
|
|
use GravityMedia\Ghostscript\Device\NoDisplay; |
13
|
|
|
use GravityMedia\Ghostscript\Device\PdfInfo; |
14
|
|
|
use GravityMedia\Ghostscript\Device\PdfWrite; |
15
|
|
|
use GravityMedia\Ghostscript\Process\Arguments; |
16
|
|
|
use Symfony\Component\Process\Process; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The Ghostscript class. |
20
|
|
|
* |
21
|
|
|
* @package GravityMedia\Ghostscript |
22
|
|
|
*/ |
23
|
|
|
class Ghostscript |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* The default binary. |
27
|
|
|
*/ |
28
|
|
|
const DEFAULT_BINARY = 'gs'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The versions. |
32
|
|
|
* |
33
|
|
|
* @var string[] |
34
|
|
|
*/ |
35
|
|
|
protected static $versions = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The options. |
39
|
|
|
* |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
protected $options; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Create Ghostscript object. |
46
|
|
|
* |
47
|
|
|
* @param array $options |
48
|
|
|
* |
49
|
|
|
* @throws \RuntimeException |
50
|
28 |
|
*/ |
51
|
|
|
public function __construct(array $options = []) |
52
|
28 |
|
{ |
53
|
|
|
$this->options = $options; |
54
|
28 |
|
|
55
|
2 |
|
if (version_compare('9.00', $this->getVersion()) > 0) { |
56
|
|
|
throw new \RuntimeException('Ghostscript version 9.00 or higher is required'); |
57
|
24 |
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get option. |
62
|
|
|
* |
63
|
|
|
* @param string $name |
64
|
|
|
* @param mixed $default |
65
|
|
|
* |
66
|
|
|
* @return mixed |
67
|
26 |
|
*/ |
68
|
|
|
public function getOption($name, $default = null) |
69
|
26 |
|
{ |
70
|
10 |
|
if (array_key_exists($name, $this->options)) { |
71
|
|
|
return $this->options[$name]; |
72
|
|
|
} |
73
|
24 |
|
|
74
|
|
|
return $default; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get process to identify version. |
79
|
|
|
* |
80
|
|
|
* @param string $binary |
81
|
|
|
* |
82
|
|
|
* @return Process |
83
|
2 |
|
*/ |
84
|
|
|
protected function createProcessToIdentifyVersion($binary) |
85
|
2 |
|
{ |
86
|
|
|
return new Process($binary . ' --version'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get version. |
91
|
|
|
* |
92
|
|
|
* @throws \RuntimeException |
93
|
|
|
* |
94
|
|
|
* @return string |
95
|
26 |
|
*/ |
96
|
|
|
public function getVersion() |
97
|
26 |
|
{ |
98
|
|
|
$binary = $this->getOption('bin', static::DEFAULT_BINARY); |
99
|
26 |
|
|
100
|
2 |
|
if (!isset(static::$versions[$binary])) { |
101
|
2 |
|
$process = $this->createProcessToIdentifyVersion($binary); |
102
|
|
|
$process->run(); |
103
|
2 |
|
|
104
|
2 |
|
if (!$process->isSuccessful()) { |
105
|
|
|
throw new \RuntimeException($process->getErrorOutput()); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
static::$versions[$binary] = $process->getOutput(); |
109
|
|
|
} |
110
|
24 |
|
|
111
|
|
|
return static::$versions[$binary]; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Create arguments object. |
116
|
|
|
* |
117
|
|
|
* @return Arguments |
118
|
16 |
|
*/ |
119
|
|
|
protected function createArguments() |
120
|
16 |
|
{ |
121
|
|
|
$arguments = new Arguments(); |
122
|
16 |
|
|
123
|
16 |
|
if ($this->getOption('quiet', true)) { |
124
|
8 |
|
$arguments->addArgument('-q'); |
125
|
|
|
} |
126
|
16 |
|
|
127
|
|
|
return $arguments; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Create PDF device object. |
132
|
|
|
* |
133
|
|
|
* @param null|string $outputFile |
134
|
|
|
* |
135
|
|
|
* @return PdfWrite |
136
|
8 |
|
*/ |
137
|
|
|
public function createPdfDevice($outputFile = null) |
138
|
8 |
|
{ |
139
|
|
|
$device = new PdfWrite($this, $this->createArguments()); |
140
|
8 |
|
$device |
141
|
8 |
|
->setSafer() |
142
|
8 |
|
->setBatch() |
143
|
|
|
->setNoPause(); |
144
|
8 |
|
|
145
|
2 |
|
if (null === $outputFile) { |
146
|
1 |
|
$outputFile = '-'; |
147
|
|
|
} |
148
|
8 |
|
|
149
|
|
|
$device->setOutputFile($outputFile); |
150
|
8 |
|
|
151
|
|
|
return $device; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Create no display device object. |
156
|
|
|
* |
157
|
|
|
* @return NoDisplay |
158
|
2 |
|
*/ |
159
|
|
|
public function createNoDisplayDevice() |
160
|
2 |
|
{ |
161
|
|
|
return new NoDisplay($this, $this->createArguments()); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Create PDF info device object. |
166
|
|
|
* |
167
|
|
|
* @param string $pdfInfoPath Path to toolbin/pdf_info.ps |
168
|
|
|
* |
169
|
|
|
* @return PdfInfo |
170
|
2 |
|
*/ |
171
|
|
|
public function createPdfInfoDevice($pdfInfoPath) |
172
|
2 |
|
{ |
173
|
|
|
return new PdfInfo($this, $this->createArguments(), $pdfInfoPath); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Create bounding box info device object. |
178
|
|
|
* |
179
|
|
|
* @return BoundingBoxInfo |
180
|
2 |
|
*/ |
181
|
|
|
public function createBoundingBoxInfoDevice() |
182
|
2 |
|
{ |
183
|
|
|
$device = new BoundingBoxInfo($this, $this->createArguments()); |
184
|
2 |
|
$device |
185
|
2 |
|
->setSafer() |
186
|
2 |
|
->setBatch() |
187
|
|
|
->setNoPause(); |
188
|
2 |
|
|
189
|
|
|
return $device; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Create inkcov device object |
194
|
|
|
* |
195
|
|
|
* @return Inkcov |
196
|
|
|
*/ |
197
|
|
|
public function createInkcovDevice() |
198
|
|
|
{ |
199
|
|
|
$this->options['quiet'] = false; |
200
|
|
|
|
201
|
|
|
$arguments = $this->createArguments(); |
202
|
|
|
$arguments->addArgument('-o'); |
203
|
|
|
$arguments->addArgument('-'); |
204
|
|
|
|
205
|
|
|
$device = new Inkcov($this, $arguments); |
206
|
|
|
|
207
|
|
|
return $device; |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|