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

SVG images are not rendered with right size #592

Closed
DanielWulfert opened this issue Nov 2, 2020 · 3 comments
Closed

SVG images are not rendered with right size #592

DanielWulfert opened this issue Nov 2, 2020 · 3 comments

Comments

@DanielWulfert
Copy link

DanielWulfert commented Nov 2, 2020

SVG images with either given dimensions or images with no given dimensions are not rendered with the right size. Reason for that is, that BatikSVGImage.parseLength does not handle double values nor handles css units.
Simple example:

<html` xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/xhtml; charset=UTF-8" />
        <title>Test</title>
    </head>
    <body>
        <div id="content" class="content">
            <div id="logo" style="margin-top:3mm;">
                <img src="some_fancy.svg" alt="here comes the SVG" title="" />
            </div>    
            <div id="main" style="font-size:15px;">Some text below the SVG</div>
         </div>
    </body>
</html>

The images is rendered correctly, but the space around the image is too big. The text should be exactly below the image and the image should be 3mm away from the top. You can find the following warning messages in the output after the rendering:
com.openhtmltopdf.general WARNING:: Invalid integer passed as dimension for SVG: 850.39px
com.openhtmltopdf.general WARNING:: Invalid integer passed as dimension for SVG: 183.035px
As mentioned I am using the BatikSVGDrawer from the openhtmltopdf-svg-support plugin.
Since I have already fixed this issue for my project I will make a pull request later if you like.

DanielWulfert added a commit to DanielWulfert/openhtmltopdf that referenced this issue Nov 2, 2020
Some changes to BatikSVGImage parseLength method, so that it supports
absolute CSS units and fractional measurements as mentioned in danfickle#592.
danfickle added a commit that referenced this issue Nov 4, 2020
We do this by firing up the CSS parser for values of the width and height attributes that are not plain numbers.

With test proof (output identical to Chrome).
@danfickle
Copy link
Owner

Hi @DanielWulfert,

Firstly, thanks heaps for contributing to this project!

I have not used your PR in favor of firing up the CSS parser to deal with unit values as you can see in commit. I think this will handle more units (font relative units, even percentages) than rolling another parser.

However, without your PR I would not have worked on this issue, so thank you.

PS. You may want to try your SVG with this commit. It gives identical output to Chrome with the simple values I have tried it with but you may find other edge cases with more complex SVGs.

Thanks again,
Dan.

@DanielWulfert
Copy link
Author

Hi @danfickle

looks fine to me and thanks for all the efford you put into this project. Great improvement of flying-saucer. Happy to at least contribute a very small part ;)
Keep up the great work.

Regards,
Daniel

@kiranduba
Copy link

Hi @DanielWulfert I tried to render SVG with BASE 64 encoding, mentioned in issue. BUT while building PDF Im getting below warning and SVG image is not aligning as per width & height. Please suggest.

public static void main(final String[] args) {

final String content = "<!DOCTYPE HTML>\r\n" + "<html><body>\r\n"
        + "<img src=\"data:image/svg+xml;base64,PHN2ZwogICB4bWxuczpzdmc9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIgogICB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciCiAgIHZlcnNpb249IjEuMCIKICAgd2lkdGg9Ijc4Mi42MTg1MyIKICAgaGVpZ2h0PSIzNTguODkzMTMiCiAgIGlkPSJzdmcyODA3Ij4KICA8ZGVmcwogICAgIGlkPSJkZWZzMjgwOSIgLz4KICA8ZwogICAgIHRyYW5zZm9ybT0idHJhbnNsYXRlKC00OC43NjI5NiwtNDA5LjkxNjgpIgogICAgIGlkPSJsYXllcjEiPgogICAgPGcKICAgICAgIGlkPSJnMzA4NSI+CiAgICAgIDxwYXRoCiAgICAgICAgIGQ9Ik0gNDguNzYyOTU4LDQwOS45MTY4MiBMIDgzMS4zODE1LDQwOS45MTY4MiBMIDgzMS4zODE1LDc2OC44MDk5NSBMIDQ4Ljc2Mjk1OCw3NjguODA5OTUgTCA0OC43NjI5NTgsNDA5LjkxNjgyIHogIgogICAgICAgICBzdHlsZT0iZmlsbDojZGEyMDI4O2ZpbGwtcnVsZTpub256ZXJvO3N0cm9rZTpub25lIgogICAgICAgICBpZD0icGF0aDI2MzUiIC8+CiAgICAgIDxwYXRoCiAgICAgICAgIGQ9Ik0gNDAxLjI4ODYyLDU5NS4xNTE5OS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Dp3aGl0ZTtmaWxsLXJ1bGU6bm9uemVybztzdHJva2U6bm9uZSIKICAgICAgICAgaWQ9InBhdGgyNjM3IiAvPgogICAgPC9nPgogIDwvZz4KPC9zdmc+\" width=\"400\" height=\"200\"/>\r\n"
        + "</body></html>";

final PdfRendererBuilder builder = new PdfRendererBuilder();

builder.withHtmlContent(content, null);
builder.useSVGDrawer(new BatikSVGDrawer());

com.openhtmltopdf.general WARNING:: Invalid integer passed as dimension for SVG: 782.61853
com.openhtmltopdf.general WARNING:: Invalid integer passed as dimension for SVG: 358.89313

SVG issue --------------------------------------

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

3 participants