Until now, I’ve never managed to reliably setup printer margins correctly in Ubuntu and have occasionally had odd characters printed on the page. I think this is a combination of using a relatively old and obscure printer (HP LaserJet 5P) and also using A4 paper when most of the cups/ppd files default to letter.

Anyway, here’s the procedure to setup your printers:

  1. Make a backup copy of the ppd file for your printer from /etc/cups/ppds
  2. Download alignmargins and align.ps from www.openprinting.org/
  3. Create a postalignmargins.pl file, with the contents of the script from this mailing list
  4. Change your current setup to have margins of 0:
    1. Run sudo perl alignmargins
    2. Throw away the page that is printed
    3. Answer all promts as 0
    4. Run sudo perl postalignmargins.pl "your-printer-name" (the alignmargins script doesn’t set all the required fields in the ppd file — this one will)
    5. Run sudo /etc/init.d/cups restart to restart the printer server
  5. Print a test page and set the x and y offsets correctly. Do not set the margins at this stage:
    1. Run sudo perl alignmargins
    2. Measure the distance between the left hand side of the paper and the feint vertical line and the bottom side of the paper and feint horizontal line (marked with dark arrows as H and V)
    3. Put these calculations into the formula on the printed sheet
    4. Enter values of 0 for ml, mb, mr, mt and your calculated values for x and y offsets (these may be negative)
    5. Run sudo perl postalignmargins.pl "your-printer-name" again
    6. Run sudo /etc/init.d/cups restart again
  6. Next, you need to setup the margins, based on the hardware limits of the printer. You need to start with a new test page:
    1. Run sudo perl alignmargins
    2. From the printed sheet, read off the values of ml, mb, mr, mt from the scales on each side of the paper, where the gray sections meets the white margin
    3. Enter these readings and the x and y offsets from before in the prompt
    4. Run sudo perl postalignmargins.pl "your-printer-name" again
    5. Run sudo /etc/init.d/cups restart again
  7. The alignmargins script isn’t perfect and will have added a new *Options section at the end of your ppd file in /etc/cups/ppd/. Find this section and delete it manually (otherwise, you will get odd characters printed on the screen). I highly recommend the graphical tool meld for this.
  8. Run sudo /etc/init.d/cups restart again
  9. Finally, run alignmargins one final time and check that the output is correct — you should see a thin black border around the page.

It took me 12 sheets of paper to perfect the process - let me know how you get on!

Credits:

  • Discovery of alignmargins script: http://home.att.net/~Tom.Horsley/linux-tidbits.html
  • Additional script to fix the results: http://osdir.com/ml/printing.cups.general/2003-09/msg00174.html