Skip to content

Instantly share code, notes, and snippets.

@theawesomecoder61
Last active April 6, 2022 02:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theawesomecoder61/43d0645364ae79474ffa4aade27677fd to your computer and use it in GitHub Desktop.
Save theawesomecoder61/43d0645364ae79474ffa4aade27677fd to your computer and use it in GitHub Desktop.
Converts a text file with Base64 encoded font (like in CSS) to a .bin file to be converted to a font
#!/usr/bin/python
# This script converts a font encoded in Base64 in a text file to a .bin font file.
# You can convert the .bin file using http://onlinefontconverter.com/, which I recommend though I am not affiliated.
# It'd be useful to know what the type (TTF, OTF, WOFF, SVG, etc.) the original font was for the online converter.
# tested with Python 2.7
from base64 import decodestring
b64_file = open("fontbase64.txt", "r") # change the file if you want
b64_data = b64_file.read()
mime = ""
font_type = "N/A"
if b64_data.find(":") > -1: # it found a mime type
mime = b64_data[b64_data.find(":")+1:b64_data.find(";")]
if mime is not "": # get the actual data if a mime type was found
b64_data = b64_data[b64_data.find(",")]
# detect font type
if mime == "application/x-font-otf":
font_type = "OTF"
elif mime == "application/x-font-ttf":
font_type = "TTF"
elif mime == "application/x-font-woff":
font_type = "WOFF"
print "Font is " + font_type
with open("fontbinary.bin", "wb") as fb: # change the file if you want
fb.write(b64_data.decode("base64"))
print "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment