Automatically converting images to SVG and embedding when converting document to HTML

I am looking to see if it is possible to automatically convert images to SVG and embedd them into HTML with the existing features in the GroupDocs.Convert package. I am trying to stay away from having to cache images. If anyone can point me in the right direction, or other forum posts of peopl trying to achieve the same thing that would be great, I could not find any.

This is a slimmed down version of what I am already working with.

 class CustomContentConverter : IContentConverter
    {
        static CustomContentConverter()
        {
            License groupDocLicense = new License();
            using (var stream = new MemoryStream(Resources.GroupDocs_Total_NET))
                groupDocLicense.SetLicense(stream);
        }

        private PossibleConversions GetPossibleConversions(string fileName)
        {
            var sourceFileType = FileType.FromFilename(fileName);
            return Converter.GetPossibleConversions(sourceFileType.Extension);
        }

        public bool CanConvert(string from, string to)
        {
            if (string.IsNullOrWhiteSpace(to))
                throw new ArgumentException("Invalid target type", nameof(to));
            if (string.IsNullOrWhiteSpace(from))
                throw new ArgumentException("Invalid file name", nameof(from));

            var possibleConversions = GetPossibleConversions(from);
            return possibleConversions.All.Any(tc => tc.Format.Extension == to);
        }

        public byte[] Convert(Stream content, string from, string to)
        {
            if (content == null)
                throw new ArgumentNullException(nameof(content));
            if (string.IsNullOrWhiteSpace(to))
                throw new ArgumentException("Invalid target type", nameof(to));
            if (string.IsNullOrWhiteSpace(from))
                throw new ArgumentException("Invalid file name", nameof(from));

            var possibleConversions = GetPossibleConversions(from);

            var resultCopyStream = new ResultStream();
            using (Converter converter = new Converter(
                () => content,
                () => possibleConversions.LoadOptions))
            {
                var targetConversionInfo = possibleConversions[to];
                var convertOptions = targetConversionInfo.ConvertOptions;
                var result = new ResultStream();

                converter.Convert(() =>
                {
                    
                    if (result.CanRead)
                    {
                        result.CopyTo(resultCopyStream);
                        return result;
                    }
                    else
                    {
                        return resultCopyStream;
                    }
                },
                targetConversionInfo.ConvertOptions);

                return result.WrittenContent;
            }
        }

        class ResultStream : MemoryStream
        {
            public byte[] WrittenContent { get; private set; }

            public override void Close()
            {
                if (CanRead)
                {
                    Position = 0;
                    WrittenContent = ToArray();
                }

                base.Close();
            }
        }
    }
1 Like

@damien113

Would you kindly provide additional details regarding this use-case? A practical example illustrating what you are seeking would greatly assist us in comprehending the scenario more effectively.
Do you intend to convert an image into SVG format and subsequently transform that SVG into HTML?

I want to generate a html representation of any type of document (Within reason) to use as a preview of a document, the problem we are having right now is that it is missing the images, we do not want to cache them though, and want to embedd as many as possible as svgs. Current supporting PDF, emails, and office documents converted to html with their images would work, but not even sure where to start to get to that point

@damien113
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): CONVERSIONNET-6321

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

Thank you :slight_smile: Will I get a response on this forum post?

@damien113

Currently, conversion to HTML embeds all images as base64 encoded images, but they are not converted to SVG. However, we are looking into the possibility if images to SVG conversion is possible.