Send Print Jobs Directly from PHP

Published: May 19th, 2010 by:

I recently learned that you can send print jobs directly from a PHP server. How cool is that? This article will cover sending print jobs from a PHP server to a printer on the same LAN. This does not mean that someone viewing a page over the Internet can have something print on their local printer with these functions. This does mean if you have PHP running on a Windows server in your internal network, you can print to printers that are configured on that Windows server only.


This article really only applies if you are hosting the web server in your local office or place of business. If you are hosting your web server in a data center somewhere and you never see it, this article is probably not for you. In the diagram below, pretend the server and attached printers are in your office building somewhere, and the computers in the red box are your clients out on the Internet somewhere. Note that the printers at the client side are not used in this application.

Another possible setup would be if everything is on a private internal network, like the diagram below. In this case everything is in the green box because it is all on an internal network joined by a network switch. The two printers are in yellow boxes because they are only visible to the server if shared from those attached network computers, and the computers are turned on.

So now that we have the big picture, there are a couple other things to note. The web server needs to be running Windows, not Linux. The PHP Printer functions are Windows only. The printer(s) can be connected locally via USB/Parallel, over the network via IP JetDirect, or shared off of another server/computer.

Printing Plain Text Documents

Printing plain text documents are a piece of cake. It’s going to be similar to echoing text out to the web browser with a couple differences. This is the first test I did:

<?php
	/* get the sample text */
	$lipsum = file_get_contents('lipsum.txt');
	
	/* open a connection to the printer */
	$printer = printer_open("Lexmark X850e XL V");
	
	/* write the text to the print job */
	printer_write($printer, $lipsum);
	
	/* close the connection */
	printer_close($printer);
?>

The printer in line 6 is in my windows printer list, and has to be matched exactly. That function can be called without a printer parameter, and PHP will use the default printer specified in php.ini or try to detect it based on how it is set in Windows. I wanted to do close to a full page of text, so I went to lipsum.com and generated some sample text and saved it to a text file.

So here is the printed result, and the cool thing is that the text was almost exactly a full page. The not-so-cool thing was that it doesn’t do simple things like word wrap. Luckily, we can run the text through wordwrap() before sending it to the printer to easily fix that.

For future examples, I will be using my PDFCreator printer so I don’t waste paper (or money) during testing, and I encourage you to do the same.

Printing a Custom Font

If you want to print with a custom font, it is done using functions that are similar to the GD Library functions. You need to define the font, X/Y position, and other font weight properties.

In this example, I’m going to use the freely available barcode font to print a barcode on a piece of paper. In this example, I am also going to define the printer document and the individual page.

<?php
	/* get the sample text */
	$lipsum = file_get_contents('lipsum.txt');
	
	/* open a connection to the printer */
	$printer = printer_open("PDFCreator");
	printer_start_doc($printer, "Doc");
	printer_start_page($printer);
	
	/* font management */
	$barcode = printer_create_font("Free 3 of 9 Extended", 400, 200, PRINTER_FW_NORMAL, false, false, false, 0);
	$arial = printer_create_font("Arial", 148, 76, PRINTER_FW_MEDIUM, false, false, false, 0);
	
	/* write the text to the print job */
	printer_select_font($printer, $barcode);
	printer_draw_text($printer, "*123456*", 50, 50);
	printer_select_font($printer, $arial);
	printer_draw_text($printer, "123456", 250, 500);
	
	/* font management */
	printer_delete_font($barcode);
	printer_delete_font($arial);
	
	/* close the connection */
	printer_end_page($printer);
	printer_end_doc($printer);
	printer_close($printer);
?>

Drawing Images

The printer functions only support writing bitmaps (bmp), so you will have to convert the images to that format before using it in this application. The X,Y parameters in the printer_draw_bmp() function are in inches.

<?php
	/* open a connection to the printer */
	$printer = printer_open("PDFCreator");
	printer_start_doc($printer, "Doc");
	printer_start_page($printer);
	
	/* draw the image to the page */
	printer_draw_bmp($printer, "c:\\path\\to\\image.bmp", 1, 1);
	
	/* close the connection */
	printer_end_page($printer);
	printer_end_doc($printer);
	printer_close($printer);
?>

Drawing Shapes

Several shapes can be drawn as well. The example will show two horizontal & parallel lines of different length.

<?php
	/* open a connection to the printer */
	$printer = printer_open("PDFCreator");
	printer_start_doc($printer, "Doc");
	printer_start_page($printer);
	
	/* create the pen handle and set some properties */
	$pen = printer_create_pen(PRINTER_PEN_SOLID, 30, "000000");
	printer_select_pen($printer, $pen);
	
	/* draw some lines on the page */
	printer_draw_line($printer, 1, 100, 1000, 100);
	printer_draw_line($printer, 1, 200, 500, 200);
	
	/* delete the pen handle */
	printer_delete_pen($pen);
	
	/* close the connection */
	printer_end_page($printer);
	printer_end_doc($printer);
	printer_close($printer);
?>

Practical Uses

Couple uses come to mind, and I’m sure there are others (post them if you have a good one!).

  • Automatically print packing slips / invoices at a shipping center as customers place orders.
  • On an intranet website, have all the company printers configured at the server side so users can use the web application w/o worrying about setting up the printers at their workstation.
  • Automatically printing out reports generated by a CLI PHP script

For more information, check out PHP print functions.

Printing from PHP on Linux Servers

“But what about Linux servers!”, you say. Well, as previously stated, the PHP functions don’t support anything other than Windows, but there is an alternative. PHP4IT.com has an article titled Printing a form directly to a PCL printer (Linux) that should help you with manually building the Postscript file and writing it through to the printer. That should help get you started.


4 Responses to “Send Print Jobs Directly from PHP”

  • costner

    Hi, thanks for the overview of the printer functions. =) Would love your advice on printing to shared printers; I can print to my server’s printer just fine, but am unable to print to any shared printer… Printer_Open(“\\\\hostpcname\\printername“) keeps returning an unable to be connected error. =(

    I’m using Windows Server 2008, with php 5.3.8 and apache.

    Would be extremely grateful if you could point me in the right direction. Thanks!

     

  • Anant Dhiman

    Thank you very much for this great learning, everything is working fine as per.
    I just to ask you one thing, when i am trying to use printer_list(PRINTER_ENUM_LOCAL | PRINTER_ENUM_SHARED) for detecting printers attached with my system, then it always give me an error  apache error.
     
    It would be great if you please let me know How to detect attached printers
     
    Thanks again.

     

  • Raj

    Dear 
    I’m trying this print examples but i’m continue getting this error
    Call to undefined function printer_open() in C:\wamp\www\PrintTest.php on line 6
    I have download Php_printer.dll
    and updated my php.ini file but it’s not loading 
    extension=php_printer.dll
     
    C:\wamp\bin\php\php5.3.10\ext
     
    can you please help me how to fix this problem.
    Apache Version :2.2.21  PHP Version :5.3.10  windows 7

     

  • Andrew

    I set this up and everything installed. However, I don’t get any errors and nor do I get any output…no file. I added PDFCreator to my system too. Again no errors, but I can’t seem to find the PDF the should have been created. Any ideas?

     

Leave a Reply





Wordpress doesn't like it when you post PHP code. Go save your code at pastebin, and post the link here.

About the Author

Andrew has been coding PHP applications since 2006, and has plenty of experience with PHP, MySQL, and Apache. He prefers Ubuntu Linux on his desktop and has plenty of experience at managing CentOS web servers. He is the owner of Wells IT Solutions LLC, and develops PHP applications full time for anyone that needs it as well as does desktop computer support locally in the local area. He spends most of his free time exploring new programming concepts and posting on The Webmaster Forums.