Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert SVG to PDF C#?

I've had this issue for a few days and have tried a number of things. Essentially, I'm having users edit an SVG and once they're finished, they save it back to a file path on the server. My problem is my SVG contains an href to a file in a directory. As an SVG, it can reference the file with no problem but when I convert the file to a PDF, it doesn't have the image but it does have the other SVG elements such as lines and text. Has anyone had this issue or know how to resolve? I'm using ImageMagick to convert but it still has that problem. See below for my controller code:

    [HttpPost]
    public void ConvertToPDF(string[] svgs, string pdfFilePath, List<string> files)
    {
        MagickReadSettings settings = new MagickReadSettings();
        using (MagickImageCollection images = new MagickImageCollection())
        {
            foreach(var s in svgs)
            {
                var fileName = System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["ConvertFromXMLToSvgTempPath"] + s + ".svg");

                //images.Read(fileName, settings);
                //var readSettings = new MagickReadSettings() { Format = MagickFormat.Svg };

                using (var image = new MagickImage(fileName))
                {
                    image.Format = MagickFormat.Pdf;
                }
            }
            images.Write(pdfFilePath);
        }
    }
like image 783
Nyssa Turner Avatar asked Oct 14 '25 14:10

Nyssa Turner


1 Answers

FYI - I ended up converting the image path into Base64. The rest of the SVG converted correctly. Hope this helps anyone in the future.

like image 94
Nyssa Turner Avatar answered Oct 17 '25 02:10

Nyssa Turner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!