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

Issue with parsing svg with values in px units and in exponential notation #376

Closed
karthurdnt opened this issue Mar 30, 2022 · 3 comments
Closed
Assignees

Comments

@karthurdnt
Copy link

karthurdnt commented Mar 30, 2022

Hi guys, I'm new to fpdf and I'm loving it, mainly because of the new svg feature. But I had a simple issue with it.

While parsing a svg I've got a ValueError: could not convert string to float: '.31512px'
Then I simply manually removed the 'px' strings at the stroke-width value in the svg file.

But then I've got a ValueError: could not convert string to float: 'e'
So I also replaced all the 'e-' strings from the svg files and it worked fine.

Since python float() function works fine with exponential notation, it seems like fpdf is breaking something before trying to float those values. Maybe it is splitting those strings before processing or something.

Minimal code

import fpdf

svg = fpdf.svg.SVGObject.from_file('test.svg')

pdf = fpdf.FPDF(unit="pt", format=(svg.width, svg.height))
pdf.add_page()
svg.draw_to_page(pdf)

pdf.output("test.pdf")

Reference of a simplified svg file:(only one path object) reproducing the cited issue:

<svg width="793.7" height="1122.5" version="1.1" viewBox="0 0 210 297" xmlns="http://www.w3.org/2000/svg">
 <g transform="translate(-10.308 6.2131)">
  <path d="m73.936 15.983c-5.6609 3.7267-10.789 8.806-14.443 16.149-0.95093 1.911-1.3942 4.2456-2.2123 6.2766-0.65959 1.6374-1.5283 3.0903-2.2123 4.7074-1.0605 2.5072-1.8436 5.2305-2.7654 7.8457-2.3665 6.714-0.55578 0.0077-2.2123 4.7074-0.04281 0.12146-0.11951 6.701 0 7.0611 0.07394 0.22279 0.37208-0.12706 0.52078 0 0.7767 0.66371 1.3864 1.6858 2.18 2.3079 2.1221 1.6637 4.3651 2.0103 6.637 3.1383 14.399 7.1487 1.2526 0.76149 11.615 7.0611 1.0717 0.65155 2.2598 0.87603 3.3185 1.5692 4.7573 3.1147 7.7432 6.6652 7.7432 14.122 0 2.9823 0.21402 5.0681-1.6593 7.0611-3.6541 3.8876-7.8545 6.5323-11.615 10.199-5.4842 5.3484-10.87 10.821-16.592 15.691-0.65879 0.56071-10.607 7.5561-9.4024 9.4149 0.36692 0.56639 4.7508 0 5.4412 0 6.5129 0 14.093-0.69517 20.464 0 2.4177 0.26382 4.7949 1.0326 7.1901 1.5691 1.0844 0.24288 5.6842 0.55553 6.637 1.5692 1.264 1.3448 1.1062 1.422 1.1062 3.1055 0 0.78294 0.2052 1.622 0 2.3488-0.74992 2.6561-4.2691 4.4181-5.5308 5.492-1.71 1.4554-3.2622 3.2653-4.9777 4.7074-5.2531 4.4159-10.526 8.8291-16.039 12.553-1.7289 1.1679-14.757 7.3513-14.933 10.199-0.02941 0.47533 0.67374 0.0369 1.0091 0 2.2123-0.26152 4.4247-0.52303 6.637-0.78455 2.028-0.52306 4.0304-1.3159 6.0839-1.5692 5.4361-0.67054 11.131 0 16.592 0 0.91272 0 4.3953-0.92303 3.3185 1.9087-0.58244 1.5316-5.9702 1.4227-6.637 1.5645-10.688 2.2741-20.892 6.3212-30.973 11.769-0.12432 0.0672-5.4927 3.5314-5.5308 3.9229-0.04691 0.48239 0.6884-0.0502 1.0299 0 1.4408 0.21168 2.8752 0.50434 4.3102 0.78454 2.5829 0.50438 5.1621 1.0461 7.7431 1.5692 8.6671 1.7564 11.345 2.062 21.017 5.492 2.2624 0.80233 4.4132 2.1421 6.637 3.1383 2.029 0.90891 4.29 1.3206 6.0839 3.1383 0.63648 0.64491 1.0485 1.6606 1.6592 2.3537 0.32191 0.36531 0.81039 0.37723 1.1062 0.78458 1.226 1.6884-2.1338 1.1091-3.21 1.2479-3.3252 0.42881-6.6312 1.127-9.9555 1.5692-2.5763 0.34264-5.1684 0.41934-7.7431 0.78458-2.958 0.41961-5.8807 1.3475-8.8493 1.5691-4.0503 0.30239-8.1119 0-12.168 0h-9.4024 1.0298c24.025 0-6.0877 0.21047 21.971-0.78458 14.541-0.51568 29.149 0 43.693 0 5.6856 0 11.476 0.47308 17.146 0 10.323-0.86137-1.8427-0.58129 9.4024-2.3537 3.1623-0.49845 6.7333 0 9.9555 0 0.92181 0 1.8656 0.28365 2.7654 0 0.80485-0.25372 1.4468-1.1348 2.2123-1.5691 3.495-1.9831 8.4047-3.1603 12.168-3.9229 1.6271-0.32974 2.8861 0.52492 4.4247-0.78459 1.2736-1.084 1.9934-3.7676 3.3185-4.7074 0.3298-0.23392 0.77089 0.21764 1.1062 0 0.12163-0.079 0.0886-2.6225 0-3.0002-0.71902-3.0663-5.3742-5.9397-6.6335-7.0562-4.0209-3.5649-4.5776-4.3343-8.8493-7.0612-2.1856-1.3952-4.471-2.4675-6.637-3.9229-1.3433-0.90261-2.4915-2.3552-3.8716-3.1383-7e-5 -4e-5 -8.8492-3.1382-8.8493-3.1383-3.0041-1.3457-6.0316-2.7089-8.8493-4.7074-0.59454-0.42171-1.0421-1.2189-1.6592-1.5691-2.9434-1.6701 1.1888 2.5052-2.7654 0-0.22049-0.13973 0.28598-0.60705 0.51226-0.72666 2.6912-1.4226 5.4693-2.494 8.1737-3.8649 12.309-6.2393 4.3828-3.8541 22.676-9.4149 2.2454-0.68256 10.359-2.0953 13.274-2.3537 1.6152-0.14319 3.3362 0 4.9778 0 0.17032 0-0.35737 0.10441-0.51095 0-0.19994-0.13589-0.27919-0.50781-0.47034-0.66715-0.71083-0.59257-1.4348-1.1799-2.2123-1.5692-2.3646-1.1839-4.7448-2.3502-7.1901-3.1383-17.192-5.5408-33.766-8.2294-50.342-17.431-1.6909-0.93859-3.3017-2.1476-4.9777-3.1383-0.53815-0.31808-1.0876-0.62237-1.6592-0.78456-0.36156-0.10257-1.0605 0.51901-1.1062 0-0.04809-0.54617 0.70842-0.45529 1.0411-0.73848 1.4579-1.2409 2.8059-2.7536 4.3271-3.8306 8.4641-5.9922 13.124-9.0587 21.57-12.553 3.0909-1.2788 6.2452-2.2426 9.4024-3.1383 0.87356-0.24784 2.1537 0.21018 2.2091 0 0.0661-0.2507-0.25179-0.40257-0.40025-0.56775-0.70778-0.78751-1.3443-1.7621-2.1646-2.286-1.2238-0.78164-2.5876-1.0148-3.8716-1.5692-2.9565-1.2764-5.9277-2.4937-8.8493-3.9228-5.2847-2.585-11.798-6.5558-16.592-10.199-2.1239-1.6141-12.155-12.553-14.38-12.553-0.16942 0 0.34754 0.07601 0.50827 0 0.3594-0.16995 0.64872-0.59238 1.0165-0.72103 1.8254-0.63851 3.6631-1.2634 5.5308-1.5691 10.284-1.6833 20.642-1.5692 30.973-1.5692 0.1694 0-0.34563 0.06747-0.50822 0-1.1055-0.45868-2.1757-1.0752-3.2736-1.5691-2.3902-1.0753-4.7627-2.2456-7.1901-3.1383-9.0318-3.3216-6.867-0.73692-15.486-7.0611-1.5239-1.1181-12.498-11.705-15.486-12.553-0.54234-0.15387-1.1342 0.24674-1.6592 0-0.07609-0.03578 0-2.2794 0-2.5552 0-3.4076 0.46894-7.5253 0-10.851-0.48916-3.4695-3.5337-7.8457-6.0804-7.8457h0.37999 2.1582 6.0839c1.1062 0 2.2274-0.25797 3.3185 0 2.3003 0.54384 4.3746 2.336 6.637 3.1383 8.9724 2.1213-0.57728-0.7983 4.9777 2.3537 1.6676 0.94623 0.6096-1.2918 2.2123 0.78455 0.12321 0.15961-0.33371 0-0.50056 0h-2.1598c-2.3876 0-4.9478 0.5786-7.1901-0.78455-0.75782-0.46072-1.4302-1.1993-2.2123-1.5692-7.7e-5 -3.7e-5 -2.2123 3.4e-5 -2.2123 0-0.61718-0.3502-1.0421-1.2189-1.6592-1.5691-0.95549-0.54216-2.3372 0.45248-3.3185 0-0.63802-0.29418-1.3566-3.3103-1.5571-3.5622-0.11877-0.14916-0.35074 0.13826-0.47589 0-0.18728-0.20691 0.22616-0.90788 0-1.0102-0.33998-0.15385-0.74447 0.20554-1.0706 0-0.1449-0.09133 0.11804-0.52459 0-0.67474-0.13165-0.16745-0.37288 0.11378-0.53046 0-0.69308-0.50041-2.4254-3.2018-2.7552-3.6101z" fill="#008000" opacity=".44726" stroke="#000" stroke-width=".31512px"/>
 </g>
</svg>

And
Environment

  • OS (Ubuntu)
  • Python 3.9.7
  • fpdf2 2.5.1 (latest version of fpdf2 from the git repository)
@karthurdnt karthurdnt added the bug label Mar 30, 2022
@karthurdnt karthurdnt changed the title Issue with parsing svg with values in exponential notation Issue with parsing svg with values in px units and in exponential notation Mar 30, 2022
@karthurdnt
Copy link
Author

karthurdnt commented Mar 31, 2022

If it is of any use, this is how I tweaked the svg file so it would bypass the bug:

import re
with open('modelo.svg', 'r') as f1, open('modelo2.svg', 'w') as f2:
    svg = f1.read()
    svg = svg.replace('px','')
    e_numbers = re.findall(r"\d+\.\d+e-\d+|\d+e-\d+",svg,re.M) # finds x.xe-x or xe-x numbers
    e_numbers.sort(key=len,reverse=True) # sorts the list from longest to shortest to prevent a 1e-1 replacing a 11e-1
    for x in e_numbers:
        svg = svg.replace(x,f'{float(x):.10f}'.rstrip('0'))
    f2.write(svg)

@Lucas-C
Copy link
Member

Lucas-C commented Mar 31, 2022

I'll have a look at this soon, thanks for reporting this issue @karthurdnt

@Lucas-C
Copy link
Member

Lucas-C commented Apr 1, 2022

Solved in 4ec48b6 by adding support for parsing exponentiated numbers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants