Download PDF option

Hi Community,

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:

Hello @cs.sabya

There is an official Download Button, did you try to use it with the pdf_file variable?

Have a nice day,
Fanilo

Thank you @andfanilo. This is working for me.

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')
1 Like

Thanks for sharing back the answer :slight_smile: will definitely help others!

Fanilo