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