minad / consult

:mag: consult.el - Consulting completing-read
GNU General Public License v3.0
1.12k stars 98 forks source link

consult preview: "PDF document is damaged" but no problem viewing it directly from pdf-tools #952

Closed maxecharel closed 2 months ago

maxecharel commented 2 months ago

Hi, For some PDF files generated with latexmk-pdflatex from AUCTeX, consult preview called by/from consult-buffer gives me this type of error:

epdfinfo: Error opening /tmp/pdf-tools-eU4wW8/consult-partial-preview-<path-to-pdf>-2--9gTN9h/consult-partial-preview-<path-to-pdf>-2--NxWqR3:PDF document is damaged

(NB: epdfinfo is the server on which pdf-tools relies)

However, I have no problem reading them directly from the pdf-view-mode provided by pdf-tools. Can also be read by Okular. Sorry for the limited amount of information provided, I have no clue regarding the possible cause of this error that seems consult-specific (epdfinfo enters the equation, but since I have no problem opening these pdfs directly from pdf-tools, I guess we can discard it as the only or even main source of the problem?)

As always, thanks for the great work.

Best,

M

Emacs 29.2 consult 20240223.1314 (MELPA) pdf-tools 20230611.239 (MELPA)

minad commented 2 months ago

Hi!

Thanks for the report. I haven't seen this problem so far, but I think I know the reason for the problem. Consult tries to preview the files partially, but then fails to detect the file as binary, because the file doesn't contain zeros in the first part of the file.

  1. Try adjusting consult-preview-partial-size and consult-preview-partial-chunk. Furthermore you can disable pdf preview via consult-preview-excluded-files. Does this change anything?

  2. Can you send me one of these pdf files such that I can test this myself?

Thanks!

minad commented 2 months ago

I conducted a few more experiments and I have pushed an improvement in https://github.com/minad/consult/commit/9d96faff6d9231d30c3a17df82abedf8c13ee2c8. Please let me know if you still observe issues.

maxecharel commented 2 months ago

Hi @minad , Thank you for the fix! After upgrading consult to 20240224.1017 and without changing the value of consult-preview-partial-size or consult-preview-partial-chunk, I get

Binary file ‘<filename.pdf>’ not previewed

in line with the changes you've committed. As you suggested, I tried to adjust consult-preview-partial-size, and after some experiments

(setq consult-preview-partial-size (* 1.5 1048576))

did the job: the file is previewed :) On the other hand, setting consult-preview-partial-size to the default and instead adjusting consult-preview-partial-chunk does not solve the problem (even with values like (* 100 102400)).

Long story short, your fix allows not to get an error, and the adjustment of consult-preview-partial-size allows to preview the file. Thanks a lot!

Do you think it would be worth documenting this problem and the solution? Even maybe giving a hint to the user, with something like

Binary file ‘<filename.pdf>’ not previewed. Try increasing the value of `consult-preview-partial-size'.

?

BTW, do you still want a copy of the files for which I've encountered the issue?

Best,

M

minad commented 2 months ago

Do you think it would be worth documenting this problem and the solution? Even maybe giving a hint to the user, with something like

The message is already long. I don't want to make it more verbose. It should not be necessary to inform the user every time about this variable. I expect users to look at configuration variables documented in the README. Also the customization interface is very useful for discovery.

BTW, do you still want a copy of the files for which I've encountered the issue?

Not needed. Thanks!