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.
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.
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 ? =) thanks
shell_exec('lpr file.txt');Might be worth a shot?
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.
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?
> 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.