Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Regression] Commit 302f09 breaks including base64-encoded svg images as data urls #1641

Closed
seifferth opened this issue May 10, 2022 · 2 comments
Labels
bug Existing features not working as expected
Milestone

Comments

@seifferth
Copy link

Summary

When including an svg image via a data url, any text inside the svg image is no longer rendered. Bisecting showed that this is related to
Commit 302f09d. Reverting that commit fixes the issue described here, but may cause other problems to resurface.

I include a more detailed description of this bug below, as well as detailed instructions on how to reproduce it. According to the commit
message, commit 302f09d changed some behaviour from using relative paths in svg images to using absolute paths. Apparently, those absolute paths do not work with svg images included via data urls. A possible solution may be to use absolute paths for external images, but return to using relative paths for images included via data urls.

Problem description

I have a simple svg plot generated by matplotlib that I want to include in an html file and render to pdf using weasyprint. For convenience, I want to include the svg image via a data url in order to only rely on a single, standalone html file. This worked well in weasyprint prior to commit 302f09d, but is broken in more recent versions. The offending commit, which I identified using git bisect, was the following one:

302f09d54a6ea30a9b56f8bacc22e236bde997c6 is the first bad commit
commit 302f09d54a6ea30a9b56f8bacc22e236bde997c6
Author: Guillaume Ayoub <[email protected]>
Date:   Mon Jan 3 14:14:36 2022 +0100

    Always use absolute paths to get hrefs in SVG

    Also be more fault-tolerant when trying to render use tags.

    Fix #1531.

 weasyprint/svg/__init__.py | 8 ++++++--
 weasyprint/svg/defs.py     | 8 ++++----
 weasyprint/svg/images.py   | 4 +---
 3 files changed, 11 insertions(+), 9 deletions(-)

Reproducing the bug

Inline images in html files are rather unwieldy and somewhat unpleasant to the human eye. I include a small script below that creates such an image using python3 and matplotlib. Running this script produces an html file on stdout which can be used to reproduce the bug.

#!/usr/bin/env python3

from matplotlib import pyplot as plt
from io import StringIO
from base64 import b64encode

html_frame = """
<html>
  <head>
    <title>Minimal example showing broken svg images</title>
  </head>
  <body>
    <img src="data:image/svg+xml;base64,{}" />
  </body>
</html>
""".strip()

if __name__ == "__main__":
    x, y = list(range(20)), list(range(20))
    plt.plot(x, y)
    svg = StringIO()
    plt.savefig(svg, format='svg')
    img = str(b64encode(svg.getvalue().encode('utf-8')), 'ascii')
    print(html_frame.format(img))
@liZe liZe added the bug Existing features not working as expected label May 10, 2022
@liZe liZe added this to the 55.0 milestone May 10, 2022
@liZe liZe closed this as completed in 50ad4d1 May 10, 2022
@liZe
Copy link
Member

liZe commented May 10, 2022

Thanks a lot for this perfect bug report. The bug is fixed (and tested), feedback is welcome!

@seifferth
Copy link
Author

seifferth commented May 10, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Existing features not working as expected
Projects
None yet
Development

No branches or pull requests

2 participants