Linux Format

CUPS: Printing

Using the Raspberry Pi as a wireless print server is easy when you have a Raspberry Pi and CUPS. Mayank Sharma shows us how.

- Mayank Sharma was a contributi­ng editor for www. linux.com and has written for LinuxToday, Digg and PCPlus.

Aprinter isn’t the most convenient of peripheral­s. They look out of place on most work desks and create quite a racket when spitting out pages. You could throw a few hundred quid on a snazzy new network printer that sits in a corner somewhere and can receive print orders from any computer on the local network or you could just hook your regular USB printer to the Raspberry Pi and enjoy the same convenienc­es offered by top of the line network printers.

If you haven’t already used your printer on Linux, before you get started with this project head to www.openprinti­ng.org/printers to check whether your printer is compatible with the CUPS printing server software. If your printer is listed, hook it up to the Raspberry Pi using one of the USB ports. For this project, we’re using the Raspbian distro and the Pi is connected to the local network via a compatible wireless adaptor. However, you can also hook the Pi up to your network via the wired Ethernet port.

You can follow the instructio­ns in this tutorial by accessing the Raspberry Pi remotely from any other computer on the network. Just make sure that the SSH server inside Raspbian is enabled by using the raspi-config tool. It’s also a good idea to assign a fixed IP address to the Pi. You can do this easily from within your router’s admin page. For this tutorial we’ll assume that the IP address of your PI is 192.168.3.111. You can now access the Pi from within Windows using the

PuTTY client or from any Linux distro with the SSH CLI command with:

$ sudo ssh pi@192.168.3.111

Install CUPS

Once you’re inside Raspbian, update the repositori­es (repos) with $ sudo apt-get update and then install any updates with $ sudo apt-get upgrade . Now pull in the CUPS print server with $ sudo apt-get install cups . When it’s installed, add your user to the group created by CUPS called lpadmin that has access to the printer queue. Unless you have created a custom user, the default user on Raspbian is called pi. Use the following command to allow it to

interact with the printer: $ sudo usermod -a -G lpadmin pi .

Here we use the usermod tool to add ( -a ) the pi user to the lpadmin group ( -G ). By default, CUPS can only be configured from the local computer that it’s installed on. Because that doesn’t work in our case, we need to edit its configurat­ion file to allow us to make changes to the server from a remote computer. First of all, you need to create a backup of the original configurat­ion file with: $ sudo cp /etc/cups/cupsd.conf /etc/cups/cupsd.conf.orig

Then open the file with the nano text editor $ sudo nano /

etc/cups/cupsd.conf . Inside the file, scroll down to the

following section: # Only listen for connection­s from the local machine Listen localhost:631

Comment out that line and add another to ask CUPS to accept connects from any computer on the network. Make sure the section looks like this: # Only listen for connection­s from the local machine # Listen localhost:631 Port 631

Then scroll further down in the configurat­ion file until you reach the <Location> sections, and add a new line that reads

Allow @local just before the close of the section. The section with the appended line should now read like this: < Location / > # Restrict access to the server Order allow,deny Allow @local < /Location >

Now add the Allow @local line to the other two Location sections – <Location /admin> and <Location /admin/conf> . Save the file and restart the CUPS server with: $ sudo /etc/

init.d/cups restart . You should now be able to access the CUPS administra­tion panel via any computer on your local network by pointing the web browser to your Pi. Then follow the walkthroug­h over the page to add your printer to CUPS.

Some Linux distros ship with a restrictiv­e iptables firewall policy that doesn’t allow connection­s via the CUPS ports. Even if Raspbian doesn’t, make sure it doesn’t throw up any unexpected errors by punching holes in the firewall with: $ sudo iptables -A INPUT -i wlan0 -p tcp -m tcp --dport 631 -j ACCEPT $ sudo iptables -A INPUT -i wlan0 -p udp -m udp --dport 631 -j ACCEPT

If you connect to the Raspberry Pi via Ethernet instead of a

wireless adaptor, modify the command and replace wlan0

with eth0 . When you are through setting up your printer using the CUPS administra­tion panel, it’s time to make it accessible to other machines on your network. While Linux distros will have no trouble detecting your new network printer, making them visible to Windows and Apple devices requires a couple of extra steps.

Network-wide access

For Windows, install the Samba server on the Pi with $ sudo apt-get install samba . Then open its configurat­ion file (/ etc/ samba/smb.conf) in the nano text editor and hunt for the section labelled [printers] and make sure it contains the line: guest ok = yes

Then scroll down to the [print$] section and change its path to the following: path = /usr/share/cups/drivers

Then scroll up to the Global Settings section at the top of the configurat­ion file. Modify the workgroup parameter within to point to the name of your workgroup, which by default is named WORKGROUP . Also enable the wins support by adding the line wins support = yes .

Now save the file and restart Samba with $ sudo /etc/ init.d/samba restart .

Then head over to the Windows machine and launch the Add New Printer wizard and click on the option to install a network printer. Thanks to the modified Samba configurat­ion, the wizard will detect and list any printers hooked up to the Pi. If you have Apple devices, you can enable support for Apple’s AirPrint system, which allows you to print from the iPad and iPhone. For this, just install the Avahi daemon with sudo aptget install avahi-daemon on the Pi, which will then make the connected printer visible to AirPrint-compatible devices.

In addition to the ability to use our network printer from within graphical applicatio­ns across all platforms, we can also use it to print from the command line interface. Furthermor­e, we can also interact with the printer using the Python programmin­g language.

Print from Python

The CUPS printing server installs a bunch of command-line tools (seeAdminis­teringCUPS­onp67) for interactin­g with the server and any connected printers. You can send files to the printer using the lp command, such as: $ lp ~/docs/a_text_file.txt

If you have multiple printers, you can print to a specific printer by specifying its name, such as: $ lp ~/docs/another-text.txt -d EPSON_LX-300

When you use the commands with a PDF or image file, CUPS converts the files using the printer drivers. You can also use Python to generate printer-friendly content. This is best done by using the PyCups library, which provides Python

bindings for the CUPS server. Install the library with:

$ sudo apt-get install python-cups.

Then create an example.py Python script with: import cups conn = cups.Connection() printers = conn.getPrinter­s () for printer in printers: print printer, printers[printer]["device-uri"]

The script fetches details about all the printers managed by CUPS and prints their name and device address to the screen. When you execute the script, it produces an output similar to the following: EPSON_LX-300 usb://EPSON/LX-300+?serial=L010209081 RICOH_Aficio_SP_100 usb://RICOH/ Aficio?serial=T382M97798­3

You can also print files from the Python script using the printFile function, by specifying them in the format: $ printFile (name of the printer, filename to print, job title, options)

Open the previous example.py script and add the following lines to it: file = “/home/pi/testfile.txt” printer_name=printers.keys()[0] conn.printFile (printer_name, file, “Project Report”, {})

The first line saves the name of the file you wish to print inside a variable named file . The second line fetches the list of printers and saves the first name, which is the default printer inside a variable named printer_name . The third line then uses the first two variables and prints the file in the specified format.

Converting from HTML to PDF

A more interestin­g way to convert HTML pages into PDF file is to use the wkHTMLtoPD­F toolkit, which passes on the PDF to the printer from within a Python script.

Before you can install the toolkit, first install the required components and a set of fonts to process the web pages: $ sudo apt-get install xvfb xfonts-100dpi xfonts-75dpi xfontsscal­able xfonts-cyrillic

Then install the tool with sudo apt-get install wkhtmltopd­f before installing the Python wrapper with: $ sudo pip install git+https://github.com/qoda/pythonwkht­mltopdf.git

You can now use the following to convert a web page into a PDF file: from wkhtmltopd­f import WKHtmlToPd­f wkhtmltopd­f = WKHtmlToPd­f ( url='http://www.linuxforma­t.com’, output_file='/home/pi/docs/lxf.pdf’,

) wkhtmltopd­f.render()

When executed, the above code saves the main page of the LinuxForma­t website into a PDF file under the /home/ pi/docs directory.

Refer to the listing below to see how all the pieces fit together – wkHTMLtoPD­F converts a page into a PDF and prints it out. #!/usr/bin/env python import cups from wkhtmltopd­f import WKHtmlToPd­f wkhtmltopd­f = WKHtmlToPd­f( url=’http://www.techradar.com’, output_file=’/home/pi/techradar.pdf’, ) wkhtmltopd­f.render() conn = cups.Connection() printers = conn.getPrinter­s() for printer in printers:

print printer, printers[printer][“device-uri”] file=”/home/pi/techradar.pdf” printer_name=printers.keys()[0] conn.printFile (printer_name, file, “PDF Print”, {})

The script first converts the www.techradar.com home page into a PDF. It then connects to CUPS, prints a list of attached and configured printers on the screen, and uses the default printer to print the PDF. The PyCups library is chockfull of methods ( https://pythonhost­ed.org/pycups) that you can use to control all aspects of the CUPS print server. Happy hacking!

 ??  ?? From the Printers tab, you can track the status of every job on every printer.
From the Printers tab, you can track the status of every job on every printer.
 ??  ?? You can also browse through its extensive documentat­ion from the CUPS browser-based control panel.
You can also browse through its extensive documentat­ion from the CUPS browser-based control panel.
 ??  ??
 ??  ??
 ??  ?? All Linux distros can access the USB printers connected to the Raspberry Pi without any tweaks to the distro.
All Linux distros can access the USB printers connected to the Raspberry Pi without any tweaks to the distro.
 ??  ?? You have to install and configure Samba to access the network printers on Windows.
You have to install and configure Samba to access the network printers on Windows.

Newspapers in English

Newspapers from Australia