84

I want to view pdf files directly on our cluster rather than copying them to my local machine and then opening them in a viewer.

How can I view a pdf file in my terminal?

3

11 Answers 11

75

In many systems less uses lesspipe, which can handle pdftotext automatically. Therefore, you can immediately try

 less file.pdf

which will show the output of pdftotext in less.

1
  • 7
    For people like me who found this and get the error "no pdftottext available" just run apt-get install pdftohtml first to get this to work. Thanks to stackoverflow.com/questions/3570591/…
    – Ryan
    Commented May 12, 2020 at 0:16
32

I guess, it is not possible to see PDF file in terminal but you can check it's content by converting PDF file to text. You can do this as:

pdftotext a.pdf

It will produce a.txt file which you can read into VIM.

For ubuntu-variant, this binary is available in following package.

poppler-utils
1
  • 8
    It is possible to see a PDF file in the terminal, like so: pdftotext -layout file.pdf - | less (that's how Ubuntu's default lesspipe script does it)
    – villapx
    Commented Jun 5, 2017 at 13:33
11

I tried the following with good results:

pdftotext filname.pdf - | less
3
  • 2
    This works better for me on RHEL, since the lesspipe.sh script doesn't appear to support *.pdf
    – klugerama
    Commented Nov 10, 2020 at 18:09
  • 1
    The -layout flag can be added to the pdftotext command to preserve the formatting.
    – user598527
    Commented Jun 2, 2022 at 9:08
  • @klugerama On Fedora (38) a PDF file opened in less (less FILE.pdf) resembled a binary format.
    – user598527
    Commented Jun 16, 2023 at 9:03
6

When I want to "view a pdf file in terminal", that for me means that I want to actually see an uncompressed PDF, I do:

pdftk in.pdf output out.pdf uncompress

I always wondered why both less in.pdf and less out.pdf give me just text strings in the PDF (and excluding the text-only PDF commands I'd expect in out.pdf).

Well, that happens because of the lesspipe assuming I want pdftotext being run first - and since here I don't, I have to specifically disable the lesspipe by setting LESSOPEN environment variable to nothing; that is:

$ LESSOPEN="" less out.pdf

And finally, I can view the uncompressed PDF code using less

4

Yet another solution... May I recommend to you the ancient utility mc.

MC(1)                       GNU Midnight Commander
mc - Visual shell for Unix-like systems.

mc is designed around text-based file-management, and it has a “view” option (F3 key) which will automatically convert .pdfs to text for viewing without a GUI. The code which does this conversion is part of mc itself, so it does not require conversion by other utilities. (Also has a native .html viewer for WIW.)

2
  • 1
    mc's viewer is very visually attractive compared to the other options here! To view a pdf without opening the file manager, you can use the -v option like this: mc -v file.pdf. Commented Jul 13, 2020 at 19:55
  • Need to work out how to suppress opening an X application for that extension though on double click. Using bottom menu is well functional.
    – mckenzm
    Commented Dec 8, 2022 at 4:14
2

This might be helpful, please note that:

  • It is for macOS only.
  • It won't open the pdf inside the terminal window rather open a Quick look, which I find very powerful to see any file or documents quickly. Simply use:
qlmanage -p <file_name>

Maybe similar command in Linux called (gnome-sushi) I haven't tested this, but might be helpful as well. I will update this post after testing it.

1
lesspipe file.pdf | less

lesspipe is provided by the package less on Debian and Ubuntu.

1

It may be worth to consider running imgcat1. You mentioned that you want to browse remote files. This solution doesn't meet your requirements to view files directly on the cluster but I reckon it may pass as acceptable. Using wget you can pass outputs into the stdout and then pipe it to imgcat as follows:

wget -O- -q https://www.nceas.ucsb.edu/sites/default/files/2020-04/colorPaletteCheatsheet.pdf | imgcat

This will produce the following results:

Example

Remarks

Again, this is not great solution as you will need to download the file (even if you are just piping it) and imgcat makes it fiddly to look at specific page in PDF. Nevertheless, I reckon it's worth to post this solution as this little trick comes in handy when dealing with PDFs that contain images, etc.


1Worth adding that there are to packages with identical name, iTerm's and one that is maintained.

0

One more solution is to use command gnome-open

gnome-open youfile.pdf

Provided that you have login your server with -X option (ssh -X)

3
  • 4
    How does this view the PDF file in the terminal?
    – user
    Commented Feb 27, 2013 at 12:27
  • Not on terminal, but can view a pdf on issuing command from terminal. Commented Feb 28, 2013 at 12:40
  • 5
    While that is true, the OP did specifically ask for a terminal-based approach.
    – user
    Commented Feb 28, 2013 at 20:52
0

If you run emacs on your machine (emacs comes preinstalled on Ubuntu 18.04), you can virtually open and see a pdf on a remote server by hitting Ctrl-x Ctrl-f (to find-file) and then type in /user@hostname:/path/to/my.pdf and hit Enter (note the very first /). You will then be prompted to enter the server's password and there it is! You can see the pdf inside emacs.

Navigate through PDF inside emacs

Use space to go one page down and backspace to go one page up. You can also use arrow keys to scroll through a single page if it doesn't fit in the screen.

Zoom

Zoom in by hitting Ctrl-x Ctrl-+. Zoom in more by hitting + only. Or zoom out more by hitting -.

Context

Yes, you can connect to a remote server from within emacs using the build-in package tramp that works as simple as I explained above. This method works, not only for pdf, but for any other type of file, such as images.

0
python -m pip install pypdfium2
pypdfium2 extract-text ./path/to/doc.pdf |less

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .