IvanStudent's picture
Upload 12 files
103ce4f verified
raw
history blame contribute delete
754 Bytes
from base64 import b64encode
from fpdf import FPDF
import streamlit as st
st.title("Demo of fpdf2 usage with streamlit")
@st.cache_data
def gen_pdf():
pdf = FPDF()
pdf.add_page()
pdf.set_font("Helvetica", size=24)
pdf.cell(w=40,h=10,border=1,txt="hello world")
return pdf.output(dest='S').encode('latin1')
# Embed PDF to display it:
base64_pdf = b64encode(gen_pdf()).decode("utf-8")
pdf_display = f'<embed src="data:application/pdf;base64,{base64_pdf}" width="700" height="400" type="application/pdf">'
st.markdown(pdf_display, unsafe_allow_html=True)
# Add a download button:
st.download_button(
label="Download PDF",
data=gen_pdf(),
file_name="file_name.pdf",
mime="application/pdf",
)