Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

module base64.decode yields corrupt pdf base64 encoded file

Tags:

python

base64

Hello when i try to decode image like this

"example_label.b64" file is here (http://ge.tt/27odGly/v/0)

i see corrupted pdf, looks like base64 decoding corrupt this file.

Can you help me?

Here code example.

import base64
with open('example_label.b64','r+') as f:
    data = f.read()
    f.close()
# data = data.decode('base64')
#or
data = base64.b64decode(data)
with open('example_label.pdf','w+') as f:
    f.write(data)
    f.close()

Thank you.

like image 539
Darius Avatar asked Nov 20 '25 05:11

Darius


1 Answers

Your write mode should specify binary or else you risk newline conversion depending on your platform. That is

open('example_label.pdf', 'wb')

The file decodes just fine on my system yielding checksums of

$ md5sum example_label.pdf 
bd9a4e16d45fe01bfc77a2af6afe1b8b  example_label.pdf
$ sha1sum example_label.pdf 
a9b77217793ef05f212b0619248480411f6ed4a1  example_label.pdf

and is a US postal service label with dummy addresses in it.

like image 99
msw Avatar answered Nov 21 '25 20:11

msw