Convert SVG to PDF on Server/Desktop

The Apryse SDK 9.5 or later can be used to convert SVG 1.1 documents to PDF without any external dependencies.

Usage

1using (PDFDoc doc = new PDFDoc()){
2 pdftron.PDF.Convert.FromSVG(doc, input_file_path + intput_file_name, null);
3 doc.Save(output_file_path + output_file_name, SDFDoc.SaveOptions.e_remove_unused);
4}

Supported SVG Flavors

The Apryse SDK accepts SVG graphics as follows:

  • SVG 1.1 (Second Edition) as published by the W3C.
  • The following Unicode formats and encodings are supported: UTF-8, UTF-16, ISO 8859-1 ... ISO 8859-15, ASCII
  • CSS styling is available, but some CSS elements are unsupported.
  • In addition to SVG files in plain text format Flate-compressed SVG files (*.svgz) are supported.
  • Colors can be specified with additional color spaces according to the SVG Color 1.2 draft (see SVG Color Extension).

See Unsupported SVG Features for restrictions.

Size of SVG Graphics

SVG graphics specify the width and height in the svg element which defines the mapping of the SVG graphics to the target viewport (e.g. the browser window or some part of a PDF page). Often the size of the viewport is specified in absolute units, e.g.

sh

1<svg xmlns="http://www.w3.org/2000/svg" width="640mm" height="480mm">

If the size is specified in pixels (px) the Apryse SDK uses 1pt=1px. These values are also used to calculate the object box for fitting operations. The svg element may also contain the viewBox attribute which specifies a window inside the viewport. You can override the size values specified in the SVG graphics file with the SetForcedWidth() and SetForcedHeight() methods of the SVGConvertOptions class.

SVG graphics without absolute size information

Some SVG graphics do not contain absolute size information since width and height are missing or contain only relative values such as in the following example:

sh

1<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%">

This is used for SVG graphics which are not intended for standalone use. If no absolute size information is available in the SVG graphics you can specify the desired dimensions via the SetFallbackWidth() and SetFallbackHeight() methods of the SVGConvertOptions class. In the fallback width and height are not set the bounding box of the SVG graphics is calculated automatically. By default the calculated bounding box is moved such that the graphics is located at the original position in the coordinate system (i.e. the box does not necessarily use the origin as corner).

Font Selection

Font selection algorithm

Font selection in SVG is controlled by the following properties:

sh

1font-family
2font-style
3font-weight
4
5font-stretch
6font-variant
7font-size
8font-size-adjust

Only the first three of these properties are relevant for selecting an external font. In order to select a suitable font, the following font names are constructed:

sh

1<font-family>,<font-weight>,<font-style>
2<font-family>-<font-weight><font-style>
3<font-family>,<font-normweight>,<font-style>
4<font-family>,<font-weight>,<font-normstyle>
5<font-family>,<font-normweight>,<font-normstyle>

where <font-normweight> is one of

sh

1Regular, Thin, Extralight, Light, Medium, Semibold, Bold, Extrabold, Black

and <font-normstyle> is

sh

1Italic

For example, with the following SVG font specification:

sh

1font-family="Tahoma" font-weight="Bold" font-style="Italic"

The SDK will search for the font Tahoma,Bold,Italic. The SDK then tries to load the font names listed above one after the other until the process is successful and a font could be loaded. If all attempts fail, the SDK tries to load the font with the name <font-family> and simulates the Bold and Italic properties if required.

The font-family property may also contain multiple font family names, e.g.

sh

1font-family="Georgia, 'Minion Web', 'Times New Roman', Times, 'MS PMincho', serif"

In this case the SDK tries to load the next font in the list if a particular font family could not be loaded. If some font for a font-family list could be loaded, the SDK attempts to load the remaining font-families in the list as fallback fonts for the first loaded font. If the master font already has fallback fonts because it has been loaded earlier, the new fallback fonts will be appended to the list of existing fallback fonts.

Mapping generic SVG font families to PDF core fonts

The SDK automatically maps the generic SVG font families monospace, sans-serif and serif to the Latin core fonts at the first occurrence of a generic SVG font family. The full list of generic font name mappings is as follows (there are no default mappings for the generic font families cursive and fantasy):

Generic font name

PDF core font name

monospace

Courier

monospace,Bold

Courier-Bold

monospace,Italic

Courier-Oblique

monospace,Bold,Italic

Courier-BoldOblique

sans

Helvetica

sans,Bold

Helvetica-Bold

sans,Italic

Helvetica-Oblique

sans,Bold,Italic

Helvetica-BoldOblique

sans-serif

Helvetica

sans-serif,Bold

Helvetica-Bold

sans-serif,Italic

Helvetica-Oblique

sans-serif,Bold,Italic

Helvetica-BoldOblique

serif

Times-Roman

serif,Bold

Times-Bold

serif,Italic

Times-Italic

serif,Bold,Italic

Times-BoldItalic

Dealing with missing Fonts and missing Glyphs

Missing fonts and the default font

If all font loading attempts fail, the SDK attempts to load a default font which was specified via the SetDefaultFontFamily() method of the SVGConvertOptions class. By default, the Arial Unicode MS font is used if available, and Helvetica otherwise. It is highly recommended to either make the Arial Unicode MS font available, or to specify another font with a large glyph complement via SetDefaultFontFamily().

Missing glyphs

If a selected font does not contain a required glyph, no text will be visible for that glyph.

Specifying a global fallback font family

The SetFallbackFontFamily() method of the SVGConvertOptions class can be used to specify a family of fallback fonts. SVG style attributes will be applied to this font family name, assuming that style variants of the specified font family are actually available.

SVG Color Extension

SVG by default uses the sRGB color space for text and vector graphics. Embedded images in SVG may use other color spaces, though. For example, an embedded or referenced JPEG image may use the CMYK color space with or without an ICC profile.

In addition to sRGB the SDK supports additional color spaces using syntax extensions according to the SVG Color 1.2 draft specification. The following table lists supported SVG color spaces along with syntax examples:

color space

SVG syntax examples

notes

sRGB

#FF0000

SVG 1.1 default color space

ICC-based color

#7F7F7F icc-color(gray.icc, 0.5)
#CC6633 icc-color(rgb.icc, 0.8, 0.4, 0.2)
#2B7FAB icc-color(cmyk.icc, 0.8, 0.4, 0.2, 0.0)

One, three or four color values must be supplied for Grayscale, RGB or CMYK profiles, respectively.

CIELab

#598237 cielab(50, -25, 35)

CIELch

#FF007E cielch(50, 127, 0)
#FF007E cielchab(50, 127, 0)

Lightness, chroma and hue (in degrees) are converted to CIELab.

n-colorant

#FFE560 device-nchannel(0, 0.7, 0.2)
#FFF36D device-nchannel(1 0 0 0 0.5 0.2 0)

The sRGB fallback color is used.

SVG syntax requires sRGB fallback colors for all variants. The SDK uses the fallback color in case of syntax errors, missing ICC profiles or if the option SetForceSRGB(true) was set via the SVGConvertOptions class.

ICC-based colors

The ICC-based color specifications apply the specified ICC profile which is searched in the SDK's configured resource search path. ICC profiles are ignored if the option SetHonorIccProfile(false) was set via the SVGConvertOptions class.

Gray, RGB and CMYK device color spaces

Device-dependent color spaces are supported with the device-gray, device-rgb and device-cmyk keywords.

DeviceN colors

The device-nchannel syntax can be used to specify DeviceN colors, but these specifications are ignored in general and the sRGB fallback color is used.

Shadings

The color spaces listed above can also be used in SVG gradients. However, PDF requires all stop colors for a gradient to be taken from the same color space. If a gradient uses stop colors from different color spaces the sRGB fallback colors for all stop colors are used.

SVG Contents beyond Vector Graphics and Text

The SDK processes the image element in SVG and accepts JPEG and PNG images as well as nested SVG graphics. Image data may be embedded in the SVG file or located in an external file.

If an image is not available (e.g. because the referenced external image file is missing), the SDK creates a transparent gray checkerboard pattern.

Unsupported SVG Features

Treatment of unsupported features

Unsupported SVG features are ignored. As a result, output is created but some aspects of the graphics may be missing or wrong.

SVG files can contain hyperlinks that can be presented to a user as clickable hyperlinks by an SVG viewer, e.g. in a web browser. Hyperlinks are not imported by the SDK.

SVG graphics may reference external resources such as fonts or images via URLs in the graphics file. The SDK does not support retrieval of remote resources. All external resources referenced by an SVG file must be available locally.

Unsupported SVG elements

The following SVG elements are not supported and are ignored:

  • elements for animation and scripting: animate, animateColor, animateMotion, animateTransform, script, mpath, set
  • elements for filters: feBlend, feColorMatrix, feComponentTransfer, feComposite, feConvolveMatrix, feDiffuseLighting, feDisplacementMap, feDistantLight, feFlood, feFuncA, feFuncB, feFuncG, feFuncR, feGaussianBlur, feImage, feMerge, feMergeNode, feMorphology, feOffset, fePointLight, feSpecularLighting, feSpotLight, feTile, feTurbulence, filter
  • other elements: cursor, foreignObject, vkern

Restricted SVG elements, attributes and properties

The following attributes and properties are subject to restrictions:

  • Some CSS directives are not supported.
  • The font selection property font-variant is supported only with the keyword smallcaps, and only for fonts containing the OpenType feature smcp.
  • Combination of values of the text presentation property text-decoration are not supported. The SDK doesn’t draw the decoration elements as areas with separate fill and stroke color, but draws the decorations as lines. The lines are drawn with the fill color if present, otherwise with the stroke color.
  • The attribute rotate for the textPath element is not supported.
  • The property unicode-bidi is honored only for TrueType/OpenType fonts containing the required tables for Bidi text layout.
  • The property glyph-orientation-vertical is supported according to SVG 1.1 except the angles 180Ëš and 270Ëš which are not supported.
  • The attribute preserveAspectRatio for the view element is ignored.
  • The property overflow for the svg element does not support the value visible if a Form XObject is created, i.e. overflowing content is clipped. This can be avoided by setting SetInline(true) via the SVGConvertOptions class, which is the default.

Unsupported SVG properties

The following SVG properties are not supported and are ignored: alignment-baseline, color-interpolation, color-interpolation-filters, color-rendering, cursor, dominant-baseline, enable-background, filter, flood-color, flood-opacity, glyph-orientation-horizontal, image-rendering, lighting-color, pointer-events, shape-rendering, text-rendering

Unsupported attributes of supported SVG elements

The following attributes of supported SVG elements are not supported and are ignored:

sh

1baseProfile (svg)
2contentScriptType (svg)
3contentStyleType (svg)
4externalResourcesRequired (all elements)
5method (textPath)
6on* (all elements)
7spacing (textPath)
8textLength and lengthAdjust for vertical text (text, textpath, tref, tspan)
9version (svg)
10zoomAndPan (svg)
11xlink:role (all elements)
12xlink:show (all elements)
13xlink:type (all elements)

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales