Hi my lovely community,
I have a small local python app and it works well with using pikepdf to remove user password of pdf file. Now I want to move it to web but stucked for several days.
The problem is here:
from pikepdf import Pdf
uploaded_pdf_file=st.file_uploader('Upload Your PDF File',type=['pdf'])
if uploaded_pdf_file is not None:
pdffile = Pdf.open(uploaded_pdf_file, allow_overwriting_input = True)
pdf_result=pdffile.save('unlocked.pdf')
It reports ValueError: “allow_overwriting_input=True” requires “open” first argument to be a file path
Is it possible that use pikepdf to open a uploaded pdf file? How?
I did lots of search. It seems like there were very few articles about streamlit and pikepdf (Maybe as PDF is evil).
import streamlit as st
from pikepdf import Pdf
uploaded_pdf_file = st.file_uploader('Upload Your PDF File', type=['pdf'])
if uploaded_pdf_file is not None:
pdffile = Pdf.open(uploaded_pdf_file)
# do some processing here with the pdf file
pdffile.save('unlocked.pdf')
with open("unlocked.pdf", "rb") as pdf_file:
PDFbyte = pdf_file.read()
st.download_button(label="Download unlocked.pdf",
data=PDFbyte,
file_name="unlocked.pdf",
mime='application/octet-stream')
Here an even better quick example without writing temporarily to disk:
import io
import streamlit as st
from pikepdf import Pdf
filestream = io.BytesIO()
uploaded_pdf_file = st.file_uploader('Upload Your PDF File', type=['pdf'])
if uploaded_pdf_file:
if len(uploaded_pdf_file.getvalue()) > 0:
st.text(f'uploaded_pdf_file: {len(uploaded_pdf_file.getvalue())} bytes')
pdffile = Pdf.open(uploaded_pdf_file)
# do some processing here with the pdf file
pdffile.save(filestream)
if filestream:
if len(filestream.getvalue()) > 0:
st.download_button(label="Download unlocked.pdf",
data=filestream.getvalue(),
file_name="unlocked.pdf",
mime='application/octet-stream')
st.text(f'filestream: {len(filestream.getvalue())} bytes')
Thank you so much! Your method works! It really saved my days!
A tips for later reader if any:
My streamlit version is 0.80.0. To use st.download_button, you have to upgrade it to 1.7.0.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.