2016-07-10 26 views
1

使用Django rest_framework收到了一個pdf文件。我嘗試了幾種方法來解析傳入文件中的內容。從Django解析pdf InMemoryUploadedFile

curl -vX POST http://127.0.0.1:8000/documents/ -d @10_CSS\ (1).pdf --header "Content-Type: application/pdf" --header "Content-Disposition: attachment; filename=10_css.pdf"

def create(self, request):   
    file_ = request.FILES['file'] 

    parser = PDFParser(file_)  
    document = PDFDocument(parser) 

PDFSyntaxError: No /Root object! - Is this really a PDF?

def create(self, request):   
    file_ = request.FILES['file'] 

    parser = PDFParser(file_.read())  
    document = PDFDocument(parser) 

AttributeError: 'str' object has no attribute 'seek'

def create(self, request):   
    utf8_file = codecs.EncodedFile(request.FILES['file'], "utf-8") 
    with open('/tmp/whatever.pdf', 'wb+') as destination:   
     for chunk in request.FILES['file'].chunks():    
      destination.write(chunk)        

    file_ = open('/tmp/whatever.pdf', 'rb')      
    parser = PDFParser(file_)          
    document = PDFDocument(parser)         

PDFSyntaxError: No /Root object! - Is this really a PDF?

我試了幾個PDF文件具有相同的結果。在我將它發送到我的應用程序之前,當我嘗試解析一個pdf文件時,它解析得很好。 **如何從InMemoryUploadedFile解析pdf文件?

回答

0

我猜你正在使用:

parser_classes = (FileUploadParser,) 

在你的頭(--header「內容處置:附件;文件名= 10_css.pdf」),這種情況下,包含在文件中。

我建議你使用MultiPartParser並且不要發送Content-Disposition頭。

More informations