Skip to content

Instantly share code, notes, and snippets.

@juanboterotech
Created May 29, 2025 05:47
Show Gist options
  • Save juanboterotech/684d0c05c73bd8be1d0268cd21ec065f to your computer and use it in GitHub Desktop.
Save juanboterotech/684d0c05c73bd8be1d0268cd21ec065f to your computer and use it in GitHub Desktop.
Convert PDF file to Base64, better way for send files through an API or saving to a DB
'''
1) Declare the path of the file (Absolute path or relative)
2) Open using 'with' statement
3) Create the base64 bytes then "translate" to ascii format, save it to encode_string var
4) Print the encoded string in base64
'''
file_path = './somefile.pdf'
with open(file_path, 'rb') as file:
encoded_string = base64.b64encode(file.read()).decode('ascii')
print(f"File in Base64: {encoded_string}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment