Skip to content

Instantly share code, notes, and snippets.

@jacobian
Forked from simonw/gist:127850
Created June 11, 2009 15:59
Show Gist options
  • Save jacobian/128012 to your computer and use it in GitHub Desktop.
Save jacobian/128012 to your computer and use it in GitHub Desktop.
# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment