typst / svg2pdf

Converts SVG files to PDF.
Apache License 2.0
273 stars 32 forks source link

CLI: Image transformation not being applied, and text not loading despite being system fonts #58

Closed Tameflame closed 6 months ago

Tameflame commented 7 months ago

I have a simple SVG which contains an embedded jpg image transformed so that it fits the height of the svg document, as well as an embedded svg which is text using the system font Verdana in the middle. But running svg2pdf loses the transform and also loses the text.

Steps to reproduce:

  1. Save the attached svg as assets/svg_test.svg
  2. run svg2pdf ./assets/svg_test.svg ./assets/svg_test.pdf

Expected Result: The PDF should render the embedded image properly fitted onto the 1920x1080p canvas, and the text should be rendered in the middle

Actual Result: The image is no longer transformed to fit the canvas, and the text is missing


Format of svg_test.svg

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" height="1920" width="1080">
<image height="100%" xlink:href="data:image/jpeg;base64,/9j/4AAQSkZ...REST_OF_IMAGE_DATA" preserveAspectRatio="xMidYMid slice"/>
    <svg height="1920" width="1080" x="0" y="0">
        <text fill="#ffffff" font-family="Verdana" font-size="35" font-weight="bold" text-anchor="middle" x="50%" y="50%">Hello World</text>
    </svg>
</svg>

THE ORIGINAL svg_test.svg svg_test

The faulty output I got: svg_test.pdf

LaurenzV commented 7 months ago
image

Text seems to work fine for me, so not sure why it's not working for you.

As for the transform, there does seem to be an issue indeed, but it's an upstream issue, I can also recreate the problem with resvg. Will try to investigate once I find the time.

Tameflame commented 7 months ago

This is strange, did you look at the pdf file attached to my comment or did you generate a new one? Because the pdf file in my comment doesn't render the text, despite Verdana being installed on my windows system. I've checked with Edge, Acrobat Pro, and iPhone on edge+safari, text doesn't render on any of them.

Conversion was done with cargo installed svg2pdf on wsl2 running ubuntu, if that matter. svg2pdf --version gives svg2pdf-cli 0.9.1.

Any suggestions?

LaurenzV commented 7 months ago

Can you try installing from git?

Then you should be able to run svg2pdf fonts and it should print all fonts that it was able to find. That subcommand is not yet available in the newest version on cargo I think.

LaurenzV commented 7 months ago

This is strange, did you look at the pdf file attached to my comment or did you generate a new one?

Oh yes, I created a new one. If I open your's it doesn't seem to show up either. Not saying that there isn't an issue with svg2pdf, but at least when converting the SVG myself it works fine, so I suspect that svg2pdf somehow isn't able to find your fonts properly.

Tameflame commented 7 months ago

Ahh it turns out Verdana isn't installed on my ubuntu os, installing it and running the conversion again works.

Thanks very much. I'll still keep this issue open as the image transformation thing still doesn't work, but it would be great to have it fixed so it saves me pre-transforming images with an image editing library before embedding them into my svg (and then converting via this app)

Thanks!

LaurenzV commented 7 months ago

Yes, I already know the issue and how to fix it. I'm currently trying to deal with it upstream. Although it will probably take a while until everything is updated, so if it's time-pressing it might be better to convert it with a different tool for now. :/

Tameflame commented 7 months ago

No worries, users like me appreciate all the time and effort you and others do for this library!

If I may ask here, my use-case is to lay out images on a new svg, I understand that there are js libraries where you can use css to accomplish this. Are there any equivalents in rust that you are aware of?

For example, I might have something like a row of items which I want aligned from right to left rather than left to right. I understand that you can use .svg with css embedded? or is there something else to accomplish this?

Apologies if this is a newbie question, I'm new to .svg

Thanks again!

LaurenzV commented 7 months ago

So you want to embed multiple SVGs into a new SVG and arrange them in some way? This library only supports converting single SVGs into PDF, you could use Typst instead to define a template for how your SVGs should be arranged, and then use the SVG export functionality to create a new SVG from that I suppose. But keep in mind that typst is a heavy dependency.

Other than that, I'm not aware of any other libraries.

LaurenzV commented 7 months ago

The bug has been fixed upstream now, should be fixed here too once the next version is released.

Tameflame commented 6 months ago

Awesome, thanks. Looking forward to the fix!

LaurenzV commented 6 months ago

It's fixed on main now, so feel free to use that version if you need it already.

Otherwise, it will be in the next release, whenever that will be. Thanks for reporting the bug!