Looking for a Solution to Embed PDF Files Larger Than 1.5MB Without Splitting

I’ve come across a post titled “Embed pdf files that are larger than 2MB” (Embed pdf files that are larger than 2MB) that suggested splitting larger PDF files to display them in a Streamlit app. While this seems to be a valid workaround, I’m seeking an alternative solution that allows me to embed and display the entire PDF file without dividing it into smaller parts.

Below is the code snippet I’m currently using in my application:

base64_pdf = base64.b64encode(blob_data).decode("utf-8")
pdf_display = f'<iframe src="data:application/pdf;base64,{base64_pdf}#page={page_no}" width="100%" height="800" style="border:none;"></iframe>'
st.markdown(pdf_display, unsafe_allow_html=True)
  • streamlit==1.32.2
  • Python 3.12.2
  • Ubuntu 22.04
  • Chrome 123.0.6312.86

This code works perfectly for PDFs under 1.5MB, but fails to render when the PDF exceeds this size. I understand that splitting the PDF could be a workaround, but I’m interested in finding a way to keep the PDF whole due to the nature of the content and the user experience I aim to provide.

Is there a way to increase the capacity for PDF file sizes or a different method of embedding that could circumvent this issue? Any advice or suggestions that do not involve splitting the PDF would be greatly appreciated.

Thank you for your time and assistance.

Hi @Masa3,

Thanks for sharing this question!

One thing you can try is by saving your PDF files in an external storage then linking it for display which will offload the heavy lifting to the server hosting the PDF and you might not be limited by the size constraints.

Hi @tonykip ,
Thank you for your initial response to my query.

I attempted to implement this solution with the following code snippet:

import streamlit as st    
    
# PDF file URL    
pdf_url = "https://www.example.com/path/to/your/file.pdf"    
    
# Creating an HTML link    
st.markdown(f"[Open PDF file]({pdf_url})", unsafe_allow_html=True)    

Unfortunately, I encountered an issue where clicking on the generated link results in a browser error stating that the site can’t be reached or that the response time was too long. I have verified that the PDF file is accessible directly via the URL in the browser, so the link itself seems to be correct.

Could you please provide any insights on why this might be happening or suggest alternative methods for linking to a PDF in an external storage within a Streamlit app?

I would appreciate any help or guidance you can offer.

Thank you!

Hi @Masa3

Have you tried using st.link_button()?

Here’s the Docs page:

Hope this helps!

Hi @dataprofessor

Thank you for the suggestion to use st.link_button().

I am aware that st.link_button() could be a good solution for linking to documents hosted on a web server. However, I have concerns that it may not work as expected with local files. To clarify, does st.link_button() support linking to a local file that is not hosted on a server, or is it intended only for URLs that point to resources on the web?

I appreciate your help and look forward to your guidance.

Here’s your example code using st.link_button and a dummy pdf url from w3.org:

import streamlit as st    
    
# PDF file URL    
pdf_url = "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf"    
    
# Creating an HTML link    
st.link_button("Open PDF file", pdf_url) 

When you click on the button on the app it opens the site where the pdf is hosted and displays it.

The link button supports only URLs.

You might also be interested in this solution for local PDF files: