Inserting local PDF

I know there isn’t a simple way to do this, but is there a reasonable workaround to insert local PDF in streamlit?

Here’s what I’ve tried:

  1. Use embed in html in markdown:
    Initially I was getting 404 error because I was using relative path in src but I managed to get past that by setting the src to be file:///home/username/path_to_file as opposed to /home/username/path_to_file. Now I am getting error
    Blocked http://xxx from loading file:///yyy Refused to load a local file in a non-local page for security reasons. Stuck there

  2. JS in html markdown:
    To avoid accessing local files via third party scripts from the browser I downloaded js library (PDFObject js) locally. However, now I can’t embed it into the html cause streamlit doesn’t seem to allow script tags in html in markdown.

  3. (not streamlit issue) pdf2image
    Tried pdf2image to convert the pdf to image first and then render it using st.image but all I got is 1x1 blank image. This doesn’t seem to be streamlit issue cause I tried it from terminal using the underlying tools that pdf2image uses and still got the same result. The pdf is only one page but has quite complex content which might be an issue.

Is there any way I can insert local PDF into my streamlit app? It is central part of the app and the main reason why I needed a visualization app in the first place
Thank you in advance!

Hey @sanjass - you probably saw the response to the associated Github issue, but I’m including the link in this thread for anyone else who comes along.

tl;dr: this isn’t supported in Streamlit currently, but it should become possible via an upcoming st.iframe feature!

Seems there is a workaround by first encoding the pdf to base64 and then using embed in html. Many thanks to @Saxamos who found the solution in a related GitHub issue. Reproducing the solution here:

base64_pdf = base64.b64encode(pdf_file.read()).decode('utf-8')
pdf_display = f'<embed src="data:application/pdf;base64,{base64_pdf}" width="700" height="1000" type="application/pdf">' 
st.markdown(pdf_display, unsafe_allow_html=True)
1 Like