btel / svg_utils

Python tools to create and manipulate SVG files
MIT License
315 stars 88 forks source link

Vague error when no height or width tag is in the svg #125

Open oskeith opened 1 year ago

oskeith commented 1 year ago

When exporting an svg from Adobe Illustrator, by default, "responsive" is ticked and no width or height tag is set. In that case, a vague error is thrown. I think it would really help a new user if there was a check and an appropriate message if the svg has no width or height tag.

import svgutils.compose as sc
sc.SVG(os.path.join(file_directory, 'face_border resp.svg'))

File "[main]", line 55, in sc.SVG(os.path.join(file_directory, 'face_border resp.svg')) File "C:\Users\keith\miniconda3\lib\site-packages\svgutils\compose.py", line 122, in init if svg.width.endswith("%"): AttributeError: 'NoneType' object has no attribute 'endswith'

Thanks!

qubodup commented 1 year ago

Same with some SVGs I have to deal with, even after editing manually in Inkscape.

Related issue: https://github.com/astraw/svg_stack/issues/8 which includes sample file and fix

Sample affected file:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Generator: Adobe Illustrator 19.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
<svg
   version="1.1"
   id="Capa_1"
   x="0px"
   y="0px"
   viewBox="0 0 512.107 512.107"
   style="enable-background:new 0 0 512.107 512.107;"
   xml:space="preserve"
   sodipodi:docname="test.svg"
   inkscape:version="1.2.2 (732a01da63, 2022-12-09)"
   xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
   xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
   xmlns="http://www.w3.org/2000/svg"
   xmlns:svg="http://www.w3.org/2000/svg"><defs
   id="defs85" /><sodipodi:namedview
   id="namedview83"
   pagecolor="#505050"
   bordercolor="#eeeeee"
   borderopacity="1"
   inkscape:showpageshadow="0"
   inkscape:pageopacity="0"
   inkscape:pagecheckerboard="0"
   inkscape:deskcolor="#505050"
   showgrid="false"
   inkscape:zoom="0.4608412"
   inkscape:cx="-1332.3462"
   inkscape:cy="451.34853"
   inkscape:current-layer="Capa_1" />
<rect
   style="fill:#efe3cd;stroke-width:11.3386;stroke-linecap:round;stroke-linejoin:round;stop-color:#000000"
   id="rect287"
   width="160.57593"
   height="128.02675"
   x="99.817467"
   y="141.04642" /></svg>

zip of file for easier download maybe: test.zip

Resizing canvas of the svg in Inkscape fixes this by the way.