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

Fails to render svg elements with text-anchor #2381

Closed
kesara opened this issue Feb 14, 2025 · 2 comments
Closed

Fails to render svg elements with text-anchor #2381

kesara opened this issue Feb 14, 2025 · 2 comments

Comments

@kesara
Copy link
Contributor

kesara commented Feb 14, 2025

This behaviour can be seen on WeasyPrint version 64.0 and on the current main branch.

When a svg element has text-anchor attribute, WeasyPrint fails to render SVG.

Example:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width initial-scale=1.0">
    <title>SVG test</title>
  </head>
  <body>
    <svg xmlns="http://www.w3.org/2000/svg" font-family="monospace" width="200" height="50" text-anchor="middle">
      <rect x="10" y="10" width="120" height="20" stroke="black" stroke-width="2" fill="lightgray" />
      <text x="75" y="25" fill="black">Hello, World!</text>
    </svg>
  </body>
</html>

Log:

$ weasyprint --debug svg.html svg.pdf
INFO: __init__.py:162 (__init__): Step 1 - Fetching and parsing HTML - svg.html
INFO: __init__.py:69 (__init__): Step 3 - Applying CSS
INFO: document.py:229 (_build_layout_context): Step 4 - Creating formatting structure
INFO: page.py:962 (make_all_pages): Step 5 - Creating layout - Page 1
INFO: __init__.py:118 (generate_pdf): Step 6 - Creating PDF
ERROR: images.py:290 (draw): Failed to render SVG image file:///Users/kesara/lab/WeasyPrint/svg.html
DEBUG: images.py:291 (draw): Error while rendering SVG image:
Traceback (most recent call last):
  File "/Users/kesara/lab/WeasyPrint/venv/lib/python3.13/site-packages/weasyprint/images.py", line 286, in draw
    self._svg.draw(
    ~~~~~~~~~~~~~~^
        stream, concrete_width, concrete_height, self._base_url,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        self._url_fetcher, self._context)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kesara/lab/WeasyPrint/venv/lib/python3.13/site-packages/weasyprint/svg/__init__.py", line 392, in draw
    self.draw_node(self.tree, size('12pt'))
    ~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kesara/lab/WeasyPrint/venv/lib/python3.13/site-packages/weasyprint/svg/__init__.py", line 473, in draw_node
    self.draw_node(child, font_size, fill_stroke)
    ~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kesara/lab/WeasyPrint/venv/lib/python3.13/site-packages/weasyprint/svg/__init__.py", line 498, in draw_node
    if is_valid_bounding_box(node.text_bounding_box):
                             ^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'Node' object has no attribute 'text_bounding_box'. Did you mean: 'bounding_box'?
INFO: __init__.py:228 (generate_pdf): Step 7 - Adding PDF metadata
@liZe
Copy link
Member

liZe commented Feb 14, 2025

Hi!

Thanks for the bug report. I think it’s a duplicate of already fixed #2375, it works for me with the current main branch.

@kesara
Copy link
Contributor Author

kesara commented Feb 14, 2025

Oh! My apologies.
I haven't pulled the latest changes to my branch. 🤦
Yes, it's fixed in the main branch.

@kesara kesara closed this as completed Feb 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants