Convert SVG to PDF using ImageMagick with reliable font selection?

I have a simple test SVG that uses two installed fonts ( Helvetica-Narrow and Helvetica-Bold ):

 <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100" height="50"> <text x="0" y="24" fill="blue" font-family="Helvetica-Bold" font-size="24px">Bold</text> <text x="0" y="48" fill="blue" font-family="Helvetica-Narrow" font-size="24px">Narrow</text> </svg> 

If I convert this to a PDF file using ImageMagick (ImageMagick 7.0.2-0 Q16 x86_64 running on CentOS Linux 7 (Core)), then the result will not use the installed fonts.

For instance:

 $ convert -density 600 test.svg test.pdf 

Productivity:

default font used

It seems that ImageMagick by default uses the usual Helvetica weight, which does not match any of the font families specified in the input SVG.

Next, I will try to specify the path to one of the fonts specified in the input SVG. This is the path to the Helvetica-Bold font, as defined when you run convert -list font .

 $ convert -density 600 -font /net/module/sw/ghostscript-fonts/5.50-32/n019004l.pfb test-helvetica-mix.svg test-helvetica-mix-bold.pdf 

bold

The first <text> element is correct - it uses Helvetica-Bold . The second <text> element is incorrect - it also uses Helvetica-Bold , but should really use Helvetica-Narrow .

However, I am approaching this approach, so I am trying to add the path to the second font used in the input SVG:

 $ convert -density 600 -font /net/module/sw/ghostscript-fonts/5.50-32/n019004l.pfb -font /net/module/sw/ghostscript-fonts/5.50-32/n019043l.pfb test-helvetica-mix.svg test-helvetica-mix-both.pdf 

both fonts added

ImageMagick uses the Helvetica-Narrow font for both elements, which is not true for the same reason.

Is there a way to convince ImageMagick to use the correct fonts specified in the <text> elements in the input SVG?

+6
source share
3 answers

Looking at what convert -list font says

 Font: Helvetica-Bold family: Helvetica style: Normal stretch: Normal weight: 700 glyphs: /usr/share/fonts/type1/gsfonts/n019004l.pfb Font: Helvetica-Narrow family: Helvetica Narrow style: Normal stretch: Condensed weight: 400 glyphs: /usr/share/fonts/type1/gsfonts/n019043l.pfb 

The font family for Helvetica-Bold and Helvetica-Narrow is Helvetica and Helvetica Narrow respectively. You can achieve the intended effect (boldness / narrowness) using additional attributes on the <text> , for example:

 <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100" height="50"> <text x="0" y="24" fill="blue" font-family="Helvetica" font-size="24px" font-weight="700">Bold</text> <text x="0" y="48" fill="blue" font-family="Helvetica Narrow" font-size="24px" font-stretch="Condensed">Narrow</text> </svg> 

I investigated if we can directly use the type name ( Helvetica-Bold , Helvetica-Narrow ), but to no avail, this is the only solution I could find that respects the ImageMagick convert tool. Hope this helps.

+2
source

Try editing the ImageMagick font file located somewhere like ~ / .magick / type.xml

You can force it to use a specific font and recognize it by any name. imagick_type_gen.

Here is a link to something that should help what you are trying to do: Make ImageMagick recognize the font

Further reading with an additional tool for use with ImageMagick: http://gothick.org.uk/2008/03/14/using-os-x-fonts-in-imagemagick/

0
source

You need to clarify where the font is installed: in the installation of SVG, OS or embedded in PDF.

SVG has its own font system, which does not cause any problems. SVG was first created before web fonts were actually used, so SVG created its own font system, which was portable. You now have a font system for SVG fonts only, and they probably won't convert to a PDF file.

It’s best to use the @font-face rule, for example, @font-face { font-family: Helvetica; src: url('/var/www/fonts/Helvetica.ttf'); } @font-face { font-family: Helvetica; src: url('/var/www/fonts/Helvetica.ttf'); } @font-face { font-family: Helvetica; src: url('/var/www/fonts/Helvetica.ttf'); } . See this ( https://graphicdesign.stackexchange.com/questions/5162/how-do-i-embed-google-web-fonts-into-an-svg/5167#5167 ) for another view.

An alternative is to completely abandon fonts and provide SVG with the right path. That is, create a PDF file with graphics, which is the display font. This is displayed when using Inkscape to make a different travel destination, from PDF to SVG. (see Convert PDF to clean SVG? or https://www.magebay.com/forum/forums/topic/pdf-svg-doesnt-embed-the-fonts/ ).

Sorry, this does not come down to 'adding this one line' without knowing much more about the details, but this should help.

0
source

All Articles