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?
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.
-
Oh, I had missed that
doc-view-mode
had a hook! I tried adivisingeww-display-pdf
and that didn't work (not sure why). I'll try your solution and report back.– OmarDec 4, 2014 at 13:11 -
Awesome, thanks! This does work (after you remove the parenthesis around
tv/prefer-pdf-tools
, which is not a function).– OmarDec 4, 2014 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 runpdf-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 thedoc-view-mode
function (probably because I'm so used to lexical scope!).– OmarDec 4, 2014 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.– OmarDec 4, 2014 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.
-
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.– OmarJan 14, 2020 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. Jan 15, 2020 at 3:18
-
doc-view-mode
was called by setting(debug-on-entry 'doc-view-mode)
.