How to convert a PDF from base64 string to a file?

From my understanding base64decode only takes in a base64 string and looks like you have some headers on your string that are not encoded.

I would remove "data:application/pdf;base64,"

check out the doc here: https://docs.python.org/2/library/base64.html

When I've used it in the past, I have only used the encoded string.


Does writing it by using the codecs.decode function work? also as Mark stated, you can try to remove the data:application/pdf;base64, portion of the string as this section of the string is not to be decoded.:

import codecs
base64String = "JVBERi0xLjQKJeHp69MKMSAwIG9iago8PC9Qcm9kdWNlciAoU2tpYS9..."


with open("test.pdf", "wb") as f:
    f.write(codecs.decode(base64string, "base64"))

Extending @Jebby's answer using Base64 (had the same issue as @SmartManoj)

import base64
base64String = "JVBERi0xLjQKJeHp69MKMSAwIG9iago8PC9Qcm9kdWNlciAoU2tpYS9..."


with open("test.pdf", "wb") as f:
    f.write(base64.b64decode(base64string))