import tempfile import streamlit as st import os def create_pdf_download_button(pdf_path): # Check if the PDF file exists if not os.path.exists(pdf_path): st.error(f"The PDF file '{os.path.basename(pdf_path)}' was not found.") return # Create a download link st.markdown( f""" Download PDF """, unsafe_allow_html=True, ) def base64_encode_file(file_path): # Read the file contents with open(file_path, "rb") as file: encoded_data = base64.b64encode(file.read()).decode() return encoded_data def main(): if "tmp_dir" not in st.session_state: st.session_state.tmp_dir = tempfile.TemporaryDirectory() if "temp_pdf_path" not in st.session_state: st.session_state.temp_pdf_path = None uploaded_file = st.file_uploader("Choose a PDF file", type="pdf") if uploaded_file: pdf_file = uploaded_file temp_pdf_path = os.path.join(st.session_state.tmp_dir, "pdf_file") with open(temp_pdf_path, "wb") as f: f.write(pdf_file.getvalue()) st.session_state.temp_pdf_path = temp_pdf_path uploaded_file = None create_pdf_download_button(st.session_state.temp_pdf_path) if __name__ == "__main__": main()