PHP CUPS Extensions

If your Unix/Linux system uses CUPS for printing, PHP applications can use it to spool print jobs to your printers, if the CUPS extensions are installed into PHP.

If CUPS is already installed before you install PHP, you will need to go to the CUPS build directory and look for phpcups.so. It is usually found in the ../scripting/php subdirectory. Copy this module to the PHP extensions directory and edit the PHP config file (typically /etc/php.ini) to add phpcups.so as an extension. For example:

     extension=/usr/local/lib/php/modules/phpcups.so

Note that you probably will have to create the extensions directory. If another PHP extension is already installed, this directory may already exist and be named something like ../extensions/no-debug-non-zts-20050922 or ../modules in the PHP architecture-independant install directory (e.g. /usr/local/lib/php). However, if it doesn't exist, you should create one (there is no need for the "no-debug-non-zts-20050922" part of the name) and copy phpcups.so into it:

     mkdir /usr/local/lib/php/extensions
     chown root:root /usr/local/lib/php/extensions
     chmod u=rwx,go=rx /usr/local/lib/php/extensions
     cp phpcups.so /usr/local/lib/php/extensions
     chown root:root /usr/local/lib/php/extensions/phpcups.so
     chmod u=rwx,go=rx /usr/local/lib/php/extensions/phpcups.so

or, alternately:

     mkdir /usr/local/lib/php/modules
     chown root:root /usr/local/lib/php/modules
     chmod u=rwx,go=rx /usr/local/lib/php/modules
     cp phpcups.so /usr/local/lib/php/modules
     chown root:root /usr/local/lib/php/modules/phpcups.so
     chmod u=rwx,go=rx /usr/local/lib/php/modules/phpcups.so

If you install CUPS after PHP is installed, the CUPS install will copy phpcups.so to the PHP extensions or modules directory for you. And, later versions of Linux (e.g. RedHat 5.x or CentOS 5.x) may have their CUPS RPMs install this module into /usr/lib/php/modules.

Note that, if you upgrade PHP to a later version that changes the API version number (e.g. from 20060613 to 20090626), you may see messages like this (from the command line version):

     PHP Warning:  PHP Startup: phpcups: Unable to initialize module
     Module compiled with module API=20060613
     PHP    compiled with module API=20090626

Or, PHP may just leave the phpcups module out (you can check this by running the phpinfo.php script, described above and looking for the phpcups extension) of your installation.

In this case, the simple fix is to proceed to the CUPS installation directory and run:

     ./configure
     rm -f scripting/php/phpcups.o
     rm -f scripting/php/phpcups.so
     make

Once you've done that, copy the newly built phpcups.so to the PHP extensions directory:

     su
     cp scripting/php/phpcups.so /usr/local/lib/php/extensions

or maybe:

     su
     cp scripting/php/phpcups.so /usr/lib/php/modules

After you install phpcups.so (or reinstall it) and use CUPS to define printers on the Web server that runs your application, the printers will appear in the list of printers enumerated by the PHP CUPS extension functions and can be used accordingly in your PHP application.

Incidentally, if you are running an OS that is installed through a package manager, using packages such as RPMs (what OS isn't, these days), you may find yourself stuck for a solution.

With at least one OS that we know of, some genius in the packaging department decided that, because a product has a "Print" button, it needs to DEPEND on CUPS. The result? If you try to uninstall CUPS, these idiotic dependancies mean that practically every package that you could ever think of needs to get unistalled. E.g. sendmail. Or all of the Gnome UI. Who gives darn? If you can't print something, it doesn't break the entire functionality of the app. Just get on with the unistall.

In order to get a working phpcups.so that can be used by PHP, we need to rebuild the module against the new Apache interface. But, since we installed a different Apache, other than the one that the OS comes with, we can't just unistall CUPS (because this would break almost everything) and build a new one. So, we need to find the version of CUPS that was chosen for the OS (probably, about 3 years old), from the CUPS archives (http://ftp.easysw.com/pub/cups/), and build it. The build will create a phpcups.so that matches the Apache interface. You can then copy that into the PHP modules directory.

If you are building against an alternate version of PHP, than the one that is installed on your OS, you can point the CUPS build at the atlernate something like this:

     ./configure --with-php=/usr/local/include/php

Note, that, if you build against a later version of PHP (one that doesn't include the php3_compat.h header file in the main directory), you may get a compile error in phpcups.c that says:

     phpcups.c:43: error: expected '=', ',', ';', 'asm' or '__attribute__'
       before 'phpcups_functions'

This is because the phpcups.c module uses the obsolete "function_entry" attribute for the list of phpcups functions, instead of the currently-used attribute "zend_function_entry". Until recently, the php3_compat.h file defined "function_entry" as "zend_function_entry". Then, the header file went away and the phpcups.c compile became broken.

The fix is to edit the phpcups.c source file and change line 43 to read:

     zend_function_entry phpcups_functions[] =

Then, rebuild phpcups.c via make and go back to the part above about reinstalling phpcups.so.