-
Notifications
You must be signed in to change notification settings - Fork 863
/
Copy pathCupsPrintConnector.php
188 lines (171 loc) · 5.12 KB
/
CupsPrintConnector.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
/**
* This file is part of escpos-php: PHP receipt printer library for use with
* ESC/POS-compatible thermal and impact printers.
*
* Copyright (c) 2014-20 Michael Billington < [email protected] >,
* incorporating modifications by others. See CONTRIBUTORS.md for a full list.
*
* This software is distributed under the terms of the MIT license. See LICENSE.md
* for details.
*/
declare(strict_types=1);
namespace Mike42\Escpos\PrintConnectors;
use Exception;
use BadMethodCallException;
/**
* Print connector that passes print data to CUPS print commands.
* Your printer mut be installed on the local CUPS instance to use this connector.
*/
class CupsPrintConnector implements PrintConnector
{
/**
* @var array $buffer
* Buffer of accumilated data.
*/
private $buffer;
/**
*
* @var string $printerName
* The name of the target printer.
*/
private $printerName;
/**
* Construct new CUPS print connector.
*
* @param string $dest
* The CUPS printer name to print to. This must be loaded using a raw driver.
* @throws BadMethodCallException
*/
public function __construct($dest)
{
$valid = $this->getLocalPrinters();
if (count($valid) == 0) {
throw new BadMethodCallException("You do not have any printers installed on " .
"this system via CUPS. Check 'lpr -a'.");
}
if (array_search($dest, $valid, true) === false) {
throw new BadMethodCallException("'$dest' is not a printer on this system. " .
"Printers are: [" . implode(", ", $valid) . "]");
}
$this->buffer = array ();
$this->printerName = $dest;
}
/**
* Cause a NOTICE if deconstructed before the job was printed.
*/
public function __destruct()
{
if ($this->buffer !== null) {
trigger_error("Print connector was not finalized. Did you forget to close the printer?", E_USER_NOTICE);
}
}
/**
* Send job to printer.
*/
public function finalize()
{
$data = implode($this->buffer);
$this->buffer = null;
// Build command to work on data
$tmpfname = tempnam(sys_get_temp_dir(), 'print-');
if ($tmpfname === false) {
throw new Exception("Failed to create temp file for printing.");
}
file_put_contents($tmpfname, $data);
$cmd = sprintf(
"lp -d %s %s",
escapeshellarg($this->printerName),
escapeshellarg($tmpfname)
);
try {
$this->getCmdOutput($cmd);
} catch (Exception $e) {
unlink($tmpfname);
throw $e;
}
unlink($tmpfname);
}
/**
* Run a command and throw an exception if it fails, or return the output if it works.
* (Basically exec() with good error handling)
*
* @param string $cmd
* Command to run
*/
protected function getCmdOutput($cmd)
{
$descriptors = array (
1 => array (
"pipe",
"w"
),
2 => array (
"pipe",
"w"
)
);
$process = proc_open($cmd, $descriptors, $fd);
if (! is_resource($process)) {
throw new Exception("Command '$cmd' failed to start.");
}
/* Read stdout */
$outputStr = stream_get_contents($fd [1]);
fclose($fd [1]);
/* Read stderr */
$errorStr = stream_get_contents($fd [2]);
fclose($fd [2]);
/* Finish up */
$retval = proc_close($process);
if ($retval != 0) {
throw new Exception("Command $cmd failed: $errorStr");
}
return $outputStr;
}
/**
* Read data from the printer.
*
* @param string $len Length of data to read.
* @return string Data read from the printer, or false where reading is not possible.
*/
public function read($len)
{
return false;
}
/**
* @param string $data
*/
public function write($data)
{
$this->buffer [] = $data;
}
/**
* Load a list of CUPS printers.
*
* @return array A list of printer names installed on this system. Any item
* on this list is valid for constructing a printer.
*/
protected function getLocalPrinters()
{
$outpStr = $this->getCmdOutput("lpstat -a");
$outpLines = explode("\n", trim($outpStr));
foreach ($outpLines as $line) {
$ret [] = $this->chopLpstatLine($line);
}
return $ret;
}
/**
* Get the item before the first space in a string
*
* @param string $line
* @return string the string, up to the first space, or the whole string if it contains no spaces.
*/
private function chopLpstatLine($line)
{
if (($pos = strpos($line, " ")) === false) {
return $line;
} else {
return substr($line, 0, $pos);
}
}
}