2

I'm trying to set up a CLI PHP application to print a set of web pages to a default or specified printer. I'm on a Windows 7 machine with PHP 5.2.11 running in a CLI. To test the print functionality I've loaded PHP_printer.dll and I'm printing to Onenote, a print to file option, using the exact printer name given in PRINTER_ENUM_LOCAL.
Update: Here's the latest code:

$handle = printer_open("Send To OneNote 2010");
printer_start_doc($handle, "My Document");
printer_start_page($handle);

$filename='index.html';
$fhandle=fopen($filename, 'r');
$contents = fread($fhandle, filesize($filename));
fclose($fhandle);

printer_set_option($handle, PRINTER_MODE, "RAW");
printer_write($handle,$contents);

printer_end_page($handle);
printer_end_doc($handle);
printer_close($handle);

I've gotten this code to print a blank page to the correct printer, but I'm unable to print the strings I pass to printer_write. I confirmed that $contents is properly filled with the contents of my test html file. No matter what I provide as the second arg (string to be printed) I get a blank page. Is there something I'm missing to at least allow me to print some text onto a page?

Alternately is there a better way to do this (using PHP/javascript files)? What I am trying to do is print web pages as they appear (CSS included) via a CLI app, the web siteis written in PHP and I'm trying to minimize complexity. If there is a better way to print these (converting to PDF and printing is an option apparently) I'm open but it sounded like this was the simplest/de facto method in PHP.

Jens A. Koch
  • 39,862
  • 13
  • 113
  • 141
Ben Brocka
  • 2,006
  • 4
  • 34
  • 53

2 Answers2

2

As far as I know, you should use php_printer.dll. Check here. Then, add the php_printer.dll extension to your php.ini. If you have done that, then...

If you qualify and put clearly the ip/route in where the printer is installed, it should do the trick. Something like printer_open('\\\\xx.x.xx.xx\\_printername_');. (Local printers wouldn't need to add the server manually, afaik)

To check for printers, try something like printer_list(PRINTER_ENUM_LOCAL | PRINTER_ENUM_SHARED). There is something like a printer.default_printer directive on php.ini, if I recall ok. Tested this quite some time ago on a php 5.x installation. Good luck.

  • I've gotten that part working now (sorry I was a bit hasty in writting the question), I've updated it to my current situation, I can print a blank page to the right printer but nothing else. – Ben Brocka Oct 20 '11 at 20:55
  • `printer_write` will print a string, therefore that updated code should work. Try using `fopen()`, `fread()` and `fclose()` for a test with an external php/txt file. If it still doesn't works, considering that that `printer_open` is correctly defined for your local(?) printer, we should have to look somewhere else. – AleksanderKseniya Oct 20 '11 at 22:00
  • Just tried that and I get the text out of the file just fine but the page is still blank, I updated my code in my question. In addition OneNote complains that it can't read the "filetype" being printed, but every screen/paper printer I print to has given me a blank page. I wanted to get this to work but it's looking like it will be best to alter a VB app of ours to print instead, if you have more ideas I'll certainly try them though. – Ben Brocka Oct 21 '11 at 13:58
  • Maybe that happens because you aren't opening it as a binary file. On the `$fhandle=fopen($filename, 'r');` instead of read mode, use read binary: `$fhandle=fopen($filename, 'rb');` (Same goes for all the rest modes, check [here](http://www.cplusplus.com/reference/clibrary/cstdio/fopen/) for a quick reference. If that doesn't works, come back and we'll find some other way. – AleksanderKseniya Oct 24 '11 at 07:55
0

i messed around with printer.dll for ages and got sick of it.

not sure if its much use, but i purchased this application http://www.coolutils.com/TotalPDFPrinter

and it lets me print pdf files from the cmd line from php, which worked well for me, but it does have a popup screen which you can only get rid of if you purchase the server version (which you will need if your apache is running as a service). obviously the non-popup version is way more expensive.

one thing i struggled with, was getting a list of printers available, so i wrote this little app in C# that spits out a list of printers as plain text, and then use php to call that and put them into a drop list on my web forms.

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Printing;

namespace printerlist
{
    class Program
    {
        static void Main(string[] args)
        {
            foreach (String printer in PrinterSettings.InstalledPrinters)
            {
                Console.WriteLine(printer.ToString());
            }
        }
    }
}
bumperbox
  • 10,166
  • 6
  • 43
  • 66
  • Thanks for the tip, that's a no-go though, we'd have to get the server version as it's an automated process. – Ben Brocka Oct 20 '11 at 21:32