diff --git a/lisp/doc-view.el b/lisp/doc-view.el index df8a9fc70f..8691d2a3a6 100644 --- a/lisp/doc-view.el +++ b/lisp/doc-view.el @@ -183,11 +183,16 @@ doc-view-pdf->png-converter-function (defcustom doc-view-ghostscript-options '("-dSAFER" ;; Avoid security problems when rendering files from untrusted ;; sources. - "-dNOPAUSE" "-sDEVICE=png16m" "-dTextAlphaBits=4" - "-dBATCH" "-dGraphicsAlphaBits=4" "-dQUIET") + "-dNOPAUSE" "-dTextAlphaBits=4" "-dBATCH" + "-dGraphicsAlphaBits=4" "-dQUIET") "A list of options to give to ghostscript." :type '(repeat string)) +(defcustom doc-view-ghostscript-device "png16m" + "Output device to give to ghostscript." + :type 'string + :version "27.1") + (defcustom doc-view-resolution 100 "Dots per inch resolution used to render the documents. Higher values result in larger images." @@ -950,16 +955,30 @@ doc-view-dvi->pdf (list "-o" pdf dvi) callback))) +(defun doc-view-pdf-password-protected-p (pdf) + "Using Ghostscript, check if a PDF file is password-protected." + (with-temp-buffer + (apply #'call-process doc-view-ghostscript-program nil (current-buffer) + nil `(,@doc-view-ghostscript-options + "-sNODISPLAY" + ,pdf)) + (goto-char (point-min)) + (search-forward "This file requires a password for access." nil t))) + (defun doc-view-pdf->png-converter-ghostscript (pdf png page callback) - (doc-view-start-process - "pdf/ps->png" doc-view-ghostscript-program - `(,@doc-view-ghostscript-options - ,(format "-r%d" (round doc-view-resolution)) - ,@(if page `(,(format "-dFirstPage=%d" page))) - ,@(if page `(,(format "-dLastPage=%d" page))) - ,(concat "-sOutputFile=" png) - ,pdf) - callback)) + (let ((pdf-passwd (if (doc-view-pdf-password-protected-p pdf) + (read-passwd "Enter password for PDF file: ")))) + (doc-view-start-process + "pdf/ps->png" doc-view-ghostscript-program + `(,@doc-view-ghostscript-options + ,(concat "-sDEVICE=" doc-view-ghostscript-device) + ,(format "-r%d" (round doc-view-resolution)) + ,@(if page `(,(format "-dFirstPage=%d" page))) + ,@(if page `(,(format "-dLastPage=%d" page))) + ,@(if pdf-passwd `(,(format "-sPDFPassword=%s" pdf-passwd))) + ,(concat "-sOutputFile=" png) + ,pdf) + callback))) (defalias 'doc-view-ps->png-converter-ghostscript 'doc-view-pdf->png-converter-ghostscript)