I use pdf-tools to read PDFs inside of Emacs and greatly prefer it to DocView. I just noticed that when I try to open a URL thar points to a PDF in eww, it uses DocView instead of the pdf-view-mode from pdf-tools. This is particularly annoying since DocView fails to display the PDF! Switching the buffer to pdf-view-mode manually then succesfully shows it. Can I configure things so that eww uses pdf-view-mode on the first try?
-
Take a look at line 403 of the source code in the attached link: http://bzr.savannah.gnu.org/lh/emacs/trunk/annotate/head:/lisp/net/eww.el It may be on a different line depending on which version you are using. – lawlist Dec 03 '14 at 21:24
-
Thanks, @lawlist, after I asked the question I did find that was where `doc-view-mode` was called by setting `(debug-on-entry 'doc-view-mode)`. – Omar Dec 03 '14 at 22:04
-
3Does this answer your question? [How to use pdf-tools (pdf-view-mode) in emacs?](https://emacs.stackexchange.com/questions/19686/how-to-use-pdf-tools-pdf-view-mode-in-emacs) – Tyler Jan 14 '20 at 20:10
-
@Tyler, I'm not sure but I won't investigate whether it does or not because (1) this question was already answered in the year 2014, as you verify by scrolling down to the answer I accepted, (2) even that answer is unnecessary nowadays since with more recent versions of Emacs and pdf-tools what I wanted works out the box! (so I removed the code kindly suggested by T. Verron from my init.el a few years ago). – Omar Jan 15 '20 at 17:51
-
@Tyler Curiosity got the better of me and I read the question you suggested. The accepted answer says that this works automatically, which as I mentioned above is correct now, but wasn't in 2014 when I asked my question. So maybe the answer to whether it answers my question is "It would had I asked it now, but it didn't back in 2014". :) – Omar Jan 15 '20 at 17:54
-
1@Omar I think the phrase "Does this answer your question?" gets added automatically when you flag a question as a duplicate now? I didn't enter it by hand, anyways. I did flag this as a duplicate, because the linked question serves as the canonical answer to questions of the form "how do I get pdf-tools to work for X". Of course, your question was asked and answered before the other question was, but I think it's normal to do this when a comprehensive general answer addresses an older, more specific question. – Tyler Jan 15 '20 at 22:13
3 Answers
Warning: dirty work-around ahead
Since you "greatly prefer [pdf-tools] to DocView", is it safe to assume that using it to view all pdfs is acceptable?
The following code snippet will switch the document to pdf-view-mode
anytime doc-view
is entered with a pdf
document.
(defvar tv/prefer-pdf-tools (fboundp 'pdf-view-mode))
(defun tv/start-pdf-tools-if-pdf ()
(when (and tv/prefer-pdf-tools
(eq doc-view-doc-type 'pdf))
(pdf-view-mode)))
(add-hook 'doc-view-mode-hook 'tv/start-pdf-tools-if-pdf)
The behavior can be enabled or disabled by setq
-ing the variable tv/prefer-pdf-tools
to t or nil.

- 4,233
- 1
- 22
- 55
-
Oh, I had missed that `doc-view-mode` had a hook! I tried adivising `eww-display-pdf` and that didn't work (not sure why). I'll try your solution and report back. – Omar Dec 04 '14 at 13:11
-
Awesome, thanks! This does work (after you remove the parenthesis around `tv/prefer-pdf-tools`, which is not a function). – Omar Dec 04 '14 at 13:17
You can use an advise around eww-display-pdf
to override the definition of doc-view-mode
temporarily. With the new nadvice
library this is as easy as:
(advice-add 'eww-display-pdf
:around (lambda (orig &rest args)
(cl-letf (((symbol-function 'doc-view-mode) #'pdf-view-mode))
(apply orig args)))
'((name . eww-display-pdf-tools)))
To revert back to the original doc-view-mode
, use (advice-remove 'eww-display-pdf 'eww-display-pdf-tools)
.
I have opened Emacs bug 19270 to make the EWW PDF Mode customizable.
-
Cool! I tried adding advice to `eww-display-pdf` to just make it run `pdf-view-mode` `:after` it did its usual work, but that didn't work for some reason. It never occurred to me to swap out the definition of the `doc-view-mode` function (probably because I'm so used to lexical scope!). – Omar Dec 04 '14 at 14:28
-
Thanks for opening the bug report too. I would imagine lots of people would actually prefer eww to open PDFs in their customary external PDF viewer. – Omar Dec 04 '14 at 14:28
I use emacs-28, eww use mailcap to open pdf, custom it by open with pdf-tools:
(add-to-list 'mailcap-user-mime-data
'((type . "application/pdf")
(viewer . pdf-view-mode)))
See Emacs - Help - pdf viewer for more information.

- 151
- 4
-
Are you from the future? I don't think Emacs 27 has been released yet (that's the pre-release build I'm using), let alone 28. – Omar Jan 14 '20 at 15:23
-
I use archlinux with the development version of emacs: aur/emacs-git 28.0.50.139945-1 [installed: 28.0.50.140001-1] (74) (1.07) GNU Emacs. Development master branch. – xinfa tang Jan 15 '20 at 03:18
-