From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Federico Tedin Newsgroups: gmane.emacs.bugs Subject: bug#33684: DocView bombs out upon password protected PDFs Date: Sun, 03 Feb 2019 13:55:14 -0300 Message-ID: <87zhrcyfpp.fsf@gmail.com> References: <87d0qb3rb1.fsf@jidanni.org> <87sgxesv6u.fsf@gmail.com> <83womjyhrw.fsf@gnu.org> <87h8dn7r6k.fsf@gnu.org> <83pnsbyeyt.fsf@gnu.org> <875zu1x607.fsf@gmail.com> <87lg2x5kpj.fsf@gnu.org> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="41072"; mail-complaints-to="usenet@blaine.gmane.org" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.1 (gnu/linux) Cc: 33684@debbugs.gnu.org, jidanni@jidanni.org To: Tassilo Horn Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Sun Feb 03 17:56:12 2019 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([209.51.188.17]) by blaine.gmane.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gqL3z-000AYf-RQ for geb-bug-gnu-emacs@m.gmane.org; Sun, 03 Feb 2019 17:56:12 +0100 Original-Received: from localhost ([127.0.0.1]:57967 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gqL3y-0005iQ-SG for geb-bug-gnu-emacs@m.gmane.org; Sun, 03 Feb 2019 11:56:10 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:38235) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gqL3r-0005i8-Tl for bug-gnu-emacs@gnu.org; Sun, 03 Feb 2019 11:56:04 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gqL3q-0006X3-OM for bug-gnu-emacs@gnu.org; Sun, 03 Feb 2019 11:56:03 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]:58449) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gqL3q-0006Wk-K5 for bug-gnu-emacs@gnu.org; Sun, 03 Feb 2019 11:56:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1gqL3q-0006sr-Fy for bug-gnu-emacs@gnu.org; Sun, 03 Feb 2019 11:56:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Federico Tedin Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 03 Feb 2019 16:56:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 33684 X-GNU-PR-Package: emacs Original-Received: via spool by 33684-submit@debbugs.gnu.org id=B33684.154921293326419 (code B ref 33684); Sun, 03 Feb 2019 16:56:02 +0000 Original-Received: (at 33684) by debbugs.gnu.org; 3 Feb 2019 16:55:33 +0000 Original-Received: from localhost ([127.0.0.1]:57730 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1gqL3N-0006s3-4r for submit@debbugs.gnu.org; Sun, 03 Feb 2019 11:55:33 -0500 Original-Received: from mail-qk1-f176.google.com ([209.85.222.176]:46522) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1gqL3L-0006rm-50 for 33684@debbugs.gnu.org; Sun, 03 Feb 2019 11:55:31 -0500 Original-Received: by mail-qk1-f176.google.com with SMTP id q1so6987173qkf.13 for <33684@debbugs.gnu.org>; Sun, 03 Feb 2019 08:55:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version; bh=e6uKqwc+498wLNUSlxCwd7HEEsgn25FCg3MifNi3/R4=; b=ZP3Rpusz2M0xOcVWbgZk3RAJ4Wg8LvJXW27MHz71jU32nm12lOxdk5xb7fESKj+G/r Pg5GKir3w/Wksc8pyJnqXG7KzRruT6PBgdgrB/IM1kA7NJko21wocI3B6HPflRhlaXwH qvrOX25a/uj51egoyXp2WcJz51ETtzysdiTs6l5Ep5S1wv/tU4MIjdT3EmgzcrQ5aI7G 95+WxE+dEFsmAzFyKHv54dR45z807Fy2rrNd/bOxPl+LxX/+xaeYhRsZDQCHTYVPjYRs 0uFlsQjNO9/nzSUFoPAs/WT5dhwZ83q+zWdQKNv7xqIK0b0ZasdTzLmniYwBXxDl1z// 61JQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:references:date:in-reply-to :message-id:user-agent:mime-version; bh=e6uKqwc+498wLNUSlxCwd7HEEsgn25FCg3MifNi3/R4=; b=FfMZKPli/+msWzCIiqQ+65fpgo9GE7GCOdNgKcheiQ4igNFTGArvRwLCiEOfYMjU6Q tsAiPzjjBMd2K6ei6PBuDW0TghXMbBNN02pv8zcDhw2eHpCCsirdmEAsuJGig3WrErJH qNXiL5LfaokoLZCpCZRJ7cYnXWEDsrwJjhS09yuf1J0+dzAElCiCeKtTwA8Z49xb1/Fi ZMGRO2D0Ovmm5LNQmZvWC0y0Vv744cyKJp5ijJK56ksVnxkwNBEq7YH5DkB6jWcHt4GK nypn7lWsJoWdzmu50DeArmmg22qKBdvBBDoFV6lr9YLt8EqlzhFkCLbD43c0Hi4w8JXJ 3EpQ== X-Gm-Message-State: AJcUukeQWtgi2epKf9FeaVrZOYkG4jImFRxC0e5oiB1B257wmA5jejHX 4asSJF7ekZa8SqyVzA3m4RE= X-Google-Smtp-Source: ALg8bN6q6m/AhUhGkyQc7TyyPuSI+GMs7DWhqgxk8eFTbfQCzpfyaDK5OgFFYtGHdlJSxIOMakA6PQ== X-Received: by 2002:a37:bf81:: with SMTP id p123mr43254068qkf.125.1549212925560; Sun, 03 Feb 2019 08:55:25 -0800 (PST) Original-Received: from hideo ([181.167.176.220]) by smtp.gmail.com with ESMTPSA id m68sm12969719qte.49.2019.02.03.08.55.22 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sun, 03 Feb 2019 08:55:24 -0800 (PST) In-Reply-To: <87lg2x5kpj.fsf@gnu.org> (Tassilo Horn's message of "Sun, 03 Feb 2019 09:40:08 +0100") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.51.188.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.org gmane.emacs.bugs:155052 Archived-At: --=-=-= Content-Type: text/plain Hi Tassilo, thanks for your feedback. I've created a new patch with the commit message, and also added an entry to NEWS. I'm attaching it here. - Federico --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=pdf.patch Content-Description: patch >From 0fb8d45a3ab0868e0f1d81f00b0f43771510deb1 Mon Sep 17 00:00:00 2001 From: Federico Tedin Date: Sun, 3 Feb 2019 13:48:31 -0300 Subject: [PATCH 1/1] Allow doc-view to open password-protected PDF files - lisp/doc-view.el (doc-view-ghostscript-options): Removed "-sDEVICE" option. (doc-view-ghostscript-device): New customizable variable, passed as "-sDEVICE" option to GhostScript. (doc-view-pdf-password-protected-ghostscript-p): New function. (doc-view-pdf->png-converter-ghostscript): Can now open password-protected PDF files. (doc-view-pdfdraw-program-subcommand): New function. (doc-view-pdf-password-protected-pdfdraw-p): New function. (doc-view-pdf->png-converter-mupdf): Can now open password-protected PDF files. - etc/NEWS: Mention new doc-view-mode feature. --- etc/NEWS | 1 + lisp/doc-view.el | 79 ++++++++++++++++++++++++++++++++++++------------ 2 files changed, 60 insertions(+), 20 deletions(-) diff --git a/etc/NEWS b/etc/NEWS index cac379fe7e..8aa646776c 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -329,6 +329,7 @@ the node "(emacs) Directory Variables" of the user manual. ** doc-view-mode *** New commands doc-view-presentation and doc-view-fit-window-to-page +*** Added support for password-protected PDF files ** map.el *** Now also understands plists. diff --git a/lisp/doc-view.el b/lisp/doc-view.el index df8a9fc70f..a5fdb91a36 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" + "-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,31 @@ doc-view-dvi->pdf (list "-o" pdf dvi) callback))) +(defun doc-view-pdf-password-protected-ghostscript-p (pdf) + "Using Ghostscript, check if a PDF file is password-protected. If it +is, return non-nil." + (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-ghostscript-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) @@ -980,17 +1000,36 @@ doc-view-djvu->tiff-converter-ddjvu ,tiff) callback)) +(defun doc-view-pdfdraw-program-subcommand () + "Return the mutool subcommand replacing mudraw. +Recent MuPDF distributions replaced 'mudraw' with 'mutool draw'." + (when (string-match "mutool[^/\\]*$" doc-view-pdfdraw-program) + '("draw"))) + +(defun doc-view-pdf-password-protected-pdfdraw-p (pdf) + "Using MuPDF, check if a PDF file is password-protected. If it is, +return non-nil." + (with-temp-buffer + (apply #'call-process doc-view-pdfdraw-program nil (current-buffer) nil + `(,@(doc-view-pdfdraw-program-subcommand) + ,(concat "-o" null-device) + ;; In case PDF isn't password-protected, "draw" only one page. + ,pdf "1")) + (goto-char (point-min)) + (search-forward "error: cannot authenticate password" nil t))) + (defun doc-view-pdf->png-converter-mupdf (pdf png page callback) - (doc-view-start-process - "pdf->png" doc-view-pdfdraw-program - ;; FIXME: Ugly hack: recent mupdf distribution replaced "mudraw" with - ;; "mutool draw". - `(,@(if (string-match "mutool[^/\\]*$" doc-view-pdfdraw-program) '("draw")) - ,(concat "-o" png) - ,(format "-r%d" (round doc-view-resolution)) - ,pdf - ,@(if page `(,(format "%d" page)))) - callback)) + (let ((pdf-passwd (if (doc-view-pdf-password-protected-pdfdraw-p pdf) + (read-passwd "Enter password for PDF file: ")))) + (doc-view-start-process + "pdf->png" doc-view-pdfdraw-program + `(,@(doc-view-pdfdraw-program-subcommand) + ,(concat "-o" png) + ,(format "-r%d" (round doc-view-resolution)) + ,@(if pdf-passwd `("-p" ,pdf-passwd)) + ,pdf + ,@(if page `(,(format "%d" page)))) + callback))) (defun doc-view-odf->pdf-converter-unoconv (odf callback) "Convert ODF to PDF asynchronously and call CALLBACK when finished. -- 2.17.1 --=-=-=--