0
votes

I am trying to read pdf file content using Django framework. what I have tried till now is

def upload(request):
    
    try:
        if request.method == 'POST':
            if request.FILES['document'].name.split('.')[-1] == "pdf":
                input = request.FILES['document'].read().decode()

                print(input)

            
            return HttpResponse('upload Products')

    except Exception:
        traceback.print_exc()  

input = request.FILES['document'].read().decode() // This method gives error like 'utf-8' codec can't decode byte 0x9c in position 72: invalid start byte

input = request.FILES['document'].read() I am not able to decode the Output

b'%PDF-1.4\n%\xc3\xa4\xc3\xbc\xc3\xb6\xc3\x9f\n2 0 obj\n<</Length 3 0 R/Filter/FlateDecode>>\nstream\nx\x9c\xdd\x1b\xcb\x8e\xe3\xb8\xf1\xde_\xe1\xf3\x02\xee\x88ER\x94\x80\x81\x01\xdbm\x07\xd8\xdbf\x1a\xc8!\xc8\xc9\xc9&Xx\x03d/\xfb\xfb
xa9'Yz\xd8\xed\xe9\xde\x1c\xa6\xd1\x18O\x95$\x92\xf5\xae\x12Y\xea\x9e\xc3\xe6\xf7\xa7\xffn\xbaM\x87P\x1e\xf33l\x86\x14\x9e\x87\xcdo\xff|\xfa\xeb\x0f\x9b\xff<\x85\r\xfd\xfd\xf6\xaf\xa7\x0eo\xc0\xe6\xd7'{\x086W\x1d@C\xaf:\x05\xfd/\xf7
xfe\xfd\xf4\xf3\x0f\x93A\xd1\r\x8a\xf7\x0619\xf8\x87\x8b\x1e^\x9fr\xc6'\x06\xc0\x9f\xd7\x7fl\xfetFj\x86\xcd\xeb\xcf_\xba\xb0{\xfd\xe5\xe9\xf4\xfa\xf4\xd3\xecy\x08\xf0\x9c\xee\x0f\xe8\x9e\xbb\xae\x0c\t\xd7\x8e\xb9\x1fz\xfc?\r\x103M\x00 cA\xe6\x87\xd8\xe3\x1c\xbf>\x11q\x02_\x99P\x12MQ\x16\xd2\x1c\xd3\xe7\xfc\x0c\xd7'\x91\xc2\x8d\xf5\xfe\xf2g\xa2\xfd\xb9\xc4<l~G\x06~DI\xff

Do I have to do anything with content type which is application/pdf

How can we get full path of file in Django ex: C:\Users\Testing\Zee\ZDemo.pdf

1
Try decode("utf-8") - Gnanavel
Already tried Error [ input = request.FILES['document'].read().decode("utf-8") ] ........'utf-8' codec can't decode byte 0x9c' - Mohammed zuhair

1 Answers

0
votes

You'll need to use a different reader. Save the content that you got uploaded into a folder, and then follow this tutorial:

https://www.geeksforgeeks.org/working-with-pdf-files-in-python/