Skip to content

Commit

Permalink
Add support for interactive PDF decryption
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Politz committed Jan 27, 2017
1 parent 9dafe31 commit 9019efc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lisp/pdf-info.el
Original file line number Diff line number Diff line change
Expand Up @@ -992,7 +992,20 @@ This command is rarely needed, see also `pdf-info-open'."
(run-hooks 'pdf-info-close-document-hook))
(with-temp-buffer
(run-hooks 'pdf-info-close-document-hook))))))


(defun pdf-info-encrypted-p (&optional file-or-buffer)
"Return non-nil if FILE-OR-BUFFER requires a password.
Note: This function returns nil, if the document is encrypted,
but was already opened (presumably using a password)."

(condition-case err
(pdf-info-open
(pdf-info--normalize-file-or-buffer file-or-buffer))
(error (or (string-match-p
":Document is encrypted\\'" (cadr err))
(signal (car err) (cdr err))))))

(defun pdf-info-metadata (&optional file-or-buffer)
"Extract the metadata from the document FILE-OR-BUFFER.
Expand Down
15 changes: 15 additions & 0 deletions lisp/pdf-view.el
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
(require 'pdf-cache)
(require 'jka-compr)
(require 'bookmark)
(require 'password-cache)


;; * ================================================================== *
Expand Down Expand Up @@ -350,9 +351,23 @@ PNG images in Emacs buffers."
'pdf-view-new-window-function nil t)
(image-mode-setup-winprops)

;; Decryption needs to be done before any other function calls into
;; pdf-info.el .
(pdf-view-decrypt-document)
;; Setup initial page and start display
(pdf-view-goto-page (or (pdf-view-current-page) 1)))

(defun pdf-view-decrypt-document ()
"Read a password, if the document is encrypted and open it."
(interactive)
(when (pdf-info-encrypted-p)
(let* ((key (concat "/pdf-tools" (buffer-file-name)))
(password (password-read (format "Enter password for `%s': "
(buffer-file-name))
key)))
(pdf-info-open nil password)
(password-cache-add key password))))

(defun pdf-view-buffer-file-name ()
"Return the local filename of the PDF in the current buffer.
Expand Down

0 comments on commit 9019efc

Please sign in to comment.