Subject: bug#44338: 27.1; EWW can't download and view pdf

From: Nicholas Harrison <[email-masked]>
Date: Wed, 4 Nov 2020 16:43:13 -0700

Not sure if this is much help, but here is the backtrace given when I do
the following steps:

1. emacs -Q
2. M-: (debug-on-entry 'select-safe-coding-system) RET
3. M-x eww RET https://www.gnu.org/software/emacs/manual/pdf/emacs-xtra.pdf
RET
(no backtrace here)
4. M-x doc-view-mode RET

Debugger entered--entering a function:
* select-safe-coding-system(1 381654 iso-latin-1-dos nil
"c:/Users/nicho/AppData/Local/Temp/docview1001/!eww pdf!")
  write-region(nil nil "c:/Users/nicho/AppData/Local/Temp/docview1001/!eww
pdf!")
  doc-view-mode()
  funcall-interactively(doc-view-mode)
  call-interactively(doc-view-mode record nil)
  command-execute(doc-view-mode record)
  execute-extended-command(nil "doc-view-mode" "doc-view-mo")
  funcall-interactively(execute-extended-command nil "doc-view-mode"
"doc-view-mo")
  call-interactively(execute-extended-command nil nil)
  command-execute(execute-extended-command)

5. ESC ESC ESC
6. RET (it asks to choose an encoding, chose default raw-text)

Debugger entered--returning value: raw-text
  select-safe-coding-system(1 381654 iso-latin-1-dos nil
"c:/Users/nicho/AppData/Local/Temp/docview1001/!eww...")
  write-region(nil nil
"c:/Users/nicho/AppData/Local/Temp/docview1001/!eww...")
  doc-view-mode()
  funcall-interactively(doc-view-mode)
  call-interactively(doc-view-mode record nil)
  command-execute(doc-view-mode record)
  execute-extended-command(nil "doc-view-mode" "doc-view-mo")
  funcall-interactively(execute-extended-command nil "doc-view-mode"
"doc-view-mo")
  call-interactively(execute-extended-command nil nil)
  command-execute(execute-extended-command)

Debugger entered--entering a function:
* select-safe-coding-system(1 383892 no-conversion nil)
  md5(#<buffer  *temp*>)
  doc-view--current-cache-dir()
  doc-view-already-converted-p()
  doc-view-initiate-display()
  doc-view-mode()
  funcall-interactively(doc-view-mode)
  call-interactively(doc-view-mode record nil)
  command-execute(doc-view-mode record)
  execute-extended-command(nil "doc-view-mode" "doc-view-mo")
  funcall-interactively(execute-extended-command nil "doc-view-mode"
"doc-view-mo")
  call-interactively(execute-extended-command nil nil)
  command-execute(execute-extended-command)

Debugger entered--returning value: no-conversion
  select-safe-coding-system(1 383892 no-conversion nil)
  md5(#<buffer  *temp*>)
  doc-view--current-cache-dir()
  doc-view-already-converted-p()
  doc-view-initiate-display()
  doc-view-mode()
  funcall-interactively(doc-view-mode)
  call-interactively(doc-view-mode record nil)
  command-execute(doc-view-mode record)
  execute-extended-command(nil "doc-view-mode" "doc-view-mo")
  funcall-interactively(execute-extended-command nil "doc-view-mode"
"doc-view-mo")
  call-interactively(execute-extended-command nil nil)
  command-execute(execute-extended-command)

Debugger entered--entering a function:
* select-safe-coding-system("100" nil prefer-utf-8 nil
"c:/Users/nicho/AppData/Local/Temp/docview1001/!eww
pdf!-2072e1249b26ee28e656f1a01f0cb4a9/resolution.el")
  write-region("100" nil
"c:/Users/nicho/AppData/Local/Temp/docview1001/!eww
pdf!-2072e1249b26ee28e656f1a01f0cb4a9/resolution...." nil silently)
  #f(compiled-function () #<bytecode 0x42abe9>)()
  doc-view-sentinel(#<process pdf/ps->png> "finished\n")

Debugger entered--returning value: prefer-utf-8-dos
  select-safe-coding-system("100" nil prefer-utf-8 nil
"c:/Users/nicho/AppData/Local/Temp/docview1001/!eww...")
  write-region("100" nil
"c:/Users/nicho/AppData/Local/Temp/docview1001/!eww..." nil silently)
  #f(compiled-function () #<bytecode 0x42abe9>)()
  doc-view-sentinel(#<process pdf/ps->png> "finished\n")

Debugger entered--returning value: prefer-utf-8-dos
  select-safe-coding-system("100" nil prefer-utf-8 nil
"c:/Users/nicho/AppData/Local/Temp/docview1001/!eww...")
  write-region("100" nil
"c:/Users/nicho/AppData/Local/Temp/docview1001/!eww..." nil silently)
  #f(compiled-function () #<bytecode 0x42abe9>)()
  doc-view-sentinel(#<process pdf/ps->png> "finished\n")

  Let me know if I was supposed to do something differently.

Nicholas

On Wed, Nov 4, 2020 at 8:07 AM Eli Zaretskii <[email-masked]> wrote:

> > From: Nicholas Harrison <[email-masked]>
> > Date: Tue, 3 Nov 2020 16:52:40 -0700
> > Cc: [email-masked]
> >
> > This can be (partially) corrected by running the following code before
> the steps 2 and 3:
> > (add-to-list 'mailcap-user-mime-data
> >              '((type . "application/pdf")
> >                (viewer . doc-view-mode)))
> >
> > This chooses a view mode for the pdf but that brings the second problem.
> This selects the default encoding
> > of raw-text and the conversion fails:
>
> You say it selects raw-text, but the screenshot you sent clearly shows
> that Emacs was trying to use iso-latin-1-dos.  In which case the
> failure is easily understandable, but I don't immediately see where
> did that value come from (it's most probably the default value of
> buffer-file-coding-system for you, but since eww-display-pdf binds
> coding-system-for-write, the question is why that value isn't being
> used).  Could you perhaps produce a backtrace from that situation?
> For example, like this:
>
>   M-: (debug-on-entry 'select-safe-coding-system) RET
>
> and then repeat the recipe.
>
> In any case, this isn't right:
>
>   (defun eww-display-pdf ()
>     (let ((data (buffer-substring (point) (point-max))))
>       (pop-to-buffer-same-window (get-buffer-create "*eww pdf*"))
>       (let ((coding-system-for-write 'raw-text)  <<<<<<<<<<<<<<<<<<<<<<
>             (inhibit-read-only t))
>         (erase-buffer)
>         (insert data)
>         (mailcap-view-mime "application/pdf")))
>     (goto-char (point-min)))
>
> We should use 'raw-text-unix here, since the buffer contents is a
> stream of raw bytes.
>