Free Support Forum - aspose.com

Getting exception when converting HTML

Hi there,

We are using Aspose.HTML 20.3.0 to convert the attached html1.zip (297 Bytes) to PDF using the following code:

        HTMLDocument htmlDocument = new HTMLDocument(@"html1.html");
        
        var options = new Aspose.Html.Saving.PdfSaveOptions()
        {
            JpegQuality = 100
        };

        Aspose.Html.Converters.Converter.ConvertHTML(htmlDocument, options, @"html1.html.pdf");

We are getting the exception

Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
   at ╚?? ?? ╗(═    ╗, ═ ? ╚)
   at    .╗   ? ╗(String ╗, ═    ╚, Boolean ║)
   at    .╗(ICSSStyleDeclaration ╗, ║ ╚, ╗    ║)
   at    .   ╗, ICSSRule ╚, ICSSStyleDeclaration ║, ╗   , ICSSRuleList ═)
   at    .   ? ╗(String ╗)
   at ═ ?.╗(String ╗, Element ╚)
   at Aspose.Html.HTMLElement.get_Style()
   at ═ ?.╗(?? ╗, Element ╚, String ║)
   at ║??.??? ╗(Int32 ╗)
   at ??.╚(Int32 ╗)
   at ═ ?.╗(Element ╗, String ╚, Int32 ║)
   at ╚   .╗(Node ╗, Node ╚)
   at ╚   .╗(Document ╗)
   at ╚   .   ? ╗(Document ╗, RenderingOptions ╚)
   at       ? ╗(IDevice ╗,    ╚)
   at   .╗()
   at   .MoveNext()
   at ╚   .   ? ╗(Renderer ╗,   [] ╚, IDevice ║, TimeSpan)
   at Aspose.Html.Rendering.HtmlRenderer.Render(IDevice device, TimeSpan timeout, Document[] documents)
   at Aspose.Html.Rendering.Renderer`1.Render(IDevice device, TDocument document, TimeSpan timeout)
   at Aspose.Html.Rendering.Renderer`1.Render(IDevice device, TDocument document)
   at  ?.╗( ? ╗, IDevice ╚)
   at  ?.═ ?? ╗( ? ╗, PdfSaveOptions ╚, ICreateStreamProvider ║)
   at  ?.═ ?? ╗(HTMLDocument ╗, PdfSaveOptions ╚, String ║)
   at Aspose.Html.Converters.Converter.ConvertHTML(HTMLDocument document, PdfSaveOptions options, String outputPath)

mainly because the html inside is not well-formed.

Is there any way we could fix it prior to converting it? Or tweaking some PdfSaveOptions? Or any kind of workaround that would just not break the conversion process?

Taking a note here that we get the same exception if we use the following code to convert to PDF:

        input.Position = 0;

        var htmlLoadOptions = new HtmlLoadOptions(conversionOptions.ResourcesBasePath)
        {
            InputEncoding = "utf-8"
        };
        
        if (conversionOptions.Margins != null)
        {
            htmlLoadOptions.PageInfo =
                new PageInfo
                {
                    Margin = new MarginInfo
                    {
                        Left = conversionOptions.Margins.Left,
                        Right = conversionOptions.Margins.Right,
                        Top = conversionOptions.Margins.Top,
                        Bottom = conversionOptions.Margins.Bottom
                    }
                };
        }

        var document = new Document(input, htmlLoadOptions);

Thank you!

@gwert,

We have logged an investigation ticket as HTMLNET-2559 in our issue tracking system. We will further investigate the scenario in details and keep you posted with the status of ticket resolution. Please spare us little time.

We are sorry for the inconvenience.