From disk to paper
Scripted Printing
Constantly recurring tasks, such as a database query, can be easily automated using a shell script. However, the command-line tools usually output the results without printable formatting.
With a little help from the enscript
command, the output can be processed quite easily. enscript
writes the result directly to a PostScript (PS) file and also supports printing the output content. This is used in the sample script from Listing 5.
Listing 5
Process Output
01 #!/bin/bash 02 # Database query (PostgreSQL) with print preparation 03 04 # Select PDF file or print 05 approach=$(echo "Print PDF" | smenu ) 06 if [ "$approach" = "Print" ]; then 07 # Select printer 08 target=$(/usr/sbin/lpc status all | grep \: | tr -d \: | smenu -n3 -c -m "Choose a printer:") 09 # Database query, character set conversion, print preparation and printing 10 psql -P border=3 -c "select * from parts;" | recode UTF8..ISO-8859-15 | enscript -H1 --highlight-bar-gray=0.8 -fCourierBold10 -P$target 11 elif [ "$approach" = "PDF" ]; then 12 # Database query, character set conversion, generate PS 13 psql -P border=3 -c "select * from parts;" | recode UTF8..ISO-8859-15 | enscript -H1 --highlight-bar-gray=0.8 -fCourierBold10 -o partlist.ps 14 # Convert to PDF file 15 ps2pdf14 partlist.ps 16 # Delete PS file 17 rm -v partlist.ps 18 echo "Query saved in partlist.pdf" 19 fi
However, it is important to note that Enscript cannot handle UTF-8 encoded files and pipes. You need to convert the data to the desired character set in advance using recode
. Both programs work both in a pipe and with files. If necessary, the PS files can also be converted to PDF format using Ps2pdf14
.
The sample script in Listing 5 still offers plenty of scope for improvements and your own ideas. The process flow is shown in Figure 9, and the database query's output is shown in Figure 10. The script is primarily intended to demonstrate how little effort it takes to solve even very complex tasks. Compared to the clicks required with a database client from an office package, the terminal script saves a huge amount of work.
![](/var/linux_magazin/storage/images/issues/2020/234/tutorial-printing-in-the-shell/figure-9/766209-1-eng-US/Figure-9_large.png)
![](/var/linux_magazin/storage/images/issues/2020/234/tutorial-printing-in-the-shell/figure-10/766212-1-eng-US/Figure-10_large.png)
Preparing the Output
The call to enscript
(lines 10 and 13 of Listing 5) can be adapted to suit your own needs, if required. For example, you can opt to print in landscape format, specify the type and size of the font, and even output source code with syntax highlighting (see the "Source Code in Color" box). Some of the corresponding options are listed in Table 3.
Table 3
enscript Options
Action |
Option |
Note |
Column specification |
|
|
Specification of the pages to be printed |
|
|
Print odd pages |
|
|
Print even pages |
|
|
Suppress page header |
|
|
Suppress job header |
|
|
Curtail over-length lines |
|
|
Specify printer |
|
Also possible, |
Duplex printing |
|
|
Syntax highlighting |
|
Output overview with |
Text font |
|
|
Header font |
|
|
Reading lines |
|
|
Specify grayscale for reading lines |
|
|
Title |
|
|
Multiple copy |
|
|
Output file |
|
|
Landscape format |
|
|
Footer |
|
|
Multiple logical pages per page |
|
|
For example, it is often necessary to avoid line breaks. If the lines are too long, it helps to use a smaller font or print in landscape format. This problem can also be solved by scripting. The wc -L
command lets you determine the length of the longest occurring line. You can then use the value obtained in this way as a criterion for determining the font size and page orientation:
- Up to 80 characters: 10/12pt font size
- 80-132 characters: 8pt font size or 10/12pt and landscape format.
- 132 characters or more: 8/10pt font size and landscape orientation.
Listing 6 shows a sub-script that uses wc
to determine the longest line (Line 7) and then tells enscript
to print in landscape mode or leave it in portrait mode with the conventional orientation (if
loop starting at line 10).
Listing 6
Choose Page Orientation
01 #!/bin/bash 02 03 # File selection 04 file=$(ls -1 | smenu -n 10 -t 4) 05 06 # maximum line length 07 mz=$(cat $file | wc -L) 08 09 # Portrait up to 80 characters, landscape above this 10 if [ $mz -lt 80 ]; then 11 cat $file | recode UTF8..ISO-8859-15 | enscript -H1 --highlight-bar-gray=0.8 -fCourierBold10 -o $file.ps 12 elif [ $mz -gt 80 ]; then 13 cat $file | recode UTF8..ISO-8859-15 | enscript -r -H1 --highlight-bar-gray=0.8 -fCourierBold10 -o $file.ps 14 fi 15 16 [... Print commands, PDF conversion ...]
Conclusions
Even in the shell and in scripts, you do not have to do without the convenience that graphical interfaces offer when printing. Printer selection, queue management, and the creation of attractive print output can be easily integrated into your shell scripts. And you don't have to reinvent the wheel or learn programming. Simple shell scripts and practical command line tools take much of the work off your hands.
Source Code in Color
Text editors intended for programming usually color highlight commands, variables, or instructions; this makes it far easier to keep track of the source code. enscript
also offers this kind of function with the -E<Language>
option. A list of all supported schemas can be obtained with the enscript --help-highlight
or enscript --help-pretty-print
commands, depending on the version. For example, the command
enscript -H1 --highlight-bar-gray=08 -fCourierBold10 --color -Ebash -o Source.sh.ps Source.sh
creates a colored image of the Source.sh
shell script (Figure 11). Before doing this, the correct character set again had to be set with recode
.
Infos
- lp manpage: http://manpages.org/lp
- lpr manpage: http://manpages.org/lpr
- "Create a select menu with smenu" by Harald Zisler, Linux Magazine, Issue 205, December 2017, p. 32, http://www.linux-magazine.com/Issues/2017/205/smenu
« Previous 1 2
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
![Learn More](https://www.linux-magazine.com/var/linux_magazin/storage/images/media/linux-magazine-eng-us/images/misc/learn-more/834592-1-eng-US/Learn-More_medium.png)
News
-
NVIDIA Released Driver for Upcoming NVIDIA 560 GPU for Linux
Not only has NVIDIA released the driver for its upcoming CPU series, it's the first release that defaults to using open-source GPU kernel modules.
-
OpenMandriva Lx 24.07 Released
If you’re into rolling release Linux distributions, OpenMandriva ROME has a new snapshot with a new kernel.
-
Kernel 6.10 Available for General Usage
Linus Torvalds has released the 6.10 kernel and it includes significant performance increases for Intel Core hybrid systems and more.
-
TUXEDO Computers Releases InfinityBook Pro 14 Gen9 Laptop
Sporting either AMD or Intel CPUs, the TUXEDO InfinityBook Pro 14 is an extremely compact, lightweight, sturdy powerhouse.
-
Google Extends Support for Linux Kernels Used for Android
Because the LTS Linux kernel releases are so important to Android, Google has decided to extend the support period beyond that offered by the kernel development team.
-
Linux Mint 22 Stable Delayed
If you're anxious about getting your hands on the stable release of Linux Mint 22, it looks as if you're going to have to wait a bit longer.
-
Nitrux 3.5.1 Available for Install
The latest version of the immutable, systemd-free distribution includes an updated kernel and NVIDIA driver.
-
Debian 12.6 Released with Plenty of Bug Fixes and Updates
The sixth update to Debian "Bookworm" is all about security mitigations and making adjustments for some "serious problems."
-
Canonical Offers 12-Year LTS for Open Source Docker Images
Canonical is expanding its LTS offering to reach beyond the DEB packages with a new distro-less Docker image.
-
Plasma Desktop 6.1 Released with Several Enhancements
If you're a fan of Plasma Desktop, you should be excited about this new point release.