Excel to HTML Image Issue


#1

I’m converting an Excel file to HTML which consists of a chart sheet and a data sheet (see attached file), and the resulting HTML is written to the Response of a .NET aspx web form

var handler = new ViewerHtmlHandler();
using (var stream = new MemoryStream())
{
    // the document is downloaded from Azure Blob Storage
    await cloudBlockBlob.DownloadToStreamAsync(stream);
    stream.Position = 0;
    var page = 1;
    var pagesToRender = new List<int>() { 1 };
    var options = new HtmlOptions()
    {
        EmbedResources = true,
        PageNumber = page,
        PageNumbersToRender = pagesToRender
    };
            
    var pages = handler.GetPages(stream, options);
    var firstPage = pages.First();
    Response.Write(firstPage.HtmlContent);
}
Response.End();

This works, except that the chart slide is converted to an image and the <img> tag’s src attribute points to a location on the server’s temp directory, which is not served by the web server.

<img src="D:\local\Temp\ChartMilk_Utilisation.jpg" alt="1">

How do I configure this to include the image correctly? I was hoping that they would be converted to data urls. I’ve tried all possible combinations of properties in populating the HtmlOptions instance.

Thanks for your help!

milkutil_dataset_13aug15.csv.xlsx.zip (67.2 KB)


#2

@saviourofdp,

Thanks for contacting support.

We are able to reproduce your reported issue and it is observed that the API is not embedding the images within the HTML even if EmbedResources property of HtmlOptions is set to true. Therefore, we have logged it in our Issue Tracking System (Issue ID: VIEWERNET-1226) for further investigation. We shall keep you notified in case of any updates.


#3

Thanks. For what it’s worth, I will post my workaround which uses HtmlAgilityPack to parse the HTML allowing me to convert any images to data urls

// if images are written separately to disk, convert them
// to data urls, otherwise they won't be served correctly
private static string ResolveImages(string htmlContent)
{
    var htmlDoc = new HtmlAgilityPack.HtmlDocument();
    htmlDoc.LoadHtml(htmlContent);
    var imageNodes = htmlDoc.DocumentNode.SelectNodes("//img");
    if (imageNodes != null)
    {
        foreach (var imageNode in imageNodes)
        {
            var src = imageNode.GetAttributeValue("src", "");
            if (File.Exists(src))
            {
                imageNode.SetAttributeValue("src", GetDataURL(src));
            }
        }
        htmlContent = htmlDoc.DocumentNode.OuterHtml;
    }
    return htmlContent;
}

// see https://stackoverflow.com/a/6826577/3898606
public static string GetDataURL(string imgFilePath)
{
    return string.Format("data:image/{0};base64,{1}"
                        , Path.GetExtension(imgFilePath).Replace(".", "")
                        , Convert.ToBase64String(File.ReadAllBytes(imgFilePath)));
}

#4

@saviourofdp,

Thanks for sharing the workaround on the forum. It will surely help others until the issue is fixed.


#6

@saviourofdp,

The issue you have found earlier (logged as VIEWERNET-1226) has been fixed in this release.