使用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文件?