I am trying to create a download option for a pdf file for the end-user. The goal is to enable the user to download the PDF file that is located in the same server where the app is hosted.
I am able to download the file, but the file is not opening after download. The PDFs size varies between 6-12 pages here.
I have tweaked the code a bit from this post. Please help.
import streamlit as st
from fpdf import FPDF
import base64
def create_download_link(val, filename):
b64 = base64.b64encode(val)
hreflink = f'<a href="data:application/octet-stream;base64,{b64.decode()}" download="{filename}.pdf">Download file</a>'
return hreflink
def main():
menu = ["Home", "PDF", "About"]
choice = st.sidebar.selectbox("Menu", menu)
if choice == "Home":
pass
if choice == "PDF":
with open("dummy2.pdf", "rb") as pdf_file:
PDFbyteText = base64.b64encode(pdf_file.read())
export_as_pdf = st.button("Export Report")
st.text(type(PDFbyteText))
if export_as_pdf:
html = create_download_link(PDFbyteText, "test")
st.markdown(html, unsafe_allow_html=True)
if choice == "About":
pass
if __name__ == '__main__':
main()
Upon opening the downloaded PDF, it throws the below error:
import streamlit as st
with open("dummy.pdf", "rb") as pdf_file:
PDFbyte = pdf_file.read()
st.download_button(label="Export_Report",
data=PDFbyte,
file_name="test.pdf",
mime='application/octet-stream')
This is by far the best solution to downloading a pdf from Streamlit!
Solutions within this discussion are also great but I’ve found the above to be most reliable, straightforward and “streamlit native”. I was able to use this hosted on Heroku to download pdfs to the client.
Thank you @cs.sabya!
Hi,
I am trying to create a PDF template that will include a name given by the user. I want to download the PDF. However, I am finding some problems. This error is displayed when I try to run the app: RuntimeError: Invalid binary data format: <class 'fpdf.fpdf.FPDF'>
This is my code:
import streamlit as st
from fpdf import FPDF
st.header('PDF generator - test')
button1 = st.button('PDF')
if button1:
name = st.text_input('Name', value='')
pdf = FPDF('P', 'mm', 'A4')
pdf.add_page()
pdf.set_font(family='Times', size=16)
pdf.cell(40, 50, txt=name)
st.download_button('Download PDF',
data=pdf,
file_name='pdf_test.pdf'
)
Any suggestion to solve this problem?
Thanks in advance,
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.