lfoppiano / streamlit-pdf-viewer

Streamlit PDF viewer
https://structure-vision.streamlit.app/
Apache License 2.0
112 stars 9 forks source link

Add possibility to open PDF at a specific page #47

Closed solita-tsjoberg closed 2 months ago

solita-tsjoberg commented 7 months ago

As stated in the title, I would love the feature to be able to open the full PDFs at a specific page.

In my usecase we settled on filtering out the specific page we wanted to open at, but opening at the right page would be much smoother.

lfoppiano commented 7 months ago

Thanks! Opening the PDF at a specific page would come a very handy feature. However, my experience with streamlit is not deep and moving to a specific page means scrolling down the PDF to a certain place. I'm currently not sure how this could or should be implemented, but I'm happy to follow suggestions.

solita-tsjoberg commented 7 months ago

Have not delved too deep into your code, but before swapping to your code we used something like this:

import base64
import streamlit as st
import fitz

if __name__ == "__main__":
    file = fitz.open(file, filetype="pdf")
    file = file.write()
    base64_pdf = base64.b64encode(file).decode("utf-8")
    st.markdown(
        f'<iframe src="data:application/pdf;base64,{base64_pdf}#page={page_number}" type="application/pdf"></iframe>',
    )

        )

Where we used the #page=page_number that seems to be part of some standard pdf string. This would not require any scrolling if you use this kind of string somewhere!

lfoppiano commented 7 months ago

Thanks!

@solita-tsjoberg are you using the rendering=legacy_iframe or legacy_embed?

We could easily add this parameter to it, but this method works only on a small subset of browsers when used by a separate component. In fact, I was thinking to remove it completely at some point, but it seems it's still used more than I though. 🤔

solita-tsjoberg commented 7 months ago

Neither, we are using the default unwrap rendering.

lfoppiano commented 3 months ago

@solita-tsjoberg the version 0.0.18-dev0 should have what you need using the unwrap rendering.

Please see here, and here for more details.