-
-
Save jacobian/128012 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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