Create a Web-based printing solution with PHP and MagickWand for PHP
Takeaway: Phillip Perkins demonstrates how to create a Web-based printing solution using PHP and MagickWand for PHP. The solution will accept file uploads and automatically send the uploaded file to a printer.
Port 80—the standard port for HTTP communications—is no longer strictly a gateway for hypertext and Web images. Applications are communicating with each other via Web services using HTTP as the transport mechanism. In this article, I'll create a Web-based printing solution using PHP and MagickWand for PHP. The solution will accept file uploads and automatically send the uploaded file to a printer.
The printer functions in PHP make it possible to send data to a printer on Windows 9x, Me, NT, 2000, and XP operating systems by making API calls to Windows' underlying, unified printing system. (This functionality is not available on *nix systems.) Printer functions are part of the PECL extensions that aren't bundled with PHP, which means you'll need to download and add the php_printer.dll extension to the extensions directory defined in your PHP installation. You must also make available the extension by uncommenting the appropriate line in your PHP.ini file:
extension=php_printer.dll
After this is done, you can use the printer functions in your PHP code. To connect to a printer and create a device context (necessary for all graphics applications), you use the printer_open() function and pass an appropriate printer name as a parameter. You begin a print job with the function printer_start_doc().
This function accepts one or two parameters. The first parameter is the handle to the printer—the return value of the printer_open() function. The second optional parameter is the name of the print job. This is the document name that will appear in the queue list of the printer window. Printer_start_page() starts a page and accepts the printer handle as an argument. Finally, printer_end_page() ends the page and printer_end_doc() ends the print job. Both of these functions take the printer handle as an argument. When your print job is done, printer_close() closes the printer session and clears up resources.
Once you begin a print page, there are other functions available that allow you to send content to the printer that will appear on the page. The function we're particularly interested in is the printer_draw_bmp() function. This function accepts up to six arguments. The first four required arguments are the printer handle, the BMP file path, and the X and Y pixel coordinates for the origin of the positioned image, respectively. The final two optional arguments are the width and height of the printed image, respectively.
Now that we know the functions we need to send content to the printer, we need to make available a file for the PHP script to print. This is done by allowing a file upload from the originating HTML page. Through the use of the file INPUT form element, we can upload a file from the client machine to the server. In the simplest form, the following HTML will do the trick:
<html>
<body>
<form enctype="multipart/form-data" action="print.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="1048576" />
File to print: <input type="printfile" type="file" />
<input type="submit" value="Print File" />
</form>
</body>
</html>
Using file upload handling techniques in PHP, we simply get the contents of the file through the $_FILES['printfile']['tmp_name'] scalar. This will return the temporary file name for the uploaded file. PHP stores uploaded files in an upload folder and populates the $_FILES hash with information about the file(s). The individual files are referenced by the name you give to the file INPUT form element, which in this case is printfile.
Now the problem is that a user can send different types of images to the server, i.e., JPEG, BMP, GIF, TIFF, etc. In the case of my solution, I would like the user to be able to send multi-page TIFF images to the server. However, the printer_draw_bmp() function only accepts BMP images.
Next week, I'll show you how to use MagickWand for PHP to convert images to BMP images during the printing process, allowing us to use the printer_draw_bmp() function. Until that time, visit The PHP Group's site to check out the printer functions available for PHP.
Miss a column?
Check out the Web Development Zone archive, and catch up on the most recent editions of Phillip Perkins' column.
SponsoredWhite Papers, Webcasts, and Downloads
- What Is Micromanagement? And What You Can Do To Avoid It. Global Knowledge
- 2007 IT Salary and Skills Survey: What Impacts Salaries? Global Knowledge
- Switching Essentials Global Knowledge
Article Categories
- Security
- Security Solutions, IT Locksmith
- Networking and Communications
- E-mail Administration NetNote, Cisco Routers and Switches
- CIO and IT Management
- Project Management, CIO Issues, Strategies that Scale
- Desktops, Laptops & OS
- Windows 2000 Professional, Microsoft Word, Microsoft Excel, Microsoft Access, Windows XP,
- Data Management
- Oracle, SQL Server
- Servers
- Windows NT, Linux NetNote, Windows Server 2003
- Career Development
- Geek Trivia
- Software/Web Development
- Web Development Zone, Visual Basic, .NET





