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