Reload PDF without losing scroll position?
See original GitHub issueQuestion 1: Is it possible to make PDFViewerApplication
to reload the PDF without losing its scroll position?
I was able to trigger a reload by using PDFViewerApplication.open()
, but the scroll position returns to the top.
If there is no such function, I could save the scrollTop
property of #viewerContainer
and then set it again after the PDF has been reloaded, but for that I would need to know when the PDF has been rendered.
Question 2: Is there a render-finished callback that I can register?
Issue Analytics
- State:
- Created 4 years ago
- Comments:9 (1 by maintainers)
Top Results From Across the Web
How to save "scroll state" of PDF file in Chrome? - Super User
(Try opening a PDF file, closing the tab, and then re-opening it with Ctrl Shift T . Or restart the browser after selecting...
Read more >Refresh Page and Keep Scroll Position - javascript
The forcedReload flag changes how some browsers handle the user's scroll position. Usually reload() restores the scroll position afterward, but forced mode ...
Read more >MacOSX PDF viewer: Automatic reload on file modification - TeX
To turn on the feature, go to Preferences in the main Skim menu, then find the Sync tab and from there select Check...
Read more >PdfViewer how to avoid flicker after LoadDocument and reset ...
in addition, resetting the scroll position seems to work only when en entire page is displayed but not when the user zooms into...
Read more >When i scroll down a certain webpage, browser reloads the ...
Adobe PDF Plug-In For Firefox and Netscape 15.9.20069 Adobe PDF ... only change the scroll bar position and size and not move the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Alright, I finally figured it out, and I describe the solution here in case some else needs this.
The default reload behavior of the pdf.js viewer is indeed to preserve the scroll position (and some other GUI states) if the file is exactly the same. However, it doesn’t check the complete file contents (via a hash or fingerprint) but relies on the document ID that is typically (but not necessarily) embedded in a PDF file (see 14.4 in the specification).
In my case, the (contentwise slightly different) PDFs are generated by pdflatex / xelatex / lualatex, and apparently they generate the PDF ID based on the current time and the pathname of the document. Therefore, even if I don’t change anything in the LaTeX document, just process it again to generate a PDF, this PDF has a different ID, and the pdf.js viewer does not treat it as the same document (preserving the scroll position), but as a new one (starting with the scroll position at the top).
Fortunately, there is a way around this. If the environment variable
SOURCE_DATE_EPOCH
is set to a Unix time stamp (number of seconds since 1 Jan 1970 00:00 UTC), then this time will be used for the PDF ID instead of the current time. Since the pathname of the document doesn’t change either, this ensures an identical ID across regenerations of the PDF, and therefore the viewer shows its scroll-preserving behavior on reload.I found another solution by overriding the fingerprint inside the PDFDocumentProxy:
Note we have to assign to
doc._pdfInfo.fingerprint
becausedoc.fingerprint
is a getter. This doesn’t appear to have ill effects despite accessing the presumably private_pdfInfo
.This is similar to the solution in #11496, but doesn’t require patching
pdf.worker.js
. You could get fancier here and use a fingerprint based on the e.g. basename or path of the file.