tomoki1207 / vscode-pdfviewer

Show PDF preview in VSCode.
https://marketplace.visualstudio.com/items/tomoki1207.pdf
Other
295 stars 69 forks source link

Setting "pdf-preview.default.sidebar" ignored with some PDFs generated from Latex #79

Open sjerdmann opened 3 years ago

sjerdmann commented 3 years ago

Hi, I set "pdf-preview.default.sidebar": false, but when opening a PDF generated from a LaTeX file where either package hyperref or bookmark is used, the sidebar is opened anyway. A side effect of this is that PDFs generated from Markdown files with pandoc have the same issue.

To reproduce : This file was generated from a tex file with hyperref on. This one was generated from the same Latex code, without the line \usepackage{hyperref}. With sidebar off by default, the first one opens with the sidebar, the second one doesn't. The original Latex file had the following content:

\documentclass{article}

\usepackage{hyperref} % This line was commented out to generate the second file
\title{foo}
\date{}

\begin{document}
  \maketitle
  bar
\end{document}

and the pdfs were generated from the bash command line with pdflatex $filename.tex.

yxlao commented 3 years ago

Sharing my naive fix for this. I am sure there are better ways to fix this, but this is a temporary solution that works for me on Ubuntu 20.04.

For some reason, if the program sleeps for just 1ms before calling close() or open(), everything works as expected. This is true even for loading large PDF files, which takes more than 1ms to load.

You'll need to edit this file:

# check the actual path since the version number can be different
.vscode/extensions/tomoki1207.pdf-1.1.0/lib/main.js

with the following changes:

diff --git a/lib/main.js b/lib/main.js
index da2b4e3..40ebc72 100644
--- a/lib/main.js
+++ b/lib/main.js
@@ -48,11 +48,16 @@
         PDFViewerApplication.pdfViewer.currentScaleValue = defaults.scale
         PDFViewerApplication.pdfViewer.scrollMode = scrollMode(defaults.scrollMode)
         PDFViewerApplication.pdfViewer.spreadMode = spreadMode(defaults.spreadMode)
-        if (defaults.sidebar) {
-          PDFViewerApplication.pdfSidebar.open()
-        } else {
-          PDFViewerApplication.pdfSidebar.close()
+        var sleep = function (time) {
+          return new Promise((resolve) => setTimeout(resolve, time));
         }
+        sleep(1).then(() => {
+          if (defaults.sidebar) {
+            PDFViewerApplication.pdfSidebar.open()
+          } else {
+            PDFViewerApplication.pdfSidebar.close()
+          }
+        });
         PDFViewerApplication.eventBus.off('documentloaded', optsOnLoad)
       }
       PDFViewerApplication.eventBus.on('documentloaded', optsOnLoad)
psybers commented 3 years ago

This seems to work for the initial load, but if the PDF changes on disk and it reloads then the sidebar pops out again.

I think the issue is once the preferences code is run after the documentloaded event, it unregisters itself. So the next time it (re)loads that code doesn't run and the default preferences come back. Removing the PDFViewerApplication.eventBus.off('documentloaded', optsOnLoad) line seems to get it working, but then of course we are never removing that event... I don't know the PDF.js API at all but I suspect there is another event we can listen to and remove the optsOnLoad handler there.

eduncan911 commented 3 years ago

Bumping this to say I have the same issue.

The "sleep" isn't a resolution. For my workflow, I am developing PDFs using LaTeX where I am reloading the same PDF over and over again, dozens of times on every "save." So, having the view open viewing the PDF will refresh the preview (which is a nice touch).

Due to this sidebar issue, I often finding myself not using this previewer tool and just opening it with xdg-open (which uses evince on linux - which has a setting to not show sidebar by default).

e-kotov commented 1 month ago

Still relevant. Bump.