我想在我的 Streamlit 应用程序中单击按钮下载 pdf 文件。 我正在使用
st.download_button
,像这样:
st.download_button(
label="Download",
key="download_button",
on_click=None, # You can specify a callback function if needed
file_name="MyDocument.pdf",
data="MyDocumentSource.pdf",
help="Click to download.",
)
pdf文件与python脚本位于同一目录中。我可以这样打开pdf文件。然而,下载后(也有效),我无法打开 pdf 文件,并且收到 - “由于数据类型不受支持,无法打开文件”。
我做错了什么?
解决方案是首先阅读 pdf,如下所示:
with open("MyDocumentSource.pdf", "rb") as pdf_file:
document = pdf_file.read()
然后需要将
data
参数指定为读入的数据。这里document
而不是路径。
data=document,