Skip to content

Instantly share code, notes, and snippets.

@lxneng
Forked from simonw/gist:127850
Created February 25, 2010 06:16
Show Gist options
  • Save lxneng/314288 to your computer and use it in GitHub Desktop.
Save lxneng/314288 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