Printing Directly From PHP

Some of you might be surprised that you can print directly to a printer of your choice through PHP. This uses a PECL extension but is only available on Windows. It is possible to print using UNIX systems, but you have to call the ps program using the system function.

To install the printer functions on Windows you need to download the PECL library for Windows, found at pecl4win.php.net. Unzip this file and move the file php_printer.dll to your extensions directory in your PHP directory (usually called ext).

Next, open up your php.ini file and add the following line at the end of the extensions list.

extension=php_printer.dll

Then add the following lines at the bottom of the file. This tells PHP what printer to use as a default. If the printer is locally installed then PHP only needs the name.

[printer]
printer.default_printer = "Your printer name"

If the printer is installed on another computer then you need to supply the full address. This would look something like \\server\printer.

Save this and restart the web server. You should now have access to the printer functions. Make sure your printer is on, and that you have enough printer ink and you can get started.

To use these function to need to open the printer, start a document, and then start a page. You can have more than one page by ending and starting pages. Here is the basic model you will need to get started.

// start printer
$handle = printer_open();
printer_start_doc($handle, "My Document");
printer_start_page($handle);
// create content here
// print
printer_end_page($handle);
printer_end_doc($handle);
printer_close($handle);

To create some content you need to call certain functions using the printer handle as a parameter. You can print text, lines, shapes and even images using a number of different functions. To print text you need to use the printer_draw_text() function, the first parameter is the printer handle, the second is the text to be printed and the last two are the x and y coordinates of the text on the page.

printer_draw_text($handle, 'the text that will be printed', 100, 100);

Of course, you will probably want to use a font to print with, in which case use the following additions to the code.

$font = printer_create_font("Arial", 72, 48, 400, false, false, false, 0);
printer_select_font($handle, $font);
printer_draw_text($handle, 'the text that will be printed', 100, 100);
printer_delete_font($font);

Drawing a line is nice and easy

$pen = printer_create_pen(PRINTER_PEN_SOLID, 30, "123fde");
printer_select_pen($handle, $pen);
 
printer_draw_line($handle, 1, 10, 1000, 10);
// draw more lines if you want...
 
printer_delete_pen($pen);

These are just two examples, there are a number of other functions available on the PHP Printer page.

Comments

can i do it on linux? directly printing from PHP just like using php_printer.dll in windows. my webserver is linux, and i have a problem because my script printer doesn't work. need help...thx
Permalink
can i do it on linux? directly printing from PHP just like using php_printer.dll in windows. my webserver is linux, and i have a problem because my script printer doesn't work. need help...thx
Permalink
but you have to call the ps program using the system function.
what dose this means ? can you explain please ? =) thanks
Permalink
Hi ! thank for that new but I've one question: is it possible to use this php extension control the print jobs which has been made through any windows driver instead ? in such a way that the extension help to save data (printer name, number of printed documents...) related to any print job automatically.
Permalink
Fatal error: call to undefined function printer_open()
Permalink
So you have no printer_open() function as this is provided by the Printer PECL package (https://pecl.php.net/package/printer). Have you read the post or are you just expecting random code snippets to work out of the box?
Name
Philip Norton
Permalink
i guess i must be using wrong Printer.dll based on my php version 5.3.5
Permalink
Maybe it's not being loaded? Have you checked your php info output?
Name
Philip Norton
Permalink
Using php printer functions - I am getting the error: Fatal error: Out of memory (allocated 3407872) (tried to allocate 4294967295 bytes) on the line that calls the printer_open() function. The printer exists and is turned on. I am calling the function with the printer's name as parameter, i.e. printer_open('PRINTER 1'). This is the exact name of the printer as it appears in the devices and printers list on the server where the script is running. I've tried adding an IP addess in front and various permutations which then result in a 'printer not found' error. I can view the printer queue for this printer in the command line. Nothing is actually getting sent to the printer as the script terminates with the error at the printer_open() line. I've been googling a lot and tried altering permissions on the printer. Nothing has worked so far. The server is running IIS and windows 2007. The printer function shows up in phpinfo as below: Printer Support enabled Version 0.1.0-dev Default printing device ,,, Module state working RCS Version $Id: printer.c 331404 2013-09-15 10:51:05Z ab $ Can anyone offer any suggestions?
Permalink
Did any one solve this problem? my web server is a Linux and I need just to print one single line of text on my local printer silently. is there any printer extensions under Linux (*.so files). Any help or sample code will be highly appreciated.
Permalink
As you are on Linux you could try to use the lpr command to print using shell_exec().shell_exec('lpr file.txt');Might be worth a shot?
Name
Philip Norton
Permalink
how can i print load page
Permalink
I am going to give this a try.
Permalink
I need to print on a custom paper size in PHP. The Custom paper size should be selected when I print, for example an invoice, after the printing is done, this paper size should be deselected. Thanks for the help
Permalink

Printing directly From PHP make is possible to let the user print a document on his own printer. But PHP is completely server-side. So this would mean using the printer-functions, you can only print on devices connecting to the server.

Permalink

Are you really serious?

Your code is nice and only meant for the developer.

How do you expect me to know the names of the all the printers for all the clients that use my app?

If there was another way, I could eh.., have my clients input or select their printer from the list of printers they have on their machines, then pass that value to the php.ini file, that would be fine.

Meanwhile, even with that, writing a code to draw-text and draw lines for every bit of row and column to be printed, is far-too-cumbersome.

Where does the CSS styling come in?

Thank you

Permalink

> Are you really serious?

Well, yes. I mean I used it for a while back in 2009. Haven't really had the need to use it since then though. I think the package has been made largely redundant for a good while now, not even sure it will work with PHP8. Since this is Windows only I haven't had any way of running this code for at least 8 years.

> Your code is nice and only meant for the developer.

Yes. It is only meant for the developer. Sorry if this isn't clear.

> How do you expect me to know the names of the all the printers for all the clients that use my app?

I think you might be mistaken. Since PHP is a server side language you should absolutely know what printer you are going to print to as it will be connected to your server.

> If there was another way, I could eh.., have my clients input or select their printer from the list of printers they have on their machines, then pass that value to the php.ini file, that would be fine.

You could allow your users to select from a list of printers, but the printer would need to be available from the server you are running your PHP on. If the printer is connected to the server via a network then you could potentially use printer_open() with the name of the printer you are trying to open as the paramter.

> Meanwhile, even with that, writing a code to draw-text and draw lines for every bit of row and column to be printed, is far-too-cumbersome.

True, but if you are printing something simple like a shopping receipt then this would be ideal.

> Where does the CSS styling come in?

Well, it doesn't as CSS is never interpreted by PHP to render the printer output. It sounds like you are trying to print from a browser, in which case this code would never be run as it would be the browser doing the printing.

Name
Philip Norton
Permalink

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
12 + 5 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.