Converting SVG with custom fonts to PNG using ImageMagick

后端 未结 4 1800
误落风尘
误落风尘 2021-01-06 06:07

I\'m converting SVG\'s to PNG using ImageMagick and all works like a charm until I tried adding custom fonts using @font-face and then in the SVG file. In Batik this worked

相关标签:
4条回答
  • 2021-01-06 06:12

    Try rsvg-convert (a command line program) provided by librsvg2 package.

    I have the same problem with ImageMagick on Amazon Linux (but not on my Ubuntu desktop). I think the SVG handling of the ImageMagick is rather poor. It does not recognize "display: none" style property correctly.

    0 讨论(0)
  • 2021-01-06 06:17

    Inkscape can be run from the command line. It has a wealth of options for doing the conversion (see man inkscape) and via the --verb and --select options can even allow carrying out actions as you'd do them from inside the GUI.

    It's also fairly quick at the conversion, as halfer pointed out. However note that its renderer is designed for interactive display rather than the single-pass static display that rsvg does, so rsvg may be faster in some cases. Also, installation of Inkscape on web servers can sometimes be problematic simply due to the large number of dependencies this GUI app pulls in.

    When we added these command line options to Inkscape it was partly because rsvg lacked support for a lot of SVG features, including font faces. Since that time rsvg has improved and I think it now should support substitution of fonts via the font-face property. The most common issues are having fonts installed in places that rsvg doesn't look (maybe proprietary fonts are kept in a different path in your distro?)

    Good luck, let us know if you sort it out.

    0 讨论(0)
  • 2021-01-06 06:18

    I once had a bunch of SVG files, one page per file (an entire book). The fonts were auto-generated, one for each page. The character encoding was non-standard, I believe that only the characters actually used in the page were put into the fonts. Even though the fonts were embedded using data-uris into the SVG, none of the programs I used (Imagemagick, Inkscape, rsvg-convert) could display them. Only Firefox was able to render the pages correctly. Here's what I did:

    First I created a simple HTML with all the pages:

    <html>
        <body>
             <img src="p001.svg" />
             <img src="p002.svg" />
    

    etc

    Then, I opened the HTML in Firefox. It took some time but it rendered the entire book.

    Then, I printed the entire page as PDF. I first set the print options to not print out headers/footers.

    The result was a large PDF of several megabytes (there were also raster images in there).

    Not the easiest solution, but it worked. It's a pity that none of the other SVG renderers are as good as the one in Firefox.

    Hope this helps someone.

    0 讨论(0)
  • 2021-01-06 06:19

    I had this answer after asking for rendering html to an image but it works also for SVG: https://stackoverflow.com/a/56194265/118125

    "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" --headless --screenshot --window-size=256,256 --default-background-color=0 image.svg
    

    It generates a screenshot.png file according to what Google Chrome would render.

    0 讨论(0)
提交回复
热议问题