PdfWrite::createProcessArguments()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 9
cts 9
cp 1
rs 9.6
c 0
b 0
f 0
cc 3
nc 4
nop 1
crap 3
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\Device;
9
10
use GravityMedia\Ghostscript\Enum\PdfSettings;
11
use GravityMedia\Ghostscript\Enum\ProcessColorModel;
12
use GravityMedia\Ghostscript\Ghostscript;
13
use GravityMedia\Ghostscript\Input;
14
use GravityMedia\Ghostscript\Process\Arguments;
15
16
/**
17
 * The PDF write device class.
18
 *
19
 * @package GravityMedia\Ghostscript\Devices
20
 */
21
class PdfWrite extends AbstractDevice
22
{
23
    /**
24
     * Use distiller parameters.
25
     */
26
    use DistillerParametersTrait;
27
28
    /**
29
     * Use color image compression distiller parameters.
30
     */
31
    use DistillerParameters\ColorImageCompressionTrait;
32
33
    /**
34
     * Use grayscale image compression distiller parameters.
35
     */
36
    use DistillerParameters\GrayImageCompressionTrait;
37
38
    /**
39
     * Use monochrome image compression distiller parameters.
40
     */
41
    use DistillerParameters\MonoImageCompressionTrait;
42
43
    /**
44
     * Use page compression distiller parameters.
45
     */
46
    use DistillerParameters\PageCompressionTrait;
47
48
    /**
49
     * Use font distiller parameters.
50
     */
51
    use DistillerParameters\FontTrait;
52
53
    /**
54
     * Use color conversion distiller parameters.
55
     */
56
    use DistillerParameters\ColorConversionTrait;
57
58
    /**
59
     * Use advanced distiller parameters.
60
     */
61
    use DistillerParameters\AdvancedTrait;
62
63
    /**
64
     * Create PDF write device object.
65
     *
66
     * @param Ghostscript $ghostscript
67
     * @param Arguments   $arguments
68
     */
69 26
    public function __construct(Ghostscript $ghostscript, Arguments $arguments)
70
    {
71 26
        parent::__construct($ghostscript, $arguments->setArgument('-sDEVICE=pdfwrite'));
72
73 26
        $this->setPdfSettings(PdfSettings::__DEFAULT);
74 26
    }
75
76
    /**
77
     * Get output file.
78
     *
79
     * @return null|string
80
     */
81 2
    public function getOutputFile()
82
    {
83 2
        return $this->getArgumentValue('-sOutputFile');
84
    }
85
86
    /**
87
     * Set output file.
88
     *
89
     * @param string $outputFile
90
     *
91
     * @return $this
92
     */
93 2
    public function setOutputFile($outputFile)
94
    {
95 2
        $this->setArgument('-sOutputFile=' . $outputFile);
96
97 2
        return $this;
98
    }
99
100
    /**
101
     * Get PDF settings.
102
     *
103
     * @return string
104
     */
105 10
    public function getPdfSettings()
106
    {
107 10
        return ltrim($this->getArgumentValue('-dPDFSETTINGS'), '/');
108
    }
109
110
    /**
111
     * Set PDF settings.
112
     *
113
     * @param string $pdfSettings
114
     *
115
     * @throws \InvalidArgumentException
116
     *
117
     * @return $this
118
     */
119 26
    public function setPdfSettings($pdfSettings)
120
    {
121 26
        $pdfSettings = ltrim($pdfSettings, '/');
122 26
        if (!in_array($pdfSettings, PdfSettings::values())) {
123 2
            throw new \InvalidArgumentException('Invalid PDF settings argument');
124
        }
125
126 26
        $this->setArgument(sprintf('-dPDFSETTINGS=/%s', $pdfSettings));
127
128 26
        return $this;
129
    }
130
131
    /**
132
     * Get process color model.
133
     *
134
     * @return string
135
     */
136 6
    public function getProcessColorModel()
137
    {
138 6
        return ltrim($this->getArgumentValue('-dProcessColorModel'), '/');
139
    }
140
141
    /**
142
     * Set process color model.
143
     *
144
     * @param string $processColorModel
145
     *
146
     * @throws \InvalidArgumentException
147
     *
148
     * @return $this
149
     */
150 8
    public function setProcessColorModel($processColorModel)
151
    {
152 8
        $processColorModel = ltrim($processColorModel, '/');
153 8
        if (!in_array($processColorModel, ProcessColorModel::values())) {
154 2
            throw new \InvalidArgumentException('Invalid process color model argument');
155
        }
156
157 6
        $this->setArgument(sprintf('-dProcessColorModel=/%s', $processColorModel));
158
159 6
        return $this;
160
    }
161
162
    /**
163
     * {@inheritdoc}
164
     */
165 2
    protected function createProcessArguments(Input $input)
166
    {
167 2
        $code = $input->getPostScriptCode();
168 2
        if (null === $code) {
169 2
            $code = '';
170 1
        }
171
172 2
        /**
173 2
         * Make sure .setpdfwrite gets called:
174 1
         * This operator conditions the environment for the pdfwrite output device. It is a shorthand for setting parameters
175
         * that have been deemed benificial. While not strictly necessary, it is usually helpful to set call this when using
176 2
         * the pdfwrite device.
177
         * @link http://ghostscript.com/doc/current/Language.htm#.setpdfwrite
178
         */
179
        if (false === strstr($code, '.setpdfwrite')) {
180
            $input->setPostScriptCode(ltrim($code . ' .setpdfwrite', ' '));
181
        }
182
183
        return parent::createProcessArguments($input);
184
    }
185
}
186