Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ability to request CUPS Job Number and the amount of pages printed #1037

Open
wants to merge 5 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 49 additions & 1 deletion src/Mike42/Escpos/PrintConnectors/CupsPrintConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@ class CupsPrintConnector implements PrintConnector
* The name of the target printer.
*/
private $printerName;

/**
*
* @var int $jobNumber
* The job dispatch number
*/
private $jobNumber;

/**
*
* @var int amountPages
* The amount of pages that CUPS reported to print
*/
private $amountPages;

/**
* Construct new CUPS print connector.
Expand Down Expand Up @@ -91,13 +105,47 @@ public function finalize()
escapeshellarg($tmpfname)
);
try {
$this->getCmdOutput($cmd);
$response = $this->getCmdOutput($cmd);
$exploded = explode(" ", $response);

// Set default data in case the checks fail, and make them obvioulsy incorrect
$this->jobNumber = -1;
$this->amountPages = -1;

if (count($exploded) > 3) {
$jobName = explode("-", $exploded[3]);
$this->jobNumber = intval($jobName[count($jobName) - 1]);
}

if (count($exploded) > 4) {
$this->amountPages = intval(substr($exploded[4], 1));
}
} catch (Exception $e) {
unlink($tmpfname);
throw $e;
}
unlink($tmpfname);
}

/**
* Retrieve the job number
*
* @return int Assigned job number, or null if the job has not yet been finalized.
*/
public function getJobNumber()
{
return $this->jobNumber;
}

/**
* Retrieve the amount of files printed
*
* @return in The amount of files CUPS reported to print, or null if the job has not yet been finalized.
*/
public function getAmountPages()
{
return $this->amountPages;
}

/**
* Run a command and throw an exception if it fails, or return the output if it works.
Expand Down
28 changes: 28 additions & 0 deletions src/Mike42/Escpos/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,34 @@ public function textRaw(string $str = "")
{
$this -> buffer -> writeTextRaw((string)$str);
}

/**
* Returns job number assigned, only available if CupsPrintConnector has been used
*
* @return int Assigned job number, or null if the job has not yet been finalized or the printconnector does not support this function
*/
public function getJobNumber()
{
if (! $this -> connector instanceof \Mike42\Escpos\PrintConnectors\CupsPrintConnector) {
return null;
}

return $this -> connector -> getJobNumber();
}

/**
* Returns amount of pages printed, only available if CupsPrintConnector has been used
*
* @return int Amount of pages printed, or null if the job has not yet been finalized or the printconnector does not support this function
*/
public function getAmountPages()
{
if (! $this -> connector instanceof \Mike42\Escpos\PrintConnectors\CupsPrintConnector) {
return null;
}

return $this -> connector -> getAmountPages();
}

/**
* Wrapper for GS ( k, to calculate and send correct data length.
Expand Down