Initial commit

This commit is contained in:
Chris 2017-10-31 00:57:12 +01:00
commit d03428a2e4
5 changed files with 174 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/composer.lock
/vendor

2
README Normal file
View File

@ -0,0 +1,2 @@
noccylabs/lpr
=============

18
composer.json Normal file
View File

@ -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/"
}
}
}

7
src/LprException.php Normal file
View File

@ -0,0 +1,7 @@
<?php
namespace NoccyLabs\Lpr;
class LprException extends \RuntimeException
{}

145
src/PrintJob.php Normal file
View File

@ -0,0 +1,145 @@
<?php
namespace NoccyLabs\Lpr;
class PrintJob
{
/** @var string The print server to use (-H) */
protected $server;
/** @var string The name of the print job (-T) */
protected $jobName;
/** @var string Document filename */
protected $document;
/** @var string The printer to use as destination[/instance] (-P) */
protected $printer;
/** @var array The job options (-o) */
protected $options = [];
/** @var bool If true, encryption is forced when communicating with server (-E) */
protected $forceEncryption = false;
/** @var bool If true, the named print files will be deleted by lpr after print (-r) */
protected $deleteAfterPrint = false;
/**
* Constructor; creates a new print job.
*
*
*/
public function __construct($document, $printer=null, $server=null)
{
$this->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);
}
}