81

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?

HalosGhost
  • 4,790

11 Answers11

74

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.

  • 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 https://stackoverflow.com/questions/3570591/cli-pdf-viewer-for-linux#comment40581262_9553096 – Ryan May 12 '20 at 00:16
31

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
Pandya
  • 24,618
SHW
  • 14,786
  • 14
  • 66
  • 101
  • 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 Jun 05 '17 at 13:33
11

I tried the following with good results:

pdftotext filname.pdf - | less
jmunsch
  • 4,346
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

sdaau
  • 6,778
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.)

  • 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. – Rachel Frei Jul 13 '20 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 Dec 08 '22 at 04: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.

Dr Neo
  • 161
1
lesspipe file.pdf | less

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

daruma
  • 426
  • 1
  • 4
  • 8
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.

Konrad
  • 313
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.

Pooya
  • 583
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)

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