Search code examples
pythonpdfstreamlit

Streamlit use a document uploaded from local disk


I have a piece of code that might work somehow in order to display a pdf file that has been uploaded on a browse from local disk:

uploaded_files = st.file_uploader("Choose a CSV file", accept_multiple_files=True)

for uploaded_file in uploaded_files:
    bytes_data = uploaded_file.read()
    st.write("filename:", uploaded_file.name)
    st.write(bytes_data)
    base64_pdf = base64.b64encode(bytes_data.read()).decode('utf-8')
    pdf_display = F'<iframe src="data:application/pdf;base64,{base64_pdf}" width="700" height="1000" type="application/pdf"></iframe>'   
    st.markdown(pdf_display, unsafe_allow_html=True)

But it does not display it. Would you be so kind as to find a solution ?


Solution

  • You can try this :

    uploaded_files = st.file_uploader("Choose a CSV file", accept_multiple_files=True)
    
    for uploaded_file in uploaded_files:
        with open(uploaded_file.name, "rb") as f:
            base64_pdf = base64.b64encode(f.read()).decode("utf-8")
            pdf_display = F'<iframe src="data:application/pdf;base64,\
            {base64_pdf}" width="700" height="200" type="application/pdf"></iframe>'
            st.markdown(pdf_display, unsafe_allow_html=True)
    

    Output :

    enter image description here