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: bzr.savannah.gnu.org/lh/emacs/trunk/annotate/head:/lisp/net/… It may be on a different line depending on which version you are using. – lawlist Dec 3 '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 3 '14 at 22:04

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-viewis 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))

(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.

  • 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 4 '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 4 '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 4 '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 4 '14 at 14:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.