Converting HTML to PDF errors after upgrading from .NET 6 to .NET 8 or .NET 9

Hi, please help, we are using Aspose.PDF for HTML to PDF conversions and encountering a blocking error when upgrading to .NET 8 or .NET 9, I have tried various versions of Aspose.PDF and the latest stable version 25.2.0. Here is the code that I’ve isolated into a c# unit test:

     [Fact]
    public void Convert_html_to_pdf_2()
    {
        // Arrange
        var html = @"<html xmlns=""http://www.w3.org/1999/xhtml"">
Stock
";
        // Act
        var input = new MemoryStream(Encoding.UTF8.GetBytes(html));
        var document = new Document(input, new HtmlLoadOptions());

        var memoryStream = new MemoryStream();
        document.Save(memoryStream);
        var actualPdfBytes = memoryStream.GetBuffer();
        Array.Resize(ref actualPdfBytes, (int)memoryStream.Length);

        // Assert
    }

The error is as follows:

Message:
System.InvalidOperationException : Enumeration already finished.

Stack Trace:
SZGenericArrayEnumerator1.get_Current() #=zC4yhBn3BqnQVChWlWuWNI4DFpUuURiwCgg==.#=zlA$DyJrO6UuY[T](IEnumerable1 #=zT9mrLC0=)
#=zmozd3_4zqqTvo$lqQEkp9cE=.#=zkZTPIm8PbnyurcrMIIiR7U4=(#=zUC9n57FvIxd1Eh9jSJJra77YOXHWl4OmLw== #=zN1KS4XoesKNh)
#=zBryqA2LICvawk62h_x_poVc=.#=zplDeB4clgyInMQwTlQkVh5g=(#=zQ7Q8eeBLfKm4pIi$snx962ceHg9m1fd0nw== #=zgYFraDw=)
SelectManySingleSelectorIterator2.MoveNext() SelectManySingleSelectorIterator2.MoveNext()
SelectManySingleSelectorIterator2.MoveNext() ConcatIterator1.MoveNext()
SelectManySingleSelectorIterator2.MoveNext() IteratorSelectIterator2.MoveNext()
<11 more frames…>
#=zejvkYdH9Dk5Ufbi25DsIdPPxW0Pu.Render(#=zsAHVwSk6metEmZCZ8xcWcyBX0OTb #=zP9SjuMo=, #=zSevFJJE= #=z2XFwSls=, TimeSpan #=zyEe2MVY=)
#=zejvkYdH9Dk5Ufbi25DsIdPPxW0Pu.Render(#=zsAHVwSk6metEmZCZ8xcWcyBX0OTb #=zP9SjuMo=, #=zSevFJJE= #=z2XFwSls=)
#=ztr1iySQAPSE3wwXTaDP8XIY9ibzr.#=z2C8rXfN98FTy(Stream #=z$G$Uv7mc4slX, Document #=z6X5TU2TE48IL, HtmlLoadOptions #=z8XWl92SxULqa, String #=z0lC$unjcSOyy)
#=ztr1iySQAPSE3wwXTaDP8XIY9ibzr.#=znlwNvPM=(Stream #=z$G$Uv7mc4slX, Document #=z6X5TU2TE48IL, HtmlLoadOptions #=z8XWl92SxULqa, String #=z0lC$unjcSOyy)
#=ztr1iySQAPSE3wwXTaDP8XIY9ibzr.#=znlwNvPM=(Stream #=z$G$Uv7mc4slX, Document #=z6X5TU2TE48IL, HtmlLoadOptions #=z8XWl92SxULqa)
Document.#=z_IIvKXU=(Stream #=z$A1ps7Y=, LoadOptions #=ze_om$e0=)
Document.ctor(Stream input, LoadOptions options)
AsposePdfGeneratorTests.Convert_html_to_pdf_2() line 65
RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)

Is this a known issues and/or is there any workaround that can be suggested because this is unfortuntely blocking us from upgrading our version of .NET

Thanks

ALSO NOTE: The following unit test passes and so it seems related to the handling of preserving whitespace:

    [Fact]
    public void Convert_html_to_pdf_1()
    {
        // Arrange
        var html = @"<html xmlns=""http://www.w3.org/1999/xhtml"">
Stock
";
        // Act
        var input = new MemoryStream(Encoding.UTF8.GetBytes(html));
        var document = new Document(input, new HtmlLoadOptions());

        var memoryStream = new MemoryStream();
        document.Save(memoryStream);
        var actualPdfBytes = memoryStream.GetBuffer();
        Array.Resize(ref actualPdfBytes, (int)memoryStream.Length);

        // Assert
    }

@keithwheeler

It seems you are encountering an InvalidOperationException with the message “Enumeration already finished” when trying to convert HTML to PDF after upgrading to .NET 8 or .NET 9. This issue appears to be related to how whitespace is handled in your HTML content, as indicated by the fact that a similar test passes when the whitespace is not preserved.

Here are a few suggestions to troubleshoot and potentially resolve the issue:

  1. Check for Updates: Ensure that you are using the latest version of Aspose.PDF. Sometimes, issues are resolved in newer releases, so it’s worth checking if a more recent version than 25.2.0 is available.

  2. Modify HTML Content: Since the error seems to be related to whitespace handling, try simplifying your HTML or adjusting the CSS styles. For example, you could remove the white-space: pre-wrap; style and see if that resolves the issue.

  3. Use HtmlLoadOptions: You can customize the HtmlLoadOptions to control how the HTML is processed. For instance, you might want to set properties that affect how resources are loaded or how the document is rendered.

  4. Test with Different HTML: Create a minimal HTML document that isolates the issue. This can help determine if the problem is with specific HTML content or a broader issue with the library.

  5. Review Documentation: Check the Aspose documentation for any notes on compatibility with .NET 8 or .NET 9, especially regarding HTML to PDF conversion. There may be specific changes or requirements introduced in these versions.

If the problem persists, consider reaching out to Aspose support with detailed information about your environment and the issue. They may have more insights or workarounds specific to the version you are using.

If you have any further questions or need additional assistance, feel free to ask!

The previous code snippet was ‘inlined’ as HTML so here is a screenshot of the code that produces the failure in .NET 8 and .NET9 (but worksd in .NET 6):

Screenshot 2025-02-12 152624.png (38.4 KB)

@keithwheeler

Instead of Aspose.PDF, would you please try using Aspose.Pdf.Drawing for .NET and if issue still persists, please let us know. We will further proceed accordingly.

We have recently upgraded our service to .Net 9 and have just come across this issue in testing. Is there a support ticket for this?

We use ‘white-space: pre-line;’ in various places in our templates and removing this affects our styling

@matthew.house

Have you tried the above suggestion? No, there is no ticket generated for this particular case. If possible, can you please share your sample HTML with us with the screenshot of the issue so that we can generate an issue in our issue tracking system and share the ID with you.