Last active
January 13, 2025 10:32
-
-
Save pgolding/12da255925695317f419f9728d49e781 to your computer and use it in GitHub Desktop.
Using BeautifulSoup to remove Adobe entity tags (and width/height attributes) from SVG
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
''' | |
When rendering SVG files exported from Adobe Illustrator using Phantom JS, | |
the injected Adobe ENTITY tags can intefere with the render. This removes | |
the tags and also removes width/height attributes from the SVG tag. | |
''' | |
SVG_FILE = 'Some-adobe-illustrator-export.svg' | |
# By default, BeautifulSoup will ignore the <!ENTITY> array when parsing the file | |
# so we don't need to delete the array. It's not entirely safe, but works OK as a hack. | |
def sanitizeAdobeSVG(file): | |
from bs4 import BeautifulSoup | |
svg = BeautifulSoup(file, "xml") | |
svg_tag = svg.find('svg') | |
if 'width' in svg_tag.attrs: | |
del svg_tag['width'] | |
if 'height' in svg_tag.attrs: | |
del svg_tag['height'] | |
return svg | |
# Get the SVG XML | |
f = open(SVG_FILE, 'r') | |
svg_xml = f.read() | |
# Check for the tell-tale Adobe ENTITY tags | |
if '<!ENTITY' in svg_xml and 'ns.adobe.com' in svg_xml: | |
print('Found Adobe-specific data') | |
f = open(SVG_FILE, 'r') | |
sanitized_svg = sanitizeAdobeSVG(f) | |
f.close() | |
svg_out = sanitized_svg.prettify("utf-8") | |
with open('output.svg', 'wb') as file: # new file, but you can overwrite old one | |
file.write(svg_out) | |
else: | |
f.close() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment