Go to specific page in PDF after loading it

I have loaded a 100 page PDF to display in the streamlit UI with some highlighted text. When the user clicks a button, I would like the app to go to the page with the highlighted text instead of having the user scroll through to find it. Currently, the way I have it working is that I reload the PDF each time and set it to the correct page. Is there a way to do it without reloading the PDF each time which causes slowness?

    with open("test.pdf","rb") as f:
        base64_pdf = base64.b64encode(f.read()).decode('utf-8')
    pdf_display = f'<iframe src="data:application/pdf;base64,{base64_pdf}#page={new_page_num}" width="800" height="800" type="application/pdf"></iframe>'
    st.markdown(pdf_display, unsafe_allow_html=True)

Hi @robo-sq,

Thanks for sharing your question! Sorry for the delayed response on this.

I’d recommend creating a separate function that loads the PDF, and then using st.cache_data to cache that function. You can also check out our docs on caching overall here.