Reputation: 17
I am getting a base64 encoded pdf file with image, which is sent in json format. But I don't understand how to decode all this back to pdf, to save it for example on my computer.
for example that's what I receive (unfortunately I can't add full json, because of his length):
{'img': 'JVBERi0xLjcKJeLjz9MKNCAwIG9iago.......'}
Upvotes: 0
Views: 1300
Reputation: 975
The below code should convert the Base64 to a PDF. Where input
is your json string
# Import only b64decode function from the base64 module
from base64 import b64decode
json_str = {'img': 'JVBERi0xLjcKJeLjz9MKNCAwIG9iago.......'}
# Define the Base64 string of the PDF file
b64 = json_str['img']
# Decode the Base64 string, making sure that it contains only valid characters
b64bytes = b64decode(b64, validate=True)
# Perform a basic validation to make sure that the result is a valid PDF file
# Be aware! The magic number (file signature) is not 100% reliable solution to validate PDF files
# Moreover, if you get Base64 from an untrusted source, you must sanitize the PDF contents
if b64bytes[0:4] != b'%PDF':
raise ValueError('Missing the PDF file signature')
# Write the PDF contents to a local file
with open('file.pdf', 'wb') as f:
f.write(b64bytes)
Source: https://base64.guru/developers/python/examples/decode-pdf
Upvotes: 2