From e5120e5d6ba0d5af1ef71f745c2a368128faa675 Mon Sep 17 00:00:00 2001 From: "Max@Blackpearl" Date: Fri, 19 Feb 2021 23:05:56 +0100 Subject: [PATCH 1/4] Added support for retrieving CUPS job number and amount of printed pages --- .../PrintConnectors/CupsPrintConnector.php | 40 ++++++++++++++++++- src/Mike42/Escpos/Printer.php | 28 +++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/src/Mike42/Escpos/PrintConnectors/CupsPrintConnector.php b/src/Mike42/Escpos/PrintConnectors/CupsPrintConnector.php index 3ef25c8..5234bfc 100644 --- a/src/Mike42/Escpos/PrintConnectors/CupsPrintConnector.php +++ b/src/Mike42/Escpos/PrintConnectors/CupsPrintConnector.php @@ -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. @@ -91,13 +105,37 @@ public function finalize() escapeshellarg($tmpfname) ); try { - $this->getCmdOutput($cmd); + $response = $this->getCmdOutput($cmd); + $exploded = explode(" ", $response); + $jobName = explode("-", $exploded[3]); + $this->jobNumber = intval($jobName[count($jobName) - 1]); + $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. diff --git a/src/Mike42/Escpos/Printer.php b/src/Mike42/Escpos/Printer.php index 05c8a6f..b7082c8 100644 --- a/src/Mike42/Escpos/Printer.php +++ b/src/Mike42/Escpos/Printer.php @@ -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 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 CupsPrintConnector) { + return null; + } + + return $this -> connector -> getAmountPages(); + } /** * Wrapper for GS ( k, to calculate and send correct data length. From 31fe11298c4d5ab895843ac24f1cbdcf844f541c Mon Sep 17 00:00:00 2001 From: "Max@Blackpearl" Date: Fri, 19 Feb 2021 23:16:55 +0100 Subject: [PATCH 2/4] Fixed namespace location of CupsPrintConnector inside of Printer.php --- src/Mike42/Escpos/Printer.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Mike42/Escpos/Printer.php b/src/Mike42/Escpos/Printer.php index b7082c8..98a5ad1 100644 --- a/src/Mike42/Escpos/Printer.php +++ b/src/Mike42/Escpos/Printer.php @@ -1033,7 +1033,8 @@ public function textRaw(string $str = "") */ public function getJobNumber() { - if (! $this -> connector instanceof CupsPrintConnector) { + if (! $this -> connector instanceof \Mike42\Escpos\PrintConnectors\CupsPrintConnector) { + echo "Connector is not cupsprintconnector\n"; return null; } @@ -1047,7 +1048,8 @@ public function getJobNumber() */ public function getAmountPages() { - if (! $this -> connector instanceof CupsPrintConnector) { + if (! $this -> connector instanceof \Mike42\Escpos\PrintConnectors\CupsPrintConnector) { + echo "connector is not cupsprintconnector\n"; return null; } From c8a808491bb4e68d84da0ec4a2f891b8e5440d1d Mon Sep 17 00:00:00 2001 From: "Max@Blackpearl" Date: Fri, 19 Feb 2021 23:30:36 +0100 Subject: [PATCH 3/4] Removed debug statements --- src/Mike42/Escpos/Printer.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Mike42/Escpos/Printer.php b/src/Mike42/Escpos/Printer.php index 98a5ad1..68716be 100644 --- a/src/Mike42/Escpos/Printer.php +++ b/src/Mike42/Escpos/Printer.php @@ -1034,7 +1034,6 @@ public function textRaw(string $str = "") public function getJobNumber() { if (! $this -> connector instanceof \Mike42\Escpos\PrintConnectors\CupsPrintConnector) { - echo "Connector is not cupsprintconnector\n"; return null; } @@ -1049,7 +1048,6 @@ public function getJobNumber() public function getAmountPages() { if (! $this -> connector instanceof \Mike42\Escpos\PrintConnectors\CupsPrintConnector) { - echo "connector is not cupsprintconnector\n"; return null; } From 2dd9377983a53fbcf19ef16a8a8fc301e6ad37aa Mon Sep 17 00:00:00 2001 From: Max Winsemius Date: Fri, 4 Jun 2021 14:51:02 +0200 Subject: [PATCH 4/4] Fixed test in which no data is returned for the print command --- .../PrintConnectors/CupsPrintConnector.php | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/Mike42/Escpos/PrintConnectors/CupsPrintConnector.php b/src/Mike42/Escpos/PrintConnectors/CupsPrintConnector.php index 5234bfc..f28e7d7 100644 --- a/src/Mike42/Escpos/PrintConnectors/CupsPrintConnector.php +++ b/src/Mike42/Escpos/PrintConnectors/CupsPrintConnector.php @@ -107,9 +107,19 @@ public function finalize() try { $response = $this->getCmdOutput($cmd); $exploded = explode(" ", $response); - $jobName = explode("-", $exploded[3]); - $this->jobNumber = intval($jobName[count($jobName) - 1]); - $this->amountPages = intval(substr($exploded[4], 1)); + + // 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;