Printing Directly From PHP

Thursday, January 22, 2009 - 17:39

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 Unzip this file and move the file php_printer.dll to your extensions directory in your PHP dicrectory (usually called ext).

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


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.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");
// create content here
// print

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);

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...

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

philipnorton42's picture

Philip Norton

Phil is the founder and administrator of #! code and is an IT professional working in the North West of the UK.
Google+ | Twitter


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

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

but you have to call the ps program using the system function.

what dose this means ? can you explain please ? =)

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.

Fatal error: call to undefined function printer_open()

i guess i must be using wrong Printer.dll based on my php version 5.3.5

philipnorton42's picture
Submitted by philipnorton42 on Thu, 10/08/2015 - 18:19

Maybe it's not being loaded? Have you checked your php info output?

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?

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.

philipnorton42's picture
Submitted by philipnorton42 on Tue, 06/21/2016 - 11:51

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?

how can i print load page

I am going to give this a try.

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

Add new comment