Printing: CUPS

Jul 16, 2011·
Julio Batista Silva
Julio Batista Silva
· 3 min read
blog Tutorial

CUPS is an open-source printing system maintained by Apple and is probably the simplest for Unix-like systems.

With CUPS, adding and sharing printers over the network becomes a fairly easy task.

  1. Download the packages with the necessary programs and drivers.

    (I only have HP printers here, but also install gutenprint, foomatic-db, foomatic-db-engine, foomatic-db-nonfree, foomatic-filters, splix, and ufr2 as needed)

    julio@julio-acer ~  $  sudo pacman -S cups ghostscript gsfonts hpoj hplip cups-pdf
    
  2. If your printer is not listed, download its PPD file from <http://www.openprinting.org/printers to /usr/share/cups/model/>

  3. Add CUPS (cupsd) to the daemons in /etc/rc.conf

    DAEMONS=(syslog-ng @network netfs crond alsa httpd mysqld dbus @ntpd sshd cupsd samba @subsonic oidentd)
    
  4. Start the daemon and access the web interface: http://localhost:631/

  5. Log in

    • username: root
    • password: same as system password
  6. In Administration, go to “Find New Printer”. It should list the connected printers, just select them from the list.

    The cups-pdf package we installed earlier allows you to use a virtual printer to generate PDFs.

    To configure it, select:

    • Device: “CUPS-PDF (Virtual PDF Printer)”
    • Make/Manufacturer: Generic
    • Model/Driver: Generic Cups-PDF Printer
  7. To access the print server remotely, edit the file /etc/cups/cupsd.conf and change Listen localhost:631 to port 631.

  8. To allow printing over the network, simply edit the Samba configuration file.

Printing from the command line

  1. Use lpstat to check available printers:

    julio@julio-acer ~> lpstat -p -d
    printer CUPS-PDF is idle.  enabled since Thu 19 Jan 2012 05:47:16 PM BRST
    printer HP_1310 is idle.  enabled since Thu 19 Jan 2012 05:50:40 PM BRST
    no system default destination
    
  2. Define a default printer

julio@julio-acer ~> lpoptions -d HP_psc_1310_series                                                   1
auth-info-required=none copies=1 device-uri=hp:/usb/psc_1310_series?serial=BR5763H068O2 finishings=3 job-hold-until=no-hold job-priority=50 job-sheets=none,none marker-change-time=0 number-up=1 printer-commands=AutoConfigure,Clean,PrintSelfTestPage printer-info='HP psc 1310 series' printer-is-accepting-jobs=true printer-is-shared=false printer-location='quarto do julio' printer-make-and-model='HP PSC 1310 Series hpijs, 3.11.10' printer-state=3 printer-state-change-time=1326936293 printer-state-reasons=none printer-type=10522636 printer-uri-supported=ipp://localhost:631/printers/HP_psc_1310_series
julio@julio-acer ~> lpstat -d
system default destination: HP_psc_1310_series
  1. We can print a text file using the lp command.
julio@julio-acer ~> lp teste.txt
request id is HP_1310-1 (1 file(s))
  1. The previous print was successful, but the text came out a bit large and without margins. We can configure the paper type, margins, and other options via the command line:
julio@julio-acer ~> lp -d HP_1310 -o media=a4 -o cpi=12 -o lpi=8 -o page-left=72 -o page-top=72 teste.txt
request id is HP_1310-20 (1 file(s))

Check the manual for more options.

Printing from VIM

After a few years using VIM as my default text editor, I realized that you can do almost anything through it, including printing texts. Even codes with syntax highlighting. Unlike GVim, which has a ‘Print’ option in the File menu, printing from VIM can only be done through commands, which are worth memorizing:

  1. Open a text file in VIM and use the following command to print it with the default printer:
:hardcopy
The printout came out with margins and with the file name and page number in the header.
  1. We can also use another printer, such as CUPS-PDF that we configured earlier

    :set printdevice=CUPS-PDF
    :set printoptions=paper:A4,syntax:y,wrap:y,number:y
    :hardcopy
    

By default, CUPS-PDF saves files in /var/spool/cups-pdf/julio. To change this, edit the following line in the file /etc/cups/cups-pdf.conf: Out /home/${USER}

Note: If printing source code, change the colorscheme to default first.


Julio Batista Silva
Authors
Senior Cloud Developer

I’m a Brazilian computer engineer based in Germany, passionate about tech, science, photography, and languages.

I’ve been programming for about two decades already, exploring everything from mobile apps and web development to machine learning. These days I focus on cloud SRE and data engineering.

I volunteer in the open source and Python communities, helping organize PyCon DE and PyData Berlin, mentoring, and contributing with code and translations.

On my blog, I share Linux tips, setup guides, and personal notes I’ve written for future reference. I hope others find them helpful as well. The content is available in multiple languages.

Browse my gallery for some of my photography.

Away from the keyboard, you’ll find me at concerts, playing clarinet, cycling, scuba diving, or exploring new places, cultures, and cuisines.

Always happy to connect! 🙂

comments powered by Disqus