Chris Nizzardini, Salt Lake City Utah, Web Developer Specializing in LAMP+Ajax Since 2006

My Blog

Here is my awesome blog. You can find information on programming, linux, documentation, tips for code and database optimization, my thoughts and rants, and whatever else I feel like sharing. Feel free to contribute to the blog by posting comments and asking questions.
Linux

adventures in linux printing, set default printer and firefox command line print

We are in the process of redesigning our packing slips that get sent out to customers with each order. The previous developer went with a solution, html2ps, that has shotty support for CSS and design principals in general. The designer and I decided we needed something else. After trying numerous solutions, a co-worker suggested I give FireFox command line print a try.

http://torisugari.googlepages.com/commandlineprint2

Example:

1
firefox -print http://www.google.com

I’m not into using beta software in production, but html2ps is a horrible solution, a windows print server (I tried) is not an option, and we are not going to pay for something either (I’ll chalk that up to a nasty recession). The FireFox Command Line print addon functions in a solid manner and allows usage of the FireFox rendering engine for converting your html/css into post script.

There are some caveats with this approach though. Our system administrator had to use his wizardary to get X to open from a cron job. Yes, FireFox Command Line Print still requires X as it will quickly open the FireFox GUI as it executes a print job. I’m not exactly sure how our Sys Admin did this, but I’m sure it was awesome.

Since we are printing invoices on a pre-printed design we did encounter some problems with margins, but that was quickly resolved by altering the print margins in FireFox about:config. The next hurdle we had to jump was handling multiple printers. We have multiple websites that tie into the same backend system. So each website needs its own pre-printed invoice complete with logo, contact information, special offers etc… This means we load up a given printer with just that websites pre-printed packing slips.

The problem is FireFox Command Line Print has shotty support for choosing which printer to send a job to. There is a switch that the documentation calls on you to use.

Example:

1
firefox -print http://www.google.com -printprinter LaserJet

For us, this did not work. The next thing I tried was using the lpoptions command (yes we are using PHPs, exec() function).

Example:

1
lpoptions -d LaserJet

Still this was not switching printers. All jobs would still go to the same printer. At this point I decided to do an…

1
echo $PRINTER

from the shell. This will display a given users default printer. When I did this, even after executing lpoptions -d LaserJet, the ouput was blank. Finally, this worked:

export PRINTER=LaserJet

This changed the output of echo $PRINTER and now I can run the export command prior to executing a print job to switch which printer an invoices goes too. Some other considerations are how to run your printing. One, it is not a good idea to execute a job for each invoice, especially when you are printing batches of hundreds of invoices. Imagine 100 instances of firefox opening, it can consume RAM and CPU that it doesn’t need to. We instead decided to add all invoices that need to be printed to a print queue mysql table, a cron job then runs every few minutes and builds 1 large html page out of all the invoices. This way we only open 1 instance of firefox.

Recap
So in the end I fell we created a much better solution in terms of design. There could be more overhead in this in the long run, but html2ps wasn’t exactly resource friendly either. Here’s a recap:
1. Converted raw database data into an HTML page.
2. Used the FireFox plugin, Command Line Print, to print the HTML page.
3. For switching between printers we used a simple shell command, export PRINTER=LaserJet
4. To avoid unneeded memory consumption we created a system of batches that require a single firefox window, not multiple.

Anyways, printing in Linux is definitely not my preference, but its nice to know there are options out there.

Related posts:

  1. Kill Windows Processes from the Command Line
  2. WaitForExit method, running command line args in c#, and other hacks
  3. Get active directory ldap attributes from the command line with ldifde
  4. linux using the cat command to append files together
  5. firefox 3.5 fails – bug with ajax callback onreadystatechange

Tags: , , , , , ,

2 Responses to “adventures in linux printing, set default printer and firefox command line print”

  1. So the good parts… it is CHEAP and makes nice barcodes. Once you get the hang of making your first one it is not to bad (but not 1 – 2 – 3). Printed easy enough. Just like printing in Word or any other document. Changing the font and color to print the barcode was easy

  2. I don’t usually reply to posts but I will in this case, great info…I will add a backlink and bookmark your site. Keep up the good work!

    I’m Out! :)

Leave a Reply