Created
May 29, 2025 05:47
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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