forked from mnico/pdfTK-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPdftk.php
104 lines (81 loc) · 1.84 KB
/
Pdftk.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
abstract class Pdftk
{
/**
* @var string $pdftk
*/
protected $pdftkCmd = '';
/**
* @var array $files
*/
private $logs = array();
/**
* @var bool $debug
*/
private $debug = FALSE;
/**
* @param string $pdftk
* Linea de comando de pdftk
* @param bool $debug
* Si se desea capturar los comandos ejecutados.
*/
public function __construct($pdftk = 'pdftk', $debug = FALSE) {
$this->debug = $debug;
$this->pdftkCmd = $pdftk;
}
/**
* Execute cmd
*/
protected function executeCmd($cmd, $sInput = null) {
$this->addLog("The command: $cmd");
/**
*
*/
$aResult = array('stdout' => '', 'stderr' => '', 'return' => '');
$aDescriptorSpec = array(
0 => array('pipe', 'r'),
1 => array('pipe', 'w'),
2 => array('pipe', 'w')
);
$proc = proc_open($cmd, $aDescriptorSpec, $aPipes);
if (!is_resource($proc)) {
throw new Exception('Unable to open command line resource');
}
fwrite($aPipes[0], $sInput);
fclose($aPipes[0]);
$aResult['stdout'] = stream_get_contents($aPipes[1]);
fclose($aPipes[1]);
$aResult['stderr'] = stream_get_contents($aPipes[2]);
fclose($aPipes[2]);
$aResult['return'] = proc_close($proc);
return $aResult;
return shell_exec($cmd);
}
/**
* Get an array with logs.
*
* @return array
*/
public function getLogs() {
if ($this->debug) {
return $this->logs;
}
return array();
}
/**
* @param string $message
* Message to log
* @param string $type
*/
protected function addLog($message, $type = "success") {
if ($this->debug) {
$this->logs[] = array(
'message' => $message,
'type' => $type,
);
}
}
static function help() {
return shell_exec("$this->pdftkCmd -h");
}
}