jacobian (owner)

Fork Of

Forks

Revisions

gist: 128012 Download_button fork
public
Public Clone URL: git://gist.github.com/128012.git
Embed All Files: show embed
Python #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 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