Are there different types of PDF file?
I’ve created a simple Streamlit PDF reader app, using the file_uploader. This works some of the time & displays the PDF. I’m using pdf display code I found in the forum to do this. Slightly modified. The code I’m using is:
import streamlit as st
import base64
image = st.sidebar.file_uploader("Please browse for a pdf file")
st.sidebar.write("Only one file at a time!")
if image is not None:
fn = image.name
if fn[-4:] ==".pdf" or fn[-4:] == ".PDF" :
with open(fn,"rb") as f:
base64_pdf = base64.b64encode(f.read()).decode('utf-8')
pdf_display = f'<embed src=”data:application/pdf;base64,{base64_pdf}” width=”800″ height=”1000″ type=”application/pdf”></embed>'
# pdf_display = f'<iframe src="data:application/pdf;base64,{base64_pdf}" width="800" height="1000" type="application/pdf"></iframe>'
st.markdown(pdf_display, unsafe_allow_html=True)
else:
"Invalid File type (must be .pdf) "
# ***************************** Function SHOW_PDF to read PDFs
# def show_pdf(file_path):
# with open(file_path,"rb") as f:
# base64_pdf = base64.b64encode(f.read()).decode('utf-8')
# pdf_display = f'<iframe src="data:application/pdf;base64,{base64_pdf}" width="800" height="800" type="application/pdf"></iframe>'
# st.markdown(pdf_display, unsafe_allow_html=True)
# *****************************
END OF CODE
With some files I get a File Not Found error & no display. This is the error message in Terminal:
-
*
-
2023-01-13 16:51:08.507 Uncaught app exception*
-
Traceback (most recent call last):*
-
File "/Users/timkendal/Desktop/Python Projects/Streamlit_testing/lib/python3.11/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 565, in _run_script*
-
exec(code, module.__dict__)*
-
File "/Users/timkendal/Desktop/Python Projects/Streamlit_testing/pdf_reader.py", line 15, in <module>*
-
with open(fn,"rb") as f:*
-
^^^^^^^^^^^^^*
-
FileNotFoundError: [Errno 2] No such file or directory: 'Hidcote Map.pdf'*
It is always the same files that fail to display, and they are actually found, as the name appears under the file browser (in the Sidebar in my case). All the files I’m testing with are in the same folder. The code and the files are all local.
You will see that there are 2 lines in the code which are identical except that one uses ‘embed’ and the other ‘iframe’. I found a post that said one of these worked where the other did not. Neither work for me.
The salmon coloured error box in the Browser is this:
FileNotFoundError: [Errno 2] No such file or directory: 'Worcs Beacon Path.pdf'
Traceback:
```
File "/Users/timkendal/Desktop/Python Projects/Streamlit_testing/lib/python3.11/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 565, in _run_script
exec(code, module.__dict__)File "/Users/timkendal/Desktop/Python Projects/Streamlit_testing/pdf_reader.py", line 15, in <module>
with open(fn,"rb") as f:
^^^^^^^^^^^^^
```
In summary, why will the code above read and display only some PDF files?
Debug info
- Streamlit version: 1.17.0 (updated to this today. Previous version had the same problem.)
- Python version: 3.11.1
- Using Conda? PipEnv? PyEnv? Pex? NO
- OS version: MacOS Catalina 10.15.7
- Browser version: Chrome Version 108.0.5359.124
Requirements file none
I hope someone can help here! It is frustrating!
Tim Kendal