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:
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.
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 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.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.