Generating mhtml file from url in c#

Hi all

I have a quick question. I see that aspose.cells provides a method that we can generate mhtml file from html file but is it possible to use direct url (http and https)?

Basically I want to create an mhtml file directly from a url, not from a html file in local-drive.

@kungfulee

Aspose.HTML offers the functionality to convert HTML file as well as an URL into other file formats. We tried to use the below sample code snippet to convert an URL to MHTML. However, the process was terminated due to a StackOverFlowException:

using (var document = new Aspose.Html.HTMLDocument("https://www.google.com"))
{
 // Initialize MHTMLSaveOptions
 var moptions = new Aspose.Html.Saving.MHTMLSaveOptions();
 // Convert HTML to MHT
 Aspose.Html.Converters.Converter.ConvertHTML(document, moptions, dataDir + "output.mht");
}

Please note that during testing, the API was able to generate a PDF from an URL. Nevertheless, we have logged an issue as HTMLNET-3330 in our issue management system to further investigate this scenario. We will look into its details and keep you posted with the status of ticket resolution. Please be patient and spare us some time.

We are sorry for the inconvenience.

Hello @asad.ali

Thank you so much for the reply. I tried your code and I did not get any exception but anyway, It did not get the image of google.com. I also tried the following code

document.Save(@“D:\Development\temp\aspose_test.mht”, Aspose.Html.Saving.HTMLSaveFormat.MHTML);

same result as your code. but html seems fine (unfortunately I need a single file).

By the way, the library is very very slow…

takes almost 1 minute => Aspose.Html.HTMLDocument(“https://www.google.com”))
takes more than 1 minute => ConvertHTML(document, moptions, dataDir + “output.mht”)

@kungfulee

Thanks for the feedback.

We have also tested the code under different environments and it executed fine. However, the URL “https://www.google.com” is not an ideal URL to test with because it keeps changing every time. Could you please try testing another URL (may be the one you want to generate MHTML from) and let us know about the results you notice in your environment. We will investigate the scenario accordingly.

Hello @asad.ali

Thank you for your reply. I tried with twitter and unfortunately it does not work at all. I have an exception…

here is my code

private static void TryWithAspose()
{
        try
        {
            using (var document = new Aspose.Html.HTMLDocument("https://twitter.com/elonmusk/status/1423830326665650179"))
            {   
                var options = new Aspose.Html.Saving.HTMLSaveOptions(); 
                document.Save(Path.Combine(@"D:\Development\Temp Projects\", "save-with-linked-file_out.html"), options);

                document.Save(@"D:\Development\Temp Projects\aspose_test.mht", Aspose.Html.Saving.HTMLSaveFormat.MHTML);

                var moptions = new Aspose.Html.Saving.MHTMLSaveOptions();
                Aspose.Html.Converters.Converter.ConvertHTML(document, moptions, @"D:\Development\Temp Projects\aspose_output.mht");

            }
        }
        catch (Exception e) { }
    }

I think that, the library has a lot of problem. I found another way to generate a mhtml file. Thank you anway

@kungfulee

We apologize for the disappointment faced. We have recorded your feedback along with the ticket and will surely work on improving the conversion engine of the API. We will also keep posting in the thread to update you about fixes in the API related to this scenario.

1 Like