Skip to content

Instantly share code, notes, and snippets.

@gvangool
Forked from simonw/gist:127850
Created June 15, 2009 06:11
Show Gist options
  • Save gvangool/129962 to your computer and use it in GitHub Desktop.
Save gvangool/129962 to your computer and use it in GitHub Desktop.
Validate that an uploaded file is a PDF
# Django: validate that an uploaded file is a valid PDF
import pyPdf # from http://pybrary.net/pyPdf/
from pyPdf.utils import PdfReadError
class DocumentForm(forms.ModelForm):
pdf = forms.FileField()
class Meta:
model = Document
def clean_pdf(self):
file = self.cleaned_data['pdf']
try:
pdf = pyPdf.PdfFileReader(file)
except PdfReadError:
raise forms.ValidationError, 'You must upload a valid PDF file'
print pdf.documentInfo
return file
@hcisilio
Copy link

There is a way to know if pdf it's a pdf/A?

@taten14
Copy link

taten14 commented Oct 11, 2020

There is a way to know if pdf it's a pdf/A?

I need the same!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment