question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Reload PDF without losing scroll position?

See original GitHub issue

Question 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:closed
  • Created 4 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
allefeldcommented, Nov 26, 2019

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.

2reactions
jrkoenigcommented, Jul 16, 2020

I found another solution by overriding the fingerprint inside the PDFDocumentProxy:

// Binary pdf contents stored in data
let doc = await pdfjsLib.getDocument({data}).promise;
// Override the fingerprint after parsing but before passing to the view layer.
// This means that the view state (page, scroll offset, etc.) is preserved.
doc._pdfInfo.fingerprint = 'constantFingerprint';
PDFViewerApplication.load(doc);

Note we have to assign to doc._pdfInfo.fingerprint because doc.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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found