1
|
|
|
<?php |
2
|
|
|
class pdftk { |
3
|
|
|
protected $config; |
4
|
|
|
|
5
|
|
|
public function __construct( $config = array() ) { |
6
|
|
|
if (!$config['cmd']) { |
7
|
|
|
$config['cmd'] = '/usr/bin/pdftk '; |
8
|
|
|
} |
9
|
|
|
|
10
|
|
View Code Duplication |
if (!$config['temp']) { |
11
|
|
|
$context = pobject::getContext(); |
12
|
|
|
$me = $context["arCurrentObject"]; |
13
|
|
|
$config['temp'] = $me->store->get_config( "files" ) . "temp/"; |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
$this->config = $config; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function concat( $files = array() ) { |
20
|
|
|
if (!sizeof($files)) { |
21
|
|
|
return false; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
$inputs = array(); |
25
|
|
|
$i = 1; |
|
|
|
|
26
|
|
View Code Duplication |
foreach ($files as $file) { |
27
|
|
|
$tempFile = tempnam( $this->config['temp'], 'pdftk-input-' ); |
28
|
|
|
if ( !$tempFile ) { |
29
|
|
|
return ar_error::raiseError( "pdftk: could not create a temporary input file", 202 ); |
30
|
|
|
} |
31
|
|
|
$inputs[] = $tempFile; |
32
|
|
|
file_put_contents($tempFile, $file); |
33
|
|
|
} |
34
|
|
|
unset($files); |
35
|
|
|
|
36
|
|
|
$outputFile = tempnam( $this->config['temp'], 'pdftk-output-' ); |
37
|
|
|
if ( !$outputFile ) { |
38
|
|
|
return ar_error::raiseError( "pdftk: could not create a temporary output file", 204 ); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
// pdftk in1.pdf in2.pdf cat output out1.pdf |
42
|
|
|
$execString = $this->config['cmd']; |
43
|
|
|
$execString .= " " . implode(" ", $inputs); |
44
|
|
|
$execString .= " cat output $outputFile"; |
45
|
|
|
|
46
|
|
|
$execOutput = array(); |
47
|
|
|
$execResult = 0; |
48
|
|
|
|
49
|
|
|
exec( $execString, $execOutput, $execResult ); |
50
|
|
|
|
51
|
|
View Code Duplication |
if ( $execResult != 0 ) { |
|
|
|
|
52
|
|
|
foreach ($inputs as $file) { |
53
|
|
|
@unlink($file); |
54
|
|
|
} |
55
|
|
|
@unlink($outputFile); |
56
|
|
|
return ar_error::raiseError( "pdftk: error ($execResult) while trying to generate PDF: " . implode( "\n", (array) $execOutput ), 203 ); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$result = file_get_contents( $outputFile ); |
60
|
|
|
|
61
|
|
|
foreach ($inputs as $file) { |
62
|
|
|
@unlink($file); |
63
|
|
|
} |
64
|
|
|
@unlink($outputFile); |
65
|
|
|
return $result; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function background( $files = array() ) { |
69
|
|
|
/* |
70
|
|
|
Call this with arguments: |
71
|
|
|
$files = array( |
72
|
|
|
"pdf" => $pdfData, |
73
|
|
|
"background" => $backgroundPdf |
74
|
|
|
); |
75
|
|
|
*/ |
76
|
|
|
if (!sizeof($files)) { |
77
|
|
|
return false; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$inputs = array(); |
81
|
|
|
$i = 1; |
|
|
|
|
82
|
|
View Code Duplication |
foreach ($files as $key => $file) { |
83
|
|
|
$tempFile = tempnam( $this->config['temp'], 'pdftk-input-' ); |
84
|
|
|
if ( !$tempFile ) { |
85
|
|
|
return ar_error::raiseError( "pdftk: could not create a temporary input file", 202 ); |
86
|
|
|
} |
87
|
|
|
$inputs[$key] = $tempFile; |
88
|
|
|
file_put_contents($tempFile, $file); |
89
|
|
|
} |
90
|
|
|
unset($files); |
91
|
|
|
|
92
|
|
|
$outputFile = tempnam( $this->config['temp'], 'pdftk-output-' ); |
93
|
|
|
if ( !$outputFile ) { |
94
|
|
|
return ar_error::raiseError( "pdftk: could not create a temporary output file", 204 ); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
// pdftk in1.pdf in2.pdf cat output out1.pdf |
98
|
|
|
// system("pdftk.exe \"$frontpage\" background $frontpage_file output \"$wm_frontpage\""); |
99
|
|
|
$execString = $this->config['cmd']; |
100
|
|
|
$execString .= " " . $inputs["pdf"]; |
101
|
|
|
$execString .= " background " . $inputs["background"] . " output $outputFile"; |
102
|
|
|
|
103
|
|
|
$execOutput = array(); |
104
|
|
|
$execResult = 0; |
105
|
|
|
|
106
|
|
|
exec( $execString, $execOutput, $execResult ); |
107
|
|
|
|
108
|
|
View Code Duplication |
if ( $execResult != 0 ) { |
|
|
|
|
109
|
|
|
foreach ($inputs as $file) { |
110
|
|
|
@unlink($file); |
111
|
|
|
} |
112
|
|
|
@unlink($outputFile); |
113
|
|
|
return ar_error::raiseError( "pdftk: error ($execResult) while trying to generate PDF: " . implode( "\n", (array) $execOutput ), 203 ); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$result = file_get_contents( $outputFile ); |
117
|
|
|
|
118
|
|
|
foreach ($inputs as $file) { |
119
|
|
|
@unlink($file); |
120
|
|
|
} |
121
|
|
|
@unlink($outputFile); |
122
|
|
|
return $result; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function pages( $args = array() ) { |
126
|
|
|
/* |
127
|
|
|
Call this with arguments: |
128
|
|
|
$args = array( |
129
|
|
|
"pdf" => $pdfData, |
130
|
|
|
"pages" => "1-r2" // removes the last page; |
131
|
|
|
); |
132
|
|
|
*/ |
133
|
|
|
if (!sizeof($args)) { |
134
|
|
|
return false; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$inputs = array(); |
138
|
|
|
|
139
|
|
|
$tempFile = tempnam( $this->config['temp'], 'pdftk-input-' ); |
140
|
|
|
if ( !$tempFile ) { |
141
|
|
|
return ar_error::raiseError( "pdftk: could not create a temporary input file", 202 ); |
142
|
|
|
} |
143
|
|
|
$inputs['pdf'] = $tempFile; |
144
|
|
|
file_put_contents($tempFile, $args['pdf']); |
145
|
|
|
|
146
|
|
|
$outputFile = tempnam( $this->config['temp'], 'pdftk-output-' ); |
147
|
|
|
if ( !$outputFile ) { |
148
|
|
|
return ar_error::raiseError( "pdftk: could not create a temporary output file", 204 ); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
// pdftk in1.pdf cat 1-r2 output out1.pdf |
152
|
|
|
// system("pdftk.exe \"$frontpage\" background $frontpage_file output \"$wm_frontpage\""); |
153
|
|
|
$execString = $this->config['cmd']; |
154
|
|
|
$execString .= " " . $inputs["pdf"]; |
155
|
|
|
$execString .= " cat " . escapeshellcmd($args["pages"]) . " output $outputFile"; |
156
|
|
|
|
157
|
|
|
$execOutput = array(); |
158
|
|
|
$execResult = 0; |
159
|
|
|
|
160
|
|
|
exec( $execString, $execOutput, $execResult ); |
161
|
|
|
|
162
|
|
View Code Duplication |
if ( $execResult != 0 ) { |
|
|
|
|
163
|
|
|
foreach ($inputs as $file) { |
164
|
|
|
@unlink($file); |
165
|
|
|
} |
166
|
|
|
@unlink($outputFile); |
167
|
|
|
return ar_error::raiseError( "pdftk: error ($execResult) while trying to generate PDF: " . implode( "\n", (array) $execOutput ), 203 ); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
$result = file_get_contents( $outputFile ); |
171
|
|
|
|
172
|
|
|
foreach ($inputs as $file) { |
173
|
|
|
@unlink($file); |
174
|
|
|
} |
175
|
|
|
@unlink($outputFile); |
176
|
|
|
return $result; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
class pinp_pdftk { |
181
|
|
|
private $instance; |
182
|
|
|
|
183
|
|
|
public function __construct() { |
184
|
|
|
$this->instance = new pdftk(); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public function _concat( $files = array() ) { |
188
|
|
|
return $this->instance->concat( $files ); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function _background( $args = array() ) { |
192
|
|
|
return $this->instance->background( $args ); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
public function _pages( $args = array() ) { |
196
|
|
|
return $this->instance->pages( $args ); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
public static function _get() { |
200
|
|
|
return new pinp_pdftk(); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.