Team LiB
Previous Section Next Section

Hack 53. Display PDF Documents in a Terminal

You can view PDF documents in a terminal or console; no graphical desktop is required.

This hack is similar to the previous one, which converts Word documents into HTML. But this hack shows you how to convert PDF documents into HTML. Then you view the HTML version with a text-based browser or pager called elinks. (The previous hack used the w3m text browser, but elinks works better than w3m with this hack.) With a little scripting magic and help from two programs, you can view the contents of a PDF file in a text terminal or console.

This time you need to convert a PDF document to HTML before you run it through the elinks pager. There's a fine utility for doing just that, and it's called (appropriately) pdftohtml. You can find the home page for pdftohtml at http://pdftohtml.sourceforge.net/. If pdftohtml isn't already installed in your distribution of Linux, or isn't on your CD set, it's commonly available for Debian and RPM-based distributions, such as Fedora, SUSE, and more. The elinks program is also easily available if it isn't automatically installed in your distribution. For example, you can install pdftohtml and elinks in Debian Linux with this command:

# apt-get install pdftohtml elinks

Users of the yum package can get the RPM version with this command:

# yum install pdftohtml

Now you can view a PDF document with the following command. This particular command has one drawback. The output will not include frames (PDF files generally have a frame on the left that lets you jump to different pages).

$ pdftohtml -q -noframes -stdout  document .pdf | elinks

If you want the left frame of page numbers, you can always use the following command instead:

$ pdftohtml -q  document .pdf ; elinks  document .html

You can write a script to save you all this typing each time you view a document. Use sudo or log in as root to create the /usr/local/bin/viewpdf script and enter the following code:

#!/bin/bash

pdftohtml -q $1 ~/temp.html
elinks ~/temp.html

This code assumes it's OK to store the temporary HTML file in your home directory. You can use another location if you prefer.

Now save your work and make the file executable:

$ sudo chmod +x /usr/local/bin/viewpdf

Figure 7-4 shows an example of what a PDF document will look like when you use viewpdf.

Figure 7-4. A PDF file shown in elinks


    Team LiB
    Previous Section Next Section