Skip to content

Commit f12defe

Browse files
committed
add: remove password from PDF before displaying again
1 parent bc854be commit f12defe

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

basxbread/contrib/customforms/models.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,9 @@ class Meta:
6464
verbose_name_plural = _("Custom form fields")
6565

6666

67-
def pdf_fields(pdffile, password=None):
67+
def pdf_fields(pdffile):
6868
fields = {}
6969
pdf = fitz.Document(stream=pdffile)
70-
if password is not None:
71-
pdf.authenticate(password)
7270
for page in pdf:
7371
widget = page.first_widget
7472
while widget:

basxbread/contrib/customforms/views.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import base64
22

3+
import fitz
34
import htmlgenerator as hg
45
from django import forms
56
from django.shortcuts import get_object_or_404
@@ -88,6 +89,14 @@ def formview(request, pk):
8889
return formview_processing(request, form=form)
8990

9091

92+
def remove_pdf_password(pdf_content, password):
93+
pdf = fitz.Document(stream=pdf_content)
94+
pdf.authenticate(password)
95+
ret = pdf.tobytes()
96+
pdf.close()
97+
return ret
98+
99+
91100
@utils.aslayout
92101
def pdfimportview(request, pk):
93102
class UploadForm(forms.Form):
@@ -101,9 +110,11 @@ class UploadForm(forms.Form):
101110
if uploadform.is_valid():
102111
if uploadform.cleaned_data.get("importfile"):
103112
pdfcontent = uploadform.cleaned_data["importfile"].read()
104-
pdffields = models.pdf_fields(
105-
pdfcontent, uploadform.cleaned_data["password"] or None
106-
)
113+
if uploadform.cleaned_data["password"]:
114+
pdfcontent = remove_pdf_password(
115+
pdfcontent, uploadform.cleaned_data["password"]
116+
)
117+
pdffields = models.pdf_fields(pdfcontent)
107118
initial = {}
108119
for pdf_formfield in pdfimporter.fields.exclude(customform_field=None):
109120
value = pdffields.get(pdf_formfield.pdf_field_name, "")

0 commit comments

Comments
 (0)