12

When I open pdf with emacs, it got frozen. And I installed pdf-tools, open pdf, it got frozen again.

What's the underlining process when emacs open a pdf? Is there a lot of converting work? What should I do to speed it up?

  • Emacs 24.4
  • Ubuntu 14.04 LTS
  • Intel 2.4GHz x 2
  • 4G RAM.

Update:

I just found this page:

"learnt a lesson. Never open a pdf in emacs. Never even do it accidentally."

https://twitter.com/ergoemacs/status/456088661059457024

It's a sad news to me! Isn't it a great idea to view pdf using Emacs?

Update 2

I thought it was because my pdf file is too large:

5.7M, 1313 pages

So I tried a small pdf file,

402K, 66 pages

it got frozen again.

Update 3

I open pdf file using C-c C-x from helm, and it opens the pdf file in an external viewer: zathura. Since there is no converting process, the file opens lighting fast. (As fast as mupdf. I guess zathura using mupdf as its back end.)

There are four points makes zathura outstanding:

  1. Lighting fast. (Both open and search)
  2. High quality display.
  3. Tab displays the chapter/section menu just like Adobe Reader.
  4. Good keyboard shortcuts support. (Actually, it uses vim-style keybinding, which make sense since there is no need to switch mode in pdf viewer.

Update 4

Screen shot for quality comparasion:

!Left: Zathura; Right: PDF in Emacs

I really got confused what Emacs uses for open PDF. (I've already removed pdf-tools)

Nick
  • 4,423
  • 4
  • 24
  • 41
  • Don't you want to tell us more about your Emacs config related to pdf files? – Nsukami _ Dec 24 '14 at 09:26
  • @Nsukami_ Thanks for asking. Actually I have nothing related to pdf file in my configuration. 1. I open pdf with default installation. 2. Installed pdf-tools, open pdf. – Nick Dec 24 '14 at 09:32
  • You can use Emacs [as PDF reader](http://tuhdo.github.io/static/emacs-read-pdf.gif) fine. I now use Emacs to read PDF exclusively. I also use Ubuntu. But how large is your PDF file? – Tu Do Dec 24 '14 at 09:42
  • @TuDo It's 5.7M, 1313 pages. – Nick Dec 24 '14 at 09:44
  • @TuDo I tried a small pdf file, 402K, 66 pages, it got frozen again. So I guess may be there's something wrong with the configuration. How do I know whether I am using pdf-tools or the default pdf package shipped with emacs? – Nick Dec 24 '14 at 09:47
  • Do you have `linum-mode` enabled when opening PDF? – Tu Do Dec 24 '14 at 09:50
  • 4
    Clearly, it's time for `emacs -Q` again. – wasamasa Dec 24 '14 at 09:53
  • @TuDo Yes. I disabled `linmu-mode` and restart emacs. Open the pdf, a poor quality pdf displayed. and the plating message says: `Page 21 of page 1312 (Still converting...)`. – Nick Dec 24 '14 at 09:58
  • For such many number of pages, it may took a while (not long) but it didn't hang again, correct? Did you try it with smaller PDF? Also, if you use `pdf-view-mode`, it gives better quality, render faster and always resize to full buffer where the PDF file is opened. – Tu Do Dec 24 '14 at 10:05
  • @wasamasa @TuDo I killed emacs and start it with `emacs -Q`, open the pdf file again. It open the PDF file with poor quality. Yes, this time, no frozen. I saw it is in DocView mode. How can I use pdf-view-mode? – Nick Dec 24 '14 at 10:15
  • I guess, open in external program is not an option :\ – Nsukami _ Dec 24 '14 at 10:21
  • @Nick just follow the build instructions provided by [pdf-tools](https://github.com/politza/pdf-tools#installation) and it should work. – Tu Do Dec 24 '14 at 13:29
  • @TuDo I installed `pdf-tools` using `el-get` and I saw the message that `pdf-tools` is successfully installed. Before I build it again, I would like to ask the way Emacs display PDFs: Does it convert it to image first? That is, it doesn't display pdf, it display image? – Nick Dec 24 '14 at 13:43
  • @Nick Yes, it converts the PDF file to images in a temporary directory and displays one image after another in the docview buffer. That's why it's basically a giant hack compared to just opening an external PDF viewer. – wasamasa Dec 25 '14 at 08:56
  • 2
    To install `pdf-tools`, you have to clone the repo, run `./autogen.sh` and follow the standard procedure: `./configure` && `make` && `sudo make install`. Then, it will produce a `pdf-tools.tar.gz` in the pdf-tools directory you cloned. Then, put `(pdf-tools-install)` somewhere in your init file. From now on, Emacs will use `pdf-view-mdoe` which gives better quality and better rendering speed. – Tu Do Dec 25 '14 at 15:26
  • Here is a [demo using pdf-tools](http://i.imgur.com/wu8pMoN.gif). The PDF file is 28MB and it took a few seconds to open. Note that the quality of the images in the gif are a bit lower quality compared with the actual source, but as you can see the text is crystal clear. I did not record the whole screen to reduce the size of the gif, because it's already 13MB. – Tu Do Dec 26 '14 at 02:50
  • @TuDo With the help of your previous comment, I installed `pdf-tools` from scratch. It works! Both of the opening speed and display quality is almost identical to `zathura`. This is really exciting! Thank you very much. The only problem is the search. By default, it uses incremental search. The search hangs several times. I have to wait for it to stop. What kind of search do you use? – Nick Dec 26 '14 at 07:54

2 Answers2

15

You should disable linum-mode when opening a PDF file. Otherwise it will hang your Emacs. i.e. only add it to prog-mode for editing text:

(add-hook 'prog-mode-hook 'linum-on)

To install pdf-tools:

  • You have to clone the repository:

    git clone https://github.com/politza/pdf-tools.git

  • cd pdf-tools

  • Run make.

  • Then, it will produce a pdf-tools-${VERSION}.tar in the pdf-tools directory you cloned; in Emacs, M-x package-install-file RET pdf-tools-${VERSION}.tar RET

  • Then, put (pdf-tools-install) somewhere in your init file. From now on, Emacs will use pdf-view-mode which gives better quality and better rendering speed.

Here is a demo using pdf-tools. The PDF file is 28MB and it took a few seconds to open. Note that the quality of the images in the gif are a bit lower quality compared with the actual source, but as you can see the text is crystal clear. I did not record the whole screen to reduce the size of the gif, because it's already 13MB.

I use plain Isearch using C-s as in other buffers for searching. Don't press too fast, leave about 1 second delay between searches. You should use Isearch to search current page or nearby pages, basically something small. To search the whole document, use pdf-occur; the command is quite fast. You may want to keep the search results for later use (in the same session) by renaming the occur default *PDF-Occur* buffer to some other name to avoid it is overridden by future searches.

If you want the table of contents, in the PDF buffer press o to open it in outline-mode.

Tu Do
  • 6,772
  • 20
  • 39
  • Hat tips to this great package (and answer). The difference is huge if you compare it with the default viewer. – KookieMonster Jun 05 '15 at 20:09
  • `(add-hook 'prog-mode-hook 'linum-on)` should be `(add-hook 'prog-mode-hook 'linum-mode)` on 24.5.1. – Yang Feb 19 '16 at 05:08
4

I’m using Emacs for reading documentation, scientific papers and books — all of that without issues.

My doc-view-mode uses mudraw (from mupdf) for rendering PDFs as you can see by checking doc-view-pdfdraw-program variable:

doc-view-pdfdraw-program is a variable defined in `doc-view.el'. Its value is "mudraw" Documentation: Name of MuPDF's program to convert PDF files to PNG. This variable was introduced, or its default value was changed, in version 24.4 of Emacs.

By default doc-view mode uses ghotscript’s gs for rendering which can be slow in comparison. You can read about switch from ghostscript’s gs to mupdf’s mudraw:

I've since been testing that patch and never looked back since the switch sped up the pdf->png conversion by a factor of two and brought anti-aliasing with it.

In your situation:

  1. install mupdf and open PDF in an emacs session without your configuration e.g.:

    env HOME=/tmp emacs -Q
    

    and check if doc-view-pdfdraw-program is set correctly to mudraw.

  2. report back if above improved your situation.

PS I have no experience with pdf-tools.

kmicu
  • 2,395
  • 1
  • 15
  • 15
  • I have mupdf installed, and it's surprisingly fast. I run env HOME=/tmp emacs -Q, emacs sais "still converting…", and takes a while to convert. The speed is fine but the quality is poor. To my surprise, there's no variable named doc-view-pdfdraw-program. If Emacs can use mupdf, that would be really terrific! – Nick Dec 24 '14 at 22:45
  • Are you sure that you are using 24.4? *doc-view-pdfdraw-program* should be available in such case. Is *mudraw* available in your shell? – kmicu Dec 25 '14 at 00:25
  • Yes, I'm running Emacs 24.4. `This is GNU Emacs 24.4.2 (x86_64-unknown-linux-gnu, GTK+ Version 3.10.8)` `mudraw` is not availabe, so I installed. (the package mupdf-tools), thus I can run mudraw in shell. The converting speed is even more slow, and the pdf display quality is still poor. – Nick Dec 25 '14 at 01:32
  • "To my surprise, there's no variable named doc-view-pdfdraw-program." without knowing what is set in that variable we cannot be sure if you are really using *mudraw* in doc-view mode, maybe this is still *gs*. "pdf display quality is still poor" is a relative and subjective statement - can you update your question with some screenshots? For me Zathura (with mupdf backend) has exactly the same PDF rendering quality and speed as in Emacs. – kmicu Dec 25 '14 at 21:22
  • Thank you for your help. I've updated a screen shot with zathura and emacs side by side. – Nick Dec 26 '14 at 00:32
  • 1
    To get that variable, you have to load it: `(require 'doc-view)`. I think `mudraw` is the default and the problem is that `doc-view` cannot fit the whole PDF page into the whole buffer. And I'm not sure about the quality. – Tu Do Dec 26 '14 at 08:30
  • *doc-view* with *mudraw* will render PDF based on your frame size. It will fit it to your liking - whole page visible, full page width, etc. – kmicu Dec 26 '14 at 19:45
  • For me the speed is horribly unusable. It needs maybe 2s or 3s for converting, then it shows the picture of a pdf page, which doesn't really look crisp, but blurred. That would be ok, if I was able to scroll through the pages at all. It takes as much time to do one scroll step, as it took initially to render the stuff. This is not usable. In my config I added `(setq-default doc-view-pdfdraw-program "mudraw")` and emacs didn't report any errors on that, so I guess `mudraw` is a no go for me. – Zelphir Kaltstahl Feb 18 '17 at 16:10
  • Maybe some unrelated plugin slows things down; did you check if you experience the same problem with `emacs -Q`? Here it works without any noticeable lag. – kmicu Mar 07 '17 at 11:04
  • @kmicu, tried mudraw in my emacs, noticed no delay but the pdf quality is somewhat low (I might say not comparable to zathura (installed via homebrew) on my Macbook Pro), any idea on this? – phye Jul 25 '17 at 04:15
  • I cannot reproduce the problem (I did not test on MacOS). Could you try Zathura with MuPDF as a rendering backend https://pwmt.org/projects/zathura-pdf-mupdf/ to check if you are able to see a quality loss? – kmicu Jul 26 '17 at 18:51