I have a PDF as a base64 string and I need to write it to file using Python. I tried this:
import base64
base64String = "data:application/pdf;base64,JVBERi0xLjQKJeHp69MKMSAwIG9iago8PC9Qcm9kdWNlciAoU2tpYS9..."
with open('temp.pdf', 'wb') as theFile:
theFile.write(base64.b64decode(base64String))
But it didn't create a valid PDF file. What am I missing?
How to convert Base64 to file. Paste your string in the “Base64” field. Press the “Decode Base64 to File” button. Click on the filename link to download the file.
Base64 encoding is used to encode binary data, such as a PDF file, into an ASCII string format that is compatible with systems that can only handle text. For example, email attachments and binary uploads in HTML forms are converted and transmitted as Base64 encoded data.
💻 Can I convert PDF to BASE64 on Linux, Mac OS or Android? Yes, you can use free Converter app on any operating system that has a web browser. Our PDF to BASE64 converter works online and does not require any software installation.
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With