Skip to content

Commit 0ef2d38

Browse files
committed
- print send
1 parent 97d6b62 commit 0ef2d38

File tree

3 files changed

+181
-0
lines changed

3 files changed

+181
-0
lines changed

print_send.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/*
3+
* Class Print_send - Abstract printing class.
4+
* Models the actual sending of data to the printer.
5+
*/
6+
7+
abstract class Print_send{
8+
9+
protected $data;
10+
protected $debug;
11+
12+
abstract protected function print_job($queue);
13+
14+
abstract protected function interpret();
15+
16+
public function __construct(){}
17+
18+
public function set_data($data){
19+
//This can be a filename or some ASCII. A file check should be made in derived classes.
20+
$this->data = $data;
21+
$this->debug .= "Data set\n";
22+
}
23+
24+
//Future functionality (?) Could create filters with this for different languages like PCL or ESC/P
25+
public static function printer_factory($type){
26+
if (include_once 'drivers/' . $type . '.php') {
27+
return new $type; // Return a new instance of a printer driver of type $type.
28+
} else {
29+
throw new Exception('Driver not found: $type');
30+
}
31+
}
32+
33+
public function get_debug(){
34+
return $this->debug;
35+
}
36+
37+
}

print_send_lpr.php

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<?php
2+
/*
3+
* Class Print_send_lpr - Sends a file to be printed remotely.
4+
* File must be either preformatted or plain ASCII until some
5+
* printer driver functionality has been incorpoated.
6+
*/
7+
8+
class Print_send_lpr extends Print_send{
9+
10+
private $host = "192.168.1.1";
11+
private $port = "6001";
12+
private $timeout = "5";
13+
private $err_no;
14+
private $err_str;
15+
16+
public function __construct() {
17+
parent::__construct();
18+
}
19+
20+
public function set_port($port){
21+
$this->port = $port;
22+
$this->debug .= "Port is ".$this->port."\n";
23+
}
24+
25+
public function set_host($host){
26+
$this->host = $host;
27+
$this->debug .= "Host is ".$this->host."\n";
28+
}
29+
30+
public function set_timeout($timeout){
31+
$this->timeout = $timeout;
32+
}
33+
34+
public function get_err_no(){
35+
return $this->err_no;
36+
}
37+
38+
public function get_err_str(){
39+
return $this->err_str;
40+
}
41+
42+
public function print_job($queue){
43+
//Private static function prints waiting jobs on the queue.
44+
$this->print_waiting($queue);
45+
46+
//Open a new connection to send the control file and data.
47+
$stream = stream_socket_client("tcp://".$this->host.":".$this->port, $this->err_no, $this->err_str, $this->timeout);
48+
49+
if(!$stream){
50+
return $this->err_no." (".$this->err_str.")";
51+
} else {
52+
$job = self::get_job_id(); // Get a new id for this job
53+
54+
//Set printer to receive file
55+
fwrite($stream, chr(2).$queue."\n");
56+
$this->debug .= "Confirmation of receive cmd: ".ord(fread($stream, 1))."\n";
57+
58+
//Send Control file.
59+
(isset($_SERVER['SERVER_NAME'])) ? $server = $_SERVER['SERVER_NAME'] : $server = "me";//Might be CLI and not have _SERVER
60+
$ctrl = "H".$server."\nPphp\nfdfA".$job.$server."\n";
61+
fwrite($stream, chr(2).strlen($ctrl)." cfA".$job.$server."\n");
62+
$this->debug .= "Confirmation of sending of control file cmd: ".ord(fread($stream, 1))."\n";
63+
64+
fwrite($stream, $ctrl.chr(0)); //Write null to indicate end of stream
65+
$this->debug .= "Confirmation of sending of control file itself: ".ord(fread($stream, 1))."\n";
66+
67+
if (is_readable($this->data)){
68+
// It's a filename, rather than just some ascii text that needs printing. Open and stream.
69+
if (strstr(strtolower($_ENV["OS"]), "windows")){
70+
$this->debug .= "Operating system is Windows\n";
71+
$data = fopen($this->data, "rb"); // Force binary in Windows.
72+
} else {
73+
$this->debug .= "Operating system is not Windows\n";
74+
$data = fopen($this->data, "r");
75+
}
76+
77+
fwrite($stream, chr(3).filesize($this->data)." dfA".$job.$server."\n");
78+
$this->debug .= "Confirmation of sending receive data cmd: ".ord(fread($stream, 1))."\n";
79+
80+
while(!feof($data)){
81+
fwrite($stream, fread($data, 8192));
82+
}
83+
84+
fwrite($stream, chr(0)); // Write null to indicate end of stream
85+
$this->debug .= "Confirmation of sending data: ".ord(fread($stream, 1))."\n";
86+
87+
fclose($data);
88+
} else {
89+
//Send data string
90+
fwrite($stream, chr(3).strlen($this->data)." dfA".$job.$server."\n");
91+
$this->debug .= "Confirmation of sending receive data cmd:".ord(fread($stream, 1))."\n";
92+
93+
fwrite($stream, $this->data.chr(0)); //Write null to indicate end of stream
94+
$this->debug .= "Confirmation of sending data:".ord(fread($stream, 1))."\n";
95+
}
96+
}
97+
98+
}
99+
100+
//Placeholder to handle filters - this will call methods in printer driver classes
101+
//instantiated by the printer_factory method..
102+
public function interpret(){}
103+
104+
private function get_job_id(){
105+
return "001";
106+
107+
//You should implement your own internal storage and incrementing of job numbers here.
108+
//This might be a database value, or some file on the file system.
109+
}
110+
111+
private function print_waiting($queue){
112+
$stream = stream_socket_client("tcp://".$this->host.":".$this->port, $this->err_no, $this->err_str, $this->timeout);
113+
114+
if (!$stream){
115+
return $this->err_no." (".$this->err_str.")";
116+
} else {
117+
//Print any waiting jobs
118+
fwrite($stream, $queue."\n");
119+
$this->debug .= "Confirmation of print waiting jobs cmd:";
120+
121+
//while(!feof($stream)){
122+
//$this->debug .= ord(fread($stream, 1));
123+
//}
124+
125+
fclose($stream);
126+
$this->debug .= "\n";
127+
}
128+
}
129+
130+
}

test.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
error_reporting(E_ALL);
3+
4+
include_once("print_send.php");
5+
include_once("print_send_lpr.php");
6+
7+
$text = "Hello world!"; // zpl/epl/ipl/gpl formatted label view
8+
9+
$lpr = new Print_send_lpr();
10+
$lpr->set_port("6001");
11+
$lpr->set_host("192.168.1.1");
12+
$lpr->set_data($text);
13+
$lpr->print_job("Queue 1");
14+
$lpr->get_debug();

0 commit comments

Comments
 (0)