Pythondex
Python Conversion Programs

Convert PDF To Base64 In Python

Last updated January 31, 2024 by Jarvis Silva

In this article I will show you how to convert pdf file to base64 using python, so inorder to convert a pdf file to base64 we will use the python library Base64 it allows us to encode and decode base64 code.

Python Code To Convert PDF To Base64


import base64

pdf = open('sample.pdf', 'rb')
bs64 = base64.b64encode(pdf.read())

pdf.close()

print(bs64)

Above is the python code to convert pdf to base64, we encode the pdf file using the base64 encode method which will give us the base64 string of the pdf.

After running the program you will see that it printed the base64 version of the pdf file, now if you want to convert the pdf string back to pdf format then you can refer this tutorial: Convert base64 to pdf in python.

Here are some more python programs you will find helpful:

I hope you found what you were looking for from this python tutorial, and if you want more python tutorials like this, do join our Telegram channel to get updated.

Thanks for reading, have a nice day 🙂