Bill Clementson's Blog

Bits and pieces (mostly Lisp-related) that I collect from the ether.

August 2007
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Jul  Sep

View PDF/PS/DVI files in an Emacs buffer

Thursday, August 30, 2007

Tassilo Horn recently developed doc-view.el: an Emacs utility for viewing PDF/PS/DVI files in Emacs. It does this by using Ghostscript to convert the document pages to individual PNG images. This utility is really cool! The quality of the output is on par with the original document and you can view the pages inside of an Emacs buffer without switching to an external utility.

doc-view.el

The above image gives you an idea of what the resulting output looks like; however, I've also created a short demo movie (~ 16MB) if you want to get a better idea. One caveat: in the movie, I am displaying documents that I've viewed with doc-view previously - thus the documents display almost instantaneously as the PNG files have already been created. However, even if you're viewing a document for the first time, display of the pages is pretty fast as the conversion process is done in the background and doc-view will display the first page quickly and subsequent pages as they become available.

The utility is still under development (there is rudimentary text search functionality at the moment and some additional features might still be added) but it is supposed to be incorporated into CVS Emacs shortly, so you will be able to get it (at some point in the future) as part of CVS Emacs. However, if you want to try it out now, you can get doc-view.el from Tassilo Horn's doc-view.el Git repository. For anyone who has hated having to switch to an external viewed to view PDF and PS files, this utility is the bee's knees!

Below are the "Commentary" and "Configuration" comments from doc-view.el so that (if you don't have Git installed) you can get an idea of the functionality that is provided:

;;; Commentary:
;; DocView is a document viewer for Emacs. It converts PDF, PS and DVI files ;; to a set of PNG files, one PNG for each page, and displays the PNG images ;; inside an Emacs buffer. This buffer uses `doc-view-mode' which provides ;; convenient key bindings for browsing the document. ;; ;; To use it simply do ;; ;; M-x doc-view RET ;; ;; and you'll be queried for a document to open. ;; ;; Since conversion may take some time all the PNG images are cached in a ;; subdirectory of `doc-view-cache-directory' and reused when you want to view ;; that file again. This reusing can be omitted if you provide a prefx ;; argument to `doc-view'. To delete all cached files use ;; `doc-view-clear-cache'. To open the cache with dired, so that you can tidy ;; it out use `doc-view-dired-cache'. ;; ;; When conversion in underway the first page will be displayed as soon as it ;; is available and the available pages are refreshed every ;; `doc-view-conversion-refresh-interval' seconds. If that variable is nil the ;; pages won't be displayed before conversion of the document finished ;; completely. ;; ;; DocView lets you select a slice of the displayed pages. This slice will be ;; remembered and applied to all pages of the current document. This enables ;; you to cut away the margins of a document to save some space. To select a ;; slice you can use `doc-view-set-slice' (bound to `s s') which will query you ;; for the coordinates of the slice's top-left corner and its width and height. ;; A much more convenient way to do the same is offered by the command ;; `doc-view-set-slice-using-mouse' (bound to `s m'). After invokation you ;; only have to press mouse-1 at the top-left corner and drag it to the ;; bottom-right corner of the desired slice. To reset the slice use ;; `doc-view-reset-slice' (bound to `s r'). ;; ;; Dired users should have a look at `doc-view-dired'. ;; ;; You can also search within the document. The command `doc-view-search' ;; (bound to `C-s') queries for a search regexp and initializes a list of all ;; matching pages and messages how many match-pages were found. After that you ;; can jump to the next page containing a match with ;; `doc-view-search-next-match' (bound to `C-S-n') or to the previous matching ;; page with `doc-view-search-previous-match' (bound to `C-S-p'). This works ;; by searching a plain text representation of the document. If that doesn't ;; already exist the first invokation of `doc-view-search' starts the ;; conversion and you have to wait till it finishes.
;;; Configuration:
;; Basically doc-view should be quite usable with its standard settings, so ;; putting ;; ;; (require 'doc-view) ;; ;; into your `user-init-file' should be enough. If the images are too small or ;; too big you should set the "-rXXX" option in `doc-view-ghostscript-options' ;; to another value. (The bigger your screen, the higher the value.) ;; ;; This and all other options can be set with the customization interface. ;; Simply do ;; ;; M-x customize-group RET doc-view RET ;; ;; and modify them to your needs.

emacs Copyright © 2007 by Bill Clementson