lfoppiano / streamlit-pdf-viewer

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

Add possibility to open PDF at a specific page #47

Open solita-tsjoberg opened 2 months ago

solita-tsjoberg commented 2 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 2 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 2 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 2 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 1 month ago

Neither, we are using the default unwrap rendering.