How to turn the Raspberry Pi into a wireless printer server

The CUPS printing server installs a bunch of command-line tools (see Administering CUPS later in this guide) for interacting 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

Turn Raspberry Pi into Printer Server

All Linux distros can access the USB printers connected to the Raspberry Pi without any tweaks to the distro

Converting from HTML to PDF

A more interesting way to convert HTML pages into PDF file is to use the wkHTMLtoPDF 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 xfonts
scalable xfonts-cyrillic

Then install the tool with

sudo apt-get install wkhtmltopdf

before installing the Python wrapper with:

$ sudo pip install git+https://github.com/qoda/pythonwkhtmltopdf.git

You can now use the following to convert a web page into a PDF file:

from wkhtmltopdf import WKHtmlToPdf

wkhtmltopdf = WKHtmlToPdf (

url='http://www.techradar.com',

output_file='/home/pi/docs/lxf.pdf',

)

wkhtmltopdf.render()

When executed, the above code saves the main techradar into a PDF file under the /home/pi/docs directory.

Refer to the listing below to see how all the pieces fit together – wkHTMLtoPDF converts a page into a PDF and prints it out.

#!/usr/bin/env python
import cups
from wkhtmltopdf import WKHtmlToPdf
wkhtmltopdf = WKHtmlToPdf(

url='http://www.techradar.com',
output_file='/home/pi/techradar.pdf',
)
wkhtmltopdf.render()
conn = cups.Connection()
printers = conn.getPrinters()
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 techradar 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://pythonhosted.org/pycups) that you can use to control all aspects of the CUPS print server.

How to turn the Raspberry Pi into a wireless printer server

You have to install and configure Samba to access the network printers on Windows

Administering CUPS

In addition to adding printers, the CUPS web interface provides access to various other useful settings. You can administer most of the printing tasks from the Administration tab, which houses settings under various different categories.

Under the Server section, for instance, you can find options to tweak the configuration of the server as well as view various types of access and error logs.

Using the 'Manage Printers' button under the Printer section, you can control the settings for individual printers. Every printer's page has options rolled under two pull-down menus labelled Maintenance and Administration. From under the Maintenance menu, you can print a test page, a self-test page, clean print heads and manage print jobs.

To customise the behaviour of the printer, use the Administration menu to tweak its default options, set it as the default printer, restrict user access, modify its settings or delete it from the CUPS server altogether. Besides the Administration tab, there are a couple of other important tabs we should mention as well.

For starters, you need to switch to the Classes tab for printer class management. A class is a collection of several printers. When you send print job to a class, CUPS automatically assigns the job to the next available printer, instead of waiting for a specific printer. Then there's the Jobs tab, which enables you to view and manage all print jobs that are currently in the print queue.

TOPICS
Mayank Sharma

With almost two decades of writing and reporting on Linux, Mayank Sharma would like everyone to think he’s TechRadar Pro’s expert on the topic. Of course, he’s just as interested in other computing topics, particularly cybersecurity, cloud, containers, and coding.