├── LICENSE
├── Output_CLI.php
├── README.md
├── command_line.php
├── php_printer.dll
├── printer.class.php
└── test_dll.php
/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (c) 2011, Jiminald
2 | All rights reserved.
3 |
4 | Redistribution and use in source and binary forms, with or without
5 | modification, are permitted provided that the following conditions are met:
6 | 1. Redistributions of source code must retain the above copyright
7 | notice, this list of conditions and the following disclaimer.
8 | 2. Redistributions in binary form must reproduce the above copyright
9 | notice, this list of conditions and the following disclaimer in the
10 | documentation and/or other materials provided with the distribution.
11 | 3. All advertising materials mentioning features or use of this software
12 | must display the following acknowledgement:
13 | This product includes software developed by Jiminald.
14 | 4. Neither the name of Jiminald nor the
15 | names of its contributors may be used to endorse or promote products
16 | derived from this software without specific prior written permission.
17 |
18 | THIS SOFTWARE IS PROVIDED BY Jiminald ''AS IS'' AND ANY
19 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 | DISCLAIMED. IN NO EVENT SHALL Jiminald BE LIABLE FOR ANY
22 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 |
--------------------------------------------------------------------------------
/Output_CLI.php:
--------------------------------------------------------------------------------
1 |
9 | * @copyright Jiminald 18/May/2010
10 | * @package 3CoreFrame
11 | * @subpackage libraries
12 | * @version 1.0
13 | */
14 |
15 | class ThreeCore_Output_CLI {
16 | public $screenDateEol = array('date' => TRUE, 'eol' => TRUE);
17 | public $screenDateOnly = array('date' => TRUE, 'eol' => FALSE);
18 | public $screenEolOnly = array('date' => FALSE, 'eol' => TRUE);
19 | public $screenNothing = array('date' => FALSE, 'eol' => FALSE);
20 |
21 | public function __construct() {
22 | ob_start();
23 | }
24 |
25 | public function screen($message, $options = array('date' => TRUE, 'eol' => TRUE)) {
26 | if ($options['date'] == TRUE) { echo '['.date('d/M/Y H:i:s').'] '; }
27 | echo $message;
28 | if ($options['eol'] == TRUE) { echo PHP_EOL; }
29 | ob_flush();
30 | flush();
31 | sleep(1);
32 | }
33 |
34 | public function __destroy() {
35 | ob_end_clean();
36 | }
37 |
38 | } //End of class
39 | ?>
40 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # PHP Printer
2 |
3 | PHP Printer has been developed for Web and Command Line Applications that require direct access to Printers.
4 |
5 | It is built upon the php_printer.dll found in the PHP PECL repositories.
6 | There was no release found in PECL, so php_printer.dll was downloaded from http://downloads.php.net/pierre/
7 |
8 |
9 | ## Requirements
10 |
11 | Before you install, make sure you have met the dependencies:
12 |
13 | Windows XP or above
14 | PHP 5.3.x or above
15 | Installed php_printer.dll in you php.ini
16 |
17 |
18 | ## How to Install
19 |
20 | Copy php_printer.dll to your PHP\ext folder
21 | add the line to your php.ini file:
22 | extension=php_printer.dll
23 |
24 |
25 | ## License
26 |
27 | Read LICENSE file
28 |
29 |
30 | ## Documentation
31 |
32 | Read the notes in the code.
33 |
34 |
35 | ## Known Issues
36 |
37 | None so far
38 |
39 |
40 | ## Contributing
41 |
42 | Feel free to fork this project and enhance it as you see fit.
43 | Push back anything, it will all be considered
44 |
--------------------------------------------------------------------------------
/command_line.php:
--------------------------------------------------------------------------------
1 | screen('Loading internal list of Printers');
18 | #print_r($printer->enumerate());
19 |
20 | $Output->screen('Select Printer. It is: '.$printer->select('PRINTER NAME'));
21 | $Output->screen('Setting Document Title to: '.$printer->document_title('PHP Printer Interface'));
22 | #$Output->screen('Setting Orientation to: '.$printer->orientation('portrait'));
23 | #$Output->screen('Setting Copies to : '.$printer->copies(2));
24 | $printer->write('Success!
PHP Printer Test Page.');
25 |
26 | $Output->screen('ACTUALLY Printing Document');
27 | $printer->print_buffer();
28 | ?>
29 |
--------------------------------------------------------------------------------
/php_printer.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jiminald/PHP-Printer/9acf25ac09526107c2c89327d05383b9e601971f/php_printer.dll
--------------------------------------------------------------------------------
/printer.class.php:
--------------------------------------------------------------------------------
1 |
10 | * @copyright Jiminald October 2011
11 | * @version 1.0
12 | */
13 |
14 | class Printer {
15 | //Global Variables
16 |
17 | //Public Variables
18 | public $Output = NULL;
19 | public $printers = array();
20 | public $ignore = array();
21 | //Private Variables
22 | private $handle = FALSE;
23 | private $selected = FALSE;
24 | private $copies = 1;
25 | private $orientation = 'Portrait';
26 | private $document_title = '';
27 | private $buffer = "\r\n ";
28 |
29 | /**
30 | * Set ignore list and scan for printers
31 | *
32 | * @return void
33 | */
34 | public function __construct() {
35 | //Set ignore list
36 | $this->ignore = array('Fax', 'Microsoft XPS Document Writer', 'Microsoft Office Document Image Writer');
37 |
38 | //Grab printer list
39 | $this->enumerate();
40 | } //End of function "__construct"
41 |
42 | /**
43 | * Show Printers Found Connected to this PC
44 | *
45 | * @return array
46 | */
47 | public function enumerate() {
48 | /* If there are no printers, find them */
49 | if (count($this->printers) == 0) {
50 | $printer_list = printer_list(PRINTER_ENUM_LOCAL);
51 |
52 | foreach ($printer_list as $printer) {
53 | if (in_array($printer['NAME'], $this->ignore) == FALSE) {
54 | $this->printers[] = $printer;
55 | }
56 | }
57 | }
58 |
59 | return $this->printers;
60 | } //End of function "enumerate"
61 |
62 | /**
63 | * Select a Printer to Print to
64 | *
65 | * @param string $printer_name Name of Printer
66 | * @return string
67 | */
68 | public function select($printer_name = '') {
69 | /* If no printer specified, return FALSE */
70 | if ($printer_name == '') { return FALSE; }
71 |
72 | /* Scan the Printers array, if the printer asked for is found, then select it, reset values to default and return TRUE */
73 | foreach ($this->printers as $printer) {
74 | if ($printer['NAME'] == $printer_name) {
75 | $this->selected = $printer_name;
76 | $this->copies = 1;
77 | $this->orientation = 'Portrait';
78 | $this->document_title = '';
79 | }
80 | }
81 |
82 | /* Return Printer Name, or return FALSE */
83 | return $this->selected;
84 | } //End of function "select"
85 |
86 | /**
87 | * Set copy amount
88 | *
89 | * @param integer $copies Copy Amount
90 | * @return integer
91 | */
92 | public function copies($copies = 0) {
93 | /* If it is not 0, set the copies */
94 | if ($copies <> 0) {
95 | $this->copies = $copies;
96 | }
97 |
98 | /* Return the current value, changed or not */
99 | return $this->copies;
100 | } //End of function "copies"
101 |
102 | /**
103 | * Set Page Orientation
104 | *
105 | * @param string $orientation Page Orientation
106 | * @return string
107 | */
108 | public function orientation($orientation = '') {
109 | /* If it is not blank, set the orientation */
110 | if ($orientation <> '') {
111 | $this->orientation = ucwords(strtolower($orientation));
112 | }
113 |
114 | /* Return the current value, changed or not */
115 | return $this->orientation;
116 | } //End of function "orientation"
117 |
118 | /**
119 | * Document Title, Or Filename if printing to PDF Printer
120 | *
121 | * @param string $title Page title
122 | * @return string
123 | */
124 | public function document_title($title = '') {
125 | /* If it is not blank, set the document Title */
126 | if ($title <> '') {
127 | $this->document_title = $title;
128 | }
129 |
130 | /* Return the current value, changed or not */
131 | return $this->document_title;
132 | } //End of function "document_title"
133 |
134 | /**
135 | * Write string to Print Buffer
136 | *
137 | * @param string $string Data to save in buffer
138 | * @return boolean
139 | */
140 | public function write($string) {
141 | if ($string == '') { return FALSE; }
142 |
143 | //Replace
to CRLF
144 | $string = str_replace(array('
', '
'), "\r\n ", $string);
145 |
146 | $this->buffer .= $string;
147 | return TRUE;
148 | } //End of function "write"
149 |
150 | /**
151 | * Print out whats in the buffer
152 | *
153 | * @return boolean
154 | */
155 | public function print_buffer() {
156 | $connect = $this->_connect();
157 | if ($connect <> FALSE) {
158 | $this->_write_buffer();
159 | $this->_close();
160 | return TRUE;
161 | }
162 |
163 | return FALSE;
164 | } //End of function "print_buffer"
165 |
166 | /**
167 | * Print file, this adds the file to the buffer and prints
168 | *
169 | * @param string $file Filename to print
170 | * @return boolean
171 | */
172 | public function print_file($file) {
173 | /* Get file contents */
174 | $fh = fopen($file, "rb");
175 | $content = fread($fh, filesize($file));
176 | fclose($fh);
177 |
178 | $connect = $this->_connect();
179 | if ($connect <> FALSE) {
180 | // Send File to printer
181 | $this->write($content);
182 | $this->_write_buffer();
183 | $this->_close();
184 | return TRUE;
185 | }
186 | } //End of function "print_file"
187 |
188 | /**
189 | * Open Printer Connection and set preferences
190 | *
191 | * @return boolean|resource
192 | */
193 | private function _connect() {
194 | //Check if the printer is already open, if it is, return the handle
195 | if ($this->handle <> FALSE) { return $this->handle; }
196 |
197 | /* Open the Printer Connetion */
198 | $this->handle = printer_open($this->selected);
199 | if ($this->handle == FALSE) {
200 | return FALSE;
201 | }
202 |
203 | /* Set Copies */
204 | $this->option(PRINTER_COPIES, $this->copies());
205 |
206 | /* Set Orientation */
207 | if ($this->orientation() == 'Landscape') {
208 | $this->option(PRINTER_ORIENTATION, PRINTER_ORIENTATION_LANDSCAPE);
209 | } else {
210 | $this->option(PRINTER_ORIENTATION, PRINTER_ORIENTATION_PORTRAIT);
211 | }
212 |
213 | /* Set Title */
214 | if ($this->document_title() <> '') {
215 | $this->option(PRINTER_TITLE, $this->document_title());
216 | }
217 |
218 | return $this->handle;
219 | } //End of function "_connect"
220 |
221 | /**
222 | * Set printer option, printer must be open for this to work
223 | *
224 | * @param integer $option Option to Set
225 | * @param integer $value Value to give
226 | * @return boolean
227 | */
228 | public function option($option, $value) {
229 | return printer_set_option($this->handle, $option, $value);
230 | } //End of function "option"
231 |
232 | /**
233 | * Write buffer contents to Printer
234 | *
235 | * @return void
236 | */
237 | private function _write_buffer() {
238 | printer_write($this->handle, $this->buffer);
239 | return;
240 | } //End of function "_write_buffer"
241 |
242 | /**
243 | * Close printer connection
244 | *
245 | * @return boolean
246 | */
247 | private function _close() {
248 | printer_close($this->handle);
249 | $this->handle = FALSE;
250 | return TRUE;
251 | } //End of function "_close"
252 |
253 | } //End of Class "Printer"
254 | ?>
255 |
--------------------------------------------------------------------------------
/test_dll.php:
--------------------------------------------------------------------------------
1 |
27 |
--------------------------------------------------------------------------------