Karthikeyan VK
Karthikeyan VK

Reputation: 6006

XSL-FO SVG Format support - .Net

I need to render svg in my XSL fo in c#.Net which is available in https://fonet.codeplex.com/. I tried to use svg in the xsl-fo but it does not render any pdf and fails silently.

If anybody has found a solution for this issue please help.

I need my pdf report to support svg contents.

Upvotes: 3

Views: 1070

Answers (1)

Gopalakrishnan T
Gopalakrishnan T

Reputation: 56

Use the below code to add Hander of an image incase of svg extensions

 FonetDriver fonetDriver = FonetDriver.Make();
 fonetDriver.ImageHandler = SvgImageHandler;

Add the SvgImageHandler Hander

 private static byte[] SvgImageHandler(string svgContent)
        {
            if (svgContent.Contains("http://www.w3.org/2000/svg"))
            {
                var svgByteAry = Encoding.UTF8.GetBytes(svgContent);
                using (var stream = new MemoryStream(svgByteAry))
                {
                    var svgDocument = SvgDocument.Open<SvgDocument>(stream);
                    using (var memoryStream = new MemoryStream())
                    {
                        svgDocument.Draw()
                                   .Save(memoryStream, ImageFormat.Png);
                        var byteArray = memoryStream.ToArray();
                        return byteArray;
                    }
                }
            }
            //Skip if not url based image
            if (!Uri.IsWellFormedUriString(svgContent, UriKind.RelativeOrAbsolute))
                return null;

            if (!ValidateUrlImage(svgContent))
            {
                ICacheService cacheService = new HttpCache();
                return cacheService.Get(Constants.NoImage,
                                        () =>
                                        {
                                            var baseDirectory = AppDomain.CurrentDomain.BaseDirectory + ConfigurationManager.AppSettings[Constants.ImagePath];
                                            var defaultUrl = Path.Combine(baseDirectory, Constants.NoImageFile);
                                            var img = Image.FromFile(defaultUrl);
                                            var imgCon = new ImageConverter();
                                            return (byte[])imgCon.ConvertTo(img, typeof(byte[]));
                                        });
            }
            return null;
        }

Return proper image if the url is valid or pass false so the No Image can be rendered. keeping the code more robust.

private static bool ValidateUrlImage(string absoluteUrl)
        {
            Uri uri;
            if (!Uri.TryCreate(absoluteUrl, UriKind.Absolute, out uri))
            {
                return true;
            }
            using (var client = new WebClient())
            {
                try
                {
                    using (var stream = client.OpenRead(uri))
                    {
                        Image.FromStream(stream);
                        return true;
                    }
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }

Upvotes: 4

Related Questions