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\Devices\PdfWrite; |
11
|
|
|
use GravityMedia\Ghostscript\Process\Arguments as ProcessArguments; |
12
|
|
|
use Symfony\Component\Process\ProcessBuilder; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* The Ghostscript class |
16
|
|
|
* |
17
|
|
|
* @package GravityMedia\Ghostscript |
18
|
|
|
*/ |
19
|
|
|
class Ghostscript |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* The default binary |
23
|
|
|
*/ |
24
|
|
|
const DEFAULT_BINARY = 'gs'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The options |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $options; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The version |
35
|
|
|
* |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $version; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Create Ghostscript object |
42
|
|
|
* |
43
|
|
|
* @param array $options |
44
|
|
|
* |
45
|
|
|
* @throws \RuntimeException |
46
|
|
|
*/ |
47
|
27 |
|
public function __construct(array $options = []) |
48
|
|
|
{ |
49
|
27 |
|
$this->options = $options; |
50
|
|
|
|
51
|
27 |
|
if (version_compare('9.00', $this->getVersion()) > 0) { |
52
|
3 |
|
throw new \RuntimeException('Ghostscript version 9.00 or higher is required'); |
53
|
|
|
} |
54
|
21 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get option |
58
|
|
|
* |
59
|
|
|
* @param string $name |
60
|
|
|
* @param mixed $default |
61
|
|
|
* |
62
|
|
|
* @return mixed |
63
|
|
|
*/ |
64
|
24 |
|
public function getOption($name, $default = null) |
65
|
|
|
{ |
66
|
24 |
|
if (array_key_exists($name, $this->options)) { |
67
|
6 |
|
return $this->options[$name]; |
68
|
|
|
} |
69
|
|
|
|
70
|
24 |
|
return $default; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Create process builder object |
75
|
|
|
* |
76
|
|
|
* @param array $arguments |
77
|
|
|
* |
78
|
|
|
* @return ProcessBuilder |
79
|
|
|
*/ |
80
|
24 |
|
protected function createProcessBuilder(array $arguments = []) |
81
|
|
|
{ |
82
|
24 |
|
$processBuilder = new ProcessBuilder($arguments); |
83
|
24 |
|
$processBuilder->setPrefix($this->getOption('bin', self::DEFAULT_BINARY)); |
84
|
24 |
|
$processBuilder->addEnvironmentVariables($this->getOption('env', [])); |
85
|
|
|
|
86
|
24 |
|
return $processBuilder; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get version |
91
|
|
|
* |
92
|
|
|
* @throws \RuntimeException |
93
|
|
|
* |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
24 |
|
public function getVersion() |
97
|
|
|
{ |
98
|
24 |
|
if (null === $this->version) { |
99
|
24 |
|
$process = $this->createProcessBuilder(['--version'])->getProcess(); |
100
|
24 |
|
$process->run(); |
101
|
|
|
|
102
|
24 |
|
if (!$process->isSuccessful()) { |
103
|
3 |
|
throw new \RuntimeException($process->getErrorOutput()); |
104
|
|
|
} |
105
|
|
|
|
106
|
21 |
|
$this->version = $process->getOutput(); |
107
|
21 |
|
} |
108
|
|
|
|
109
|
21 |
|
return $this->version; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Create process arguments object |
114
|
|
|
* |
115
|
|
|
* @param array $arguments |
116
|
|
|
* |
117
|
|
|
* @return ProcessArguments |
118
|
|
|
*/ |
119
|
6 |
|
protected function createProcessArguments(array $arguments = []) |
120
|
|
|
{ |
121
|
6 |
|
$processArguments = new ProcessArguments(); |
122
|
6 |
|
$processArguments->addArguments($arguments); |
123
|
|
|
|
124
|
6 |
|
if ($this->getOption('quiet', true)) { |
125
|
6 |
|
$processArguments->addArgument('-q'); |
126
|
6 |
|
} |
127
|
|
|
|
128
|
6 |
|
return $processArguments; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Create PDF device object |
133
|
|
|
* |
134
|
|
|
* @param null|string $outputFile |
135
|
|
|
* |
136
|
|
|
* @return PdfWrite |
137
|
|
|
*/ |
138
|
3 |
|
public function createPdfDevice($outputFile = null) |
139
|
|
|
{ |
140
|
3 |
|
$builder = $this->createProcessBuilder(); |
141
|
3 |
|
$arguments = $this->createProcessArguments([ |
142
|
3 |
|
'-dSAFER', |
143
|
3 |
|
'-dBATCH', |
144
|
|
|
'-dNOPAUSE' |
145
|
3 |
|
]); |
146
|
|
|
|
147
|
3 |
|
$device = new PdfWrite($builder, $arguments); |
148
|
|
|
|
149
|
3 |
|
if (null !== $outputFile) { |
150
|
3 |
|
$device->setOutputFile($outputFile); |
151
|
3 |
|
} |
152
|
|
|
|
153
|
3 |
|
return $device; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|