yWorks / svg2pdf.js

A javascript-only SVG to PDF conversion utility that runs in the browser. Brought to you by yWorks - the diagramming experts
MIT License
643 stars 96 forks source link

Accept spaces in data urls in image urls #263

Closed yGuy closed 11 months ago

yGuy commented 11 months ago

Describe the bug When there is a newline in a data url in an image href like in #262 , the data url is not recognized and image processing fails with the creation of an invalid URL.

To Reproduce Steps to reproduce the behavior:

  1. With an SVG like the following where the data-url in an image has new lines in the attribute:

    <svg xmlns:xlink="http://www.w3.org/1999/xlink" width="500" height="500" xmlns="http://www.w3.org/2000/svg"
    >
    <image x="0" y="0" width="100" xlink:href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA9kAAAGQCAYAAAC6fKNnAACAAElEQVR4Xuy9MY4s
    PY7ve3Zx7dfWxUVjFlDALGCAtsYYvBWk1WM89AouxmqvxrxAm+1fr5xxZgG9g7OY
    eKVQKEIi/ySlSCkzQsUfwO+rDCkokiIVUmaeql+L4ziO4ziO4ziO4zhd+EUvOI7j
    OI7jOI7jOI5zDj9kO47jOI7jOI7jOE4n/JDtOI7jOI7jOI7jOJ3wQ7bjOI7jOI7j
    OI7jdMIP2Y7jOI7jOI7jOI7TCT9kO47jOI7jOI7jOE4n/JDtOI7jOI7jOI7jOJ3w
    Q7bjOI7jOI7jOI7jdMIP2Y7jOI7jOI7jOI7TCT9kO47jOI7jOI7jOE4n/JDtOI7j
    OI7jOI7jOJ3wQ7bjOI7jOI7jOI7jdMIP2Y7jOI7jOI7jOI7TCT9kO47jOI7jOI7j
    OE4n/JDtOI7jOI7jOI7jOJ3wQ7bjOI7jOI7jOI7jdMIP2Y7jOI7jOI7jOI7TCT9k
    O47jOI7jOI7jOE4n/JDtOI7jOI7jOI7jOJ3oesj+9euXi4uLi4uLi4uLi4uLi8st
    ZARdtQYj/88/Fpebic+bLh4fFxcXFxcXl58mvv9x+Qnih2yXYeLzpovHx8XFxcXF
    xeWnie9/XH6C+CHbZZj4vOni8XFxcXFxcXH5aeL7H5efIH7IdhkmPm+6eHxcXFxc
    XFxcfpr4/sflJ4gfsl2Gic+bLh4fFxcXFxcXl58mvv9x+Qnih2yXYeLzpovHx8XF
    xeWs/F7+7Z9+Lb/+6XP5D9bmckr+7+fyh+/n0q9//eJtLi4dxfc/Lj9B/JDdRb6W
    fw4Ppl+P5d/3n7H8838K7etDbds07H2lcYJ8LP/2f2n7tUSct/QgZ/6nPnk8wX1s
    U5XHjdyTx6y4L9+gCXOyyTFn9vz+4f9J9xw2/MdfPmI72biEa/lrF5eu0qXO9Hxf
    .... snip ....
    "
      />
    </svg>
  2. Conversion will fail because the data url is not recognized

Expected behavior Browsers are more lenient with respect to data urls. XML parsers will treat new lines as spaces and browsers simply remove these white space from the base64 url before parsing it. Svg2pdf should ideally do the same.

This seems to be in accordance to the following data-url specs and implementations: RFC2045 MDN

Additional context The Regex in the code that handles data urls does not consider this additional whitespace and hence processing continues with the wrong steps. Sanitizing/minifying the SVG by removing the whitespace from the data urls fixes the issue like in #262