Quantcast
Channel: PHP Starter » Andrew
Viewing all articles
Browse latest Browse all 10

Send Print Jobs Directly from PHP

$
0
0

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.


Viewing all articles
Browse latest Browse all 10

Trending Articles