69

Hey, for quite a while now, I am looking for a pdf viewer for the command line.

As I like to work without X on Linux, and often work on a remote machine, I would like to have a tool to read pdfs. There are quite a lot of really good graphical programs (evince, okular, acroread, ...) to do the job, so I figured there should be at least one decent text-mode tool. But I don't even know of a crappy one!

Currently, I either start X only to read pdfs, or use pdftohtml+lynx. However, the latter does not produce a very good output, and most documents are just unreadable, especially if they contain mathematical formula.

Google is full of people saying either it's not possible or suggesting the pdftohtml version.

I realise, this is not exactly a programming question, but I am currently considering starting a project to implement such a program, unless there already is a good one out there.

Thanks for any suggestions.

Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
bitmask
  • 32,434
  • 14
  • 99
  • 159
  • How would it handle pdfs that are built from image source (no text), which is waaaayyy too common? – µBio Aug 25 '10 at 22:06
  • Some PDFs are converted to a bad text!, either just shapes or OCR output that is hidden over the source image for the sake of selection only. Which can never be converted to a good text! How this you think should be handled? – Omar Al-Ithawi Aug 25 '10 at 22:12
  • 1
    @0xA3: well, there ARE libraries capable of rendering graphics to the console - libcaca and aalib. It is even possible to see movies in ascii art. Haven't heard about pdf viewer like that, though. Also, it is possible to render images to framebuffer using svgalib. Haven't heard about CLI pdf viewer though. Obviously it can be done, but I'm not sure if anyone actually bothered to do that. Oh, and with libcaca/aalib "graphic" pdf will be barely readable. – SigTerm Aug 25 '10 at 22:16
  • @Omar Dolaimy & Lucas Heneks: Well, I am aware, that there are pdfs, that contain only paths. But as SigTerm suggests, *it can be done* and it is done for the sake of searching the web - google surely does it for indexing pdfs. – bitmask Aug 25 '10 at 22:21
  • 2
    By the way, i m always in the same situation, and i use mc (midnight commander) which handles text pdf's very well... Just view the file (F3) in mc – mlwn Aug 29 '14 at 11:44
  • @mlwn: Why not turn your comment into an answer? – bitmask Aug 29 '14 at 11:57
  • @bitmask, I felt like the question is answered :) I just commented what I do in this case... I'll paste it as answer as well.. Cheers.. – mlwn Aug 29 '14 at 20:18
  • You could write a script that converts each page to a jpeg, opens fbi on the jpegs, then erases the jpegs. It would be pretty heavy, but it seems like it would be reasonable for a lot of documents. Directly writing a program to view pdf's would be a lot of work. – Kyle May 25 '16 at 01:44
  • Same as: https://unix.stackexchange.com/questions/41362/view-pdf-file-in-terminal – 0 _ Aug 26 '17 at 01:45
  • since the question was closed (although this is pretty interesting), here is one more answer: use ranger. Pretty simple to use, you can scroll up and down, but the downside is that you can copy & paste (unless you drag your mouse over the text) or highlight. – Ricardo Pietrobon Aug 09 '18 at 02:21

7 Answers7

85

Hi I think that you don't need to write a program for your purpose I mean reading pdf file in console mode because less command already do it for you. So use it and just enjoy it.

less "the name of pdf file"
phuclv
  • 37,963
  • 15
  • 156
  • 475
Kasra
  • 882
  • 7
  • 2
  • 14
    Neat trick! On archlinux I had to install `lesspipe` for this to work, other distibutions may have it installed by default. +1 – Giacomo Mar 05 '12 at 09:24
  • 7
    isn't pdf a binary format? The text produced by `less` should be garbled – akhy Mar 10 '13 at 05:41
  • 30
    On some systems (tested on Debian), the command `lesspipe | less` works. – Abbafei Apr 29 '13 at 05:14
  • @kasra None of the `less` and `lesspip` didn't work for me on ubuntu12.04?? How to open the pdf files? – shgnInc Feb 01 '14 at 11:14
  • 10
    For people like me who found this and get the error "no pdftottext available" just `apt-get install pdftohtml` (or yum...etc depending on your distro) to get this to work. – Mike McMahon Sep 18 '14 at 23:11
  • @akhyar: less automatically uses pdftotext to convert binary to text. – Frank Kusters Feb 09 '15 at 10:11
  • I would recommend look to the answer bellow that explains `pdftotext` tool. – DrBeco Aug 23 '15 at 18:09
  • 3
    Use `sudo apt-get install poppler-utils` if pdftohtml is not available. – Felipe Nov 09 '15 at 21:04
  • Downvoted, because the answer isn't complete. Its using tools behind other tools. lesspipe check the mimetype of the document and start a configured tool. If this is not configured, it will **not** work. – silvio Apr 10 '18 at 08:58
  • What? I have been in the Linux terminal for more than 14 years now. How come I never tried it? – Shiplu Mokaddim Nov 18 '19 at 11:16
  • `less` not doing properly on my Linux it show broken texts that look like trying `cat binaryFile` – Blanket Fox Dec 26 '20 at 04:03
26

Ok, you asked to know even "crappy" ones. Here are two (decide yourself about their respective crappiness):

First: Ghostscript's txtwrite output device

 gs \
   -dBATCH \
   -dNOPAUSE \
   -sDEVICE=txtwrite \
   -sOutputFile=- \
   /path/to/your/pdf

Second: XPDF's pdftotext CLI utility (better than Ghostscript):

 pdftotext \
   -f 13 \
   -l 17 \
   -layout \
   -opw supersecret \
   -upw secret \
   -eol unix \
   -nopgbrk \
   /path/to/your/pdf
   - |less

This will display the page range 13 (first page) to 17 (last page), preserve the layout of a double-password protected named PDF file (using user and owner passwords secret and supersecret), with Unix EOL convention, but without inserting pagebreaks between PDF pages, piped through less...

pdftotext -h displays all available commandline options.

Of course, both tools only work for the text parts of PDFs (if they have any). Oh, and mathematical formula also won't work too well... ;-)


Edit: I had mis-typed the command above (originally using pdftops instead of pdftotext).

Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
  • Neither of them works for my system, I tried tweaking the arguments but it did not display the text but a ps formatted file, and since I am not a printer ... – bitmask Sep 03 '10 at 21:26
  • D'oh! I mis-typed the command. Use `pdftotext` instead of `pdftops`... (I'll edit the answer to reflect this correction). – Kurt Pfeifle Sep 06 '10 at 10:01
  • Similarly, `pdftohtml` makes an HTML copy, which can be viewed with a text mode web browser, for example `w3m` or `links` . – Abbafei Apr 29 '13 at 05:17
  • 2
    The Ghostscript command works great and works a lot better than that stupid answer above that says to use less. This should be the highest rated answer. – Tatsh May 27 '14 at 15:49
  • 2
    This one here should be the accepted answer. More precisely, `pdftotext` to the job gracefully. Specially with `-layout`. – DrBeco Aug 23 '15 at 18:08
  • `pdftotext` is included with `poppler-utils` on Fedora Linux. – Kevin Feb 14 '23 at 21:22
5

There is also the green PDF viewer. There is a demo on YouTube.

user287424
  • 1,219
  • 12
  • 22
5

By the way, i m always in the same situation, and I use mc (midnight commander) which handles text pdf's very well... Just view the file (F3) in mc

mlwn
  • 1,156
  • 1
  • 10
  • 25
5

Try fbgs, which should be provided by the fbi or fbida package depending on your distribution. Note that it only works in real terminals (ttys).

http://web.archive.org/web/20150316143120/http://linuxers.org/howto/how-open-pdf-files-linux-console-using-fbgs-framebuffer-pdf-viewer

xeruf
  • 2,602
  • 1
  • 25
  • 48
Giacomo
  • 11,087
  • 5
  • 25
  • 25
  • This is awesome! Although you have to be root, which I don't understand, but okay. Still awesome. – bitmask Sep 03 '10 at 21:27
  • Check the permissions of the framebuffer device: `$ls -l /dev/fb0 crw-rw---- 1 root video ...`. You may need to be in the `video` group in order to have user access to the framebuffer. – Giacomo Sep 18 '10 at 17:18
4

fbpdf is a framebuffer pdf viewer.

There is also a fork, jfbpdf, but at the moment I am not able to get it working.

  • This is the answer that will show you the pdf as it was designed to be seen, with the linux framebuffer, on the commandline. Specifically I have used jfbpdf with good success, and it appears to be in active development. – fivestones Dec 14 '20 at 08:28
-1

This would only work if your PDF document is structured, i.e. it is a tagged PDF document.

This is required to get the correct reading-order of the text objects in the document.

Tagged PDF documents also allow your to re-flow the document though I am not aware of any tool doing that with command line output.

Dirk Vollmar
  • 172,527
  • 53
  • 255
  • 316