-
Notifications
You must be signed in to change notification settings - Fork 10.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reload PDF without losing scroll position? #11359
Comments
I realized that
Unfortunately, it doesn't work, after the reload the scroll position returns to the top. |
That's already the default behaviour, unless you've manually changed preferences/options, when you simply reload the viewer demo at https://mozilla.github.io/pdf.js/web/viewer.html
Assuming that you're actually re-opening the exact same file, then I cannot imagine why directly calling
Given that the default viewer will only render visible pages, in order to save resources, there's generally no point where all pages are rendered (except obviously for one-page documents). |
Right. The difference in my case is that I have the
No, it's not the exact same file, otherwise the reload wouldn't change anything. The context is that slightly modified versions of the PDF are generated, based on a source document (Markdown processed by Pandoc). Because the PDF is different, reloading makes sense, and because the changes are almost always very small, keeping the scroll position makes sense. |
OK, makes sense. Would the |
The functionality in question relies on the document
In the general case, the "almost always very small" part cannot really be assumed to hold.
Generally, yes. |
Closing since this is answered. I'm also linking #6847 here which seems relevant. |
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 |
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 This is similar to the solution in #11496, but doesn't require patching |
When using frameworks like ng-pdfjs-viewer, this doens't work as pdf.js is somewhat hidden. Our hack solution that works in combination with ng-pdfjs-viewer is:
|
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?
The text was updated successfully, but these errors were encountered: