commit d03428a2e449f8d5e87bbc7bb55339ede8fd3b69 Author: Christopher Vagnetoft Date: Tue Oct 31 00:57:12 2017 +0100 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ff72e2d --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/composer.lock +/vendor diff --git a/README b/README new file mode 100644 index 0000000..cc81f41 --- /dev/null +++ b/README @@ -0,0 +1,2 @@ +noccylabs/lpr +============= diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..9be7abe --- /dev/null +++ b/composer.json @@ -0,0 +1,18 @@ +{ + "name": "noccylabs/lpr", + "description": "Print documents and images through lpr", + "type": "library", + "license": "GPL-3.0", + "authors": [ + { + "name": "Christopher Vagnetoft", + "email": "cvagnetoft@gmail.com" + } + ], + "require": {}, + "autoload": { + "psr-4": { + "NoccyLabs\\Lpr\\": "src/" + } + } +} \ No newline at end of file diff --git a/src/LprException.php b/src/LprException.php new file mode 100644 index 0000000..26de2aa --- /dev/null +++ b/src/LprException.php @@ -0,0 +1,7 @@ +document = $document; + $this->printer = $printer; + $this->server = $server; + } + + /** + * + * + * @return PrintJob + */ + public function setPrinter($printer) + { + $this->printer = $printer; + return $this; + } + + /** + * + * + * @return PrintJob + */ + public function setMedia($media) + { + $this->media = $media; + return $this; + } + + /** + * + * + * @param string $name The print job name to use + * @return PrintJob + */ + public function setJobName($name) + { + $this->jobName = $name; + return $this; + } + + /** + * + * + * @return PrintJob + */ + public function setServer($server, $port) + { + $this->server = $server . (!!$port?":{$port}":""); + return $this; + } + + /** + * + * + * @return PrintJob + */ + public function setOption($option, $value) + { + $this->options[$option] = $value; + return $this; + } + + /** + * Attempt to submit the job using the lpr command. + * + * + */ + public function submit() + { + $options = []; + if ($this->server !== null) { + $options[] = "-H"; + $options[] = $this->server; + } + if ($this->printer !== null) { + $options[] = "-P"; + $options[] = $this->printer; + } + if ($this->jobName !== null) { + $options[] = "-T"; + $options[] = $this->jobName; + } + foreach ($this->options as $option=>$value) { + $options[] = "-o"; + if ($value === true) { + $options[] = $option; + } else { + $options[] = $option."=".$value; + } + } + + $optionString = join(" ", array_map("escapeshellarg", $options)); + $cmdLine = sprintf("lpr %s %s", $optionString, escapeshellarg($this->document)); + + $ds = [ + 1 => [ 'pipe', 'w' ], + 2 => [ 'pipe', 'w' ] + ]; + $proc = proc_open($cmdLine, $ds, $pipes); + + do { + usleep(10000); + $status = proc_get_status($proc); + } while ($status['running']); + + if ($status['exitcode'] > 0) { + $stderr = stream_get_contents($pipes[2]); + throw new LprException( + sprintf("lpr exited with code %d: %s", $status['exitcode'], $stderr) + ); + } + + proc_close($proc); + + } + +} +