Please note that this article seeks to explore viable ways of viewing (not opening) a PDF file from the Linux command-line environment. The creation of the PDF or Portable Document Format file type was inspired to solve/lessen the hurdles that made document sharing between operating systems and computers difficult.
Why View PDF File in Linux Command Line
Being able to view a PDF file from the Linux command-line environment is associated with the following benefits.
- PDF maintains a document’s format. Once a document is created and saved in a PDF format, it cannot be altered like documents existing in .txt or .docx formats. Therefore, viewing such a document from the Linux command line environment should be the same as viewing it from a graphical PDF document reader.
- PDF files are comparatively smaller than other document file formats making it easy for the Linux terminal environment to accommodate them.
- Not all Linux users are tied to a Desktop environment where access to a graphical PDF reader application is easy. For users or Linux administrators bound to a Linux server operating system environment, this approach makes it easy to relatively preview PDF documents like receipts and invoices.
[ You might also like: How to Merge Multiple PDF Files into One PDF in Linux ]
We are going to need a sample PDF file which we will try to open and view from the Linux operating system command line.
The preview of the above file is from a graphical PDF reader application installed on a Linux operating system.
Let us break down the approaches to viewing PDF files from a Linux terminal.
Method 1: View PDF File Using less Command
The GNU General Public License makes it possible to use less utility from the Linux terminal to read input files. In comparison to graphical text editors like vi and nano, less command takes a shorter duration to read and display the targeted file on the Linux terminal window.
To view our PDF file (draft.pdf), we will run the following command.
$ less draft.pdf
As expected, the PDF file format is retained.
Method 2: Combining pdftotext and less Commands
As stated on its naming convention, the pdftotext command utility is primarily effective in converting a PDF file to a text file.
Its usage syntax is as follows:
$ pdftotext [options] [PDF-file [text-file]]
However, we are not primarily interested in converting our sample PDF file to a text file format. We, therefore, need to temporarily convert the sample PDF file to a text file and then pipe the converted text file to the less command for viewing/display on the Linux terminal window.
Our modified syntax for this approach is as follows:
$ pdftotext [options] [PDF-file] - | less
The final implementation of the above command is as follows:
$ pdftotext draft.pdf - | less
We have successfully learned how to read a PDF file as input and display/view it on a Linux command-line window.