Aspose HTML to PDF - Memory leak during conversion

I have an image (1920x1080, 75KB) inside a very small html file. I load the html binary data into a memorystream and use the same stream to load the document, at this step aspose gets stuck and the memory leak starts.

using (Stream htmlStream = new MemoryStream(html))
                using (var document = new Document(htmlStream, _pdfLoadOptions)) <-- stuck here
                        document.Save(pdfFilePath, SaveFormat.Pdf);
...

Here is the image:

@criaraci

Would you please share your sample HTML file with us as well in a .zip format. We will test the scenario in our environment and address it accordingly.

Here is my html file. I’m using Aspose.PDF v21.1.0
19835436.zip (330 Bytes)

I just realized the image is originally of type jfif but has been renamed to .jpg. Aspose should be able to handle even .jfif right?

@criaraci

We used below code snippet to test the scenario with Aspose.PDF for .NET 21.1 and did not face any issue. For your kind reference, an output PDF is also attached:

HtmlLoadOptions options = new HtmlLoadOptions(dataDir);
var htmlBody = File.ReadAllText(dataDir + "19835436.html");
var htmlByteArray = Encoding.UTF8.GetBytes(htmlBody);
using (var stream = new MemoryStream(htmlByteArray))
{
 using (Document pdfDocument = new Document(stream, options))
 {
  pdfDocument.Save(dataDir + "output.pdf");
 }
}

output.pdf (325.2 KB)

Would you please make sure to use latest version of the API. In case issue still persists, please share a sample console application with us which is able to reproduce the issue. We will again test the scenario in our environment and address it accordingly.

Hello again, I updated my Aspose.PDF version to 21.2.0 and used this code, but the problem persists:

var htmlBody = File.ReadAllText(basePath + "19835436.html");
        var htmlByteArray = Encoding.UTF8.GetBytes(htmlBody);
        var options = new HtmlLoadOptions()
        {
            PageInfo =
            {
                Margin = new MarginInfo(60, 30, 60, 30)
            },
            InputEncoding = "UTF-8",
        };

        using (var stream = new MemoryStream(htmlByteArray))
        {
            using (Document pdfDocument = new Document(stream, options))
            {
                pdfDocument.Save(basePath + "output.pdf");
            }
        }

Weirdly enough, if I try without my margin settings it works, what am I doing wrong?

@criaraci

We were also able to notice the issue of memory leak when margins were specified. Therefore, have logged it as PDFNET-49412 in our issue tracking system. We will further look into its details and keep you posted with the status of its correction. Please be patient and spare us some time.

We apologize for the inconvenience.

PS: Could you please share the exception and complete stack trace information you are experiencing at your end?

The exception I get is a System.OutOfMemoryException.
Here is the stack trace:

at #=zyCVMO9JyxOOv4g0NM7DLPIfri0kV.#=zjHW0ikcutF89()
at #=z12ScnGNcUv0BvDpq$YTfDB_94JKSxk6vig==.#=zFzqU$MD0Grut(#=zc7ChcNr5qq2knk$DROia_q0= #=z1RIQoE4=, Int32 #=zQ8x$_ts=, #=zRH8f$IrX5U5FK$jd4WhwB4N5vBBcJE1cPQ== #=zbDBwKlc=, #=zdfWYS_xL1v2nHkabCRYK3MDVYatH9Zajsw== #=zj8htCwxbP9oC, #=z5$N8Sv28EY$9xcU9V3CG0eBLWEln #=zEKblAOo=)
at #=z12ScnGNcUv0BvDpq$YTfDB_94JKSxk6vig==.#=zUgBo49uZZYbZsJtZIzx6BRQ=.MoveNext()
at System.Collections.Generic.List1..ctor(IEnumerable1 collection)
at #=zC1aAsKYLmHBzqdNWU7EfDemEo4g8oHyGi3nBLlUmLISq.#=zzYQPG8o=[TSource](IEnumerable`1 #=zmRI35W0=)
at #=zBigGGKyL$kceNUWwzXLBZX1sCovtVW9TxA==.#=znAvGDMg=(#=zc7ChcNr5qq2knk$DROia_q0= #=z1RIQoE4=, #=zRH8f$IrX5U5FK$jd4WhwB4N5vBBcJE1cPQ== #=zbDBwKlc=, #=zdfWYS_xL1v2nHkabCRYK3MDVYatH9Zajsw== #=zT5C0E2a2y9jK, #=z_iTjA2qzRln1bNtsG0$dijZ_uGS$0Gu9GA== #=zzwFWpfEBgk7t, #=z5$N8Sv28EY$9xcU9V3CG0eBLWEln #=zEKblAOo=)
at #=zBigGGKyL$kceNUWwzXLBZX1sCovtVW9TxA==.#=zArmKTvSu7IOz(#=zc7ChcNr5qq2knk$DROia_q0= #=z1RIQoE4=, #=z5$N8Sv28EY$9xcU9V3CG0eBLWEln #=zjVM57wrvnHRWy15m3w==, #=zmeOKY8S4b6mC5AzmuGYZ83pu4b7N #=z81xa1bg=, #=zHnmfZckIarXWaG5mbctTspBdzG4Y #=zQwjJiYQ=)
at #=zBigGGKyL$kceNUWwzXLBZX1sCovtVW9TxA==.#=znAvGDMg=(#=zmeOKY8S4b6mC5AzmuGYZ83pu4b7N #=z81xa1bg=, #=zHnmfZckIarXWaG5mbctTspBdzG4Y #=zQwjJiYQ=)
at #=zHnmfZckIarXWaG5mbctTspBdzG4Y.#=znAvGDMg=(#=zmeOKY8S4b6mC5AzmuGYZ83pu4b7N #=z81xa1bg=)
at #=zUYwh4gGHnjz6WupSC9lPFvImYlLR.#=zJJnXVrY=()
at #=zUYwh4gGHnjz6WupSC9lPFvImYlLR.MoveNext()
at #=zoVOoX1_k_yoy$SdgRVCKefw6Vto8RaH2Og==.#=zY$$xYF_SSP7m(#=zWjyYcbN3gm$HAXty1vkAHCY= #=zyU_TeyOQj1zdqV$79Q==, #=zHnmfZckIarXWaG5mbctTspBdzG4Y[] #=zBl59zZo_eCZX, #=zmeOKY8S4b6mC5AzmuGYZ83pu4b7N #=z81xa1bg=, TimeSpan #=znzCQHig=)
at #=zkr3qXh2Cy0FHootLU90Xc7Q0Bqot.Render(#=zmeOKY8S4b6mC5AzmuGYZ83pu4b7N #=z81xa1bg=, TimeSpan #=znzCQHig=, #=z_oEE_G2TrJ_VCJxf0ax06Z8=[] #=zZKPE8oQ=, #=zc7ChcNr5qq2knk$DROia_q0=[] #=z_9e5oCg=)
at #=zkr3qXh2Cy0FHootLU90Xc7Q0Bqot.Render(#=zmeOKY8S4b6mC5AzmuGYZ83pu4b7N #=z81xa1bg=, TimeSpan #=znzCQHig=, #=zc7ChcNr5qq2knk$DROia_q0=[] #=z_9e5oCg=)
at #=zydOR_DOVqqJq2mMcsn9UWjrjQSma.Render(#=zmeOKY8S4b6mC5AzmuGYZ83pu4b7N #=z81xa1bg=, #=z1GMa$Pg= #=z1RIQoE4=, TimeSpan #=znzCQHig=)
at #=zydOR_DOVqqJq2mMcsn9UWjrjQSma.Render(#=zmeOKY8S4b6mC5AzmuGYZ83pu4b7N #=z81xa1bg=, #=z1GMa$Pg= #=z1RIQoE4=)
at #=zmWICyq19WIuvM9i6ePwd9T77qR6q.#=zf$tdttQ=(Stream #=zbn5rTmdc18TE, Document #=zKj_lKgLX5mp2, HtmlLoadOptions #=zjqkcKb_AEEKT, String #=ziBEDtdP$YmOR)
at #=zmWICyq19WIuvM9i6ePwd9T77qR6q.#=zf$tdttQ=(Stream #=zbn5rTmdc18TE, Document #=zKj_lKgLX5mp2, HtmlLoadOptions #=zjqkcKb_AEEKT)
at Aspose.Pdf.Document.#=zNLf_q1I=(Stream #=zgnMJO48=, LoadOptions #=zEKblAOo=)
at Aspose.Pdf.Document…ctor(Stream input, LoadOptions options)
at ConsoleApp2.Program.Main(String[] args) in C:\Users******\source\repos\ConsoleApp2\ConsoleApp2\Program.cs:line 38

This is line 38:

using (Document pdfDocument = new Document(stream, options))

@criaraci

Thanks for sharing the details.

We have updated the ticket information accordingly and will inform you as soon as it is resolved. Please give us some time.

We are sorry for the inconvenience.

Thanks @asad.ali!

1 Like

Hello @asad.ali,

I have another issue but it seems to be connected to this one. I have this document which gives me errors when I try to load it into a document object. But if I set Margin in loadOptions the document gets loaded correctly.

My code:

var htmlByteArray = Encoding.UTF8.GetBytes(htmlBody);
        var options = new HtmlLoadOptions()
        {
            //PageInfo =
            //{
            //    Margin = new MarginInfo(60, 30, 60, 30)
            //}
        };

        using (var stream = new MemoryStream(htmlByteArray))
        {
            using (Document pdfDocument = new Document(stream, options))
            {                  
                pdfDocument.Save(basePath + "output.pdf");
            }
        }

The error I get is this:

Index was outside the bounds of the array.
at #=zPStLM8uNbp5r2jVO6Uu2DG$uLD4e.#=zv8B1vsOT6z44AvJZdqb0yvU=.#=zfTJiC2Qk$f7wcZ_7$_YI5F5TPck9(Tuple2[][] #=zAoltCYU=, Tuple2[] #=zvtR2TXW9ED0I, Boolean #=zEPyjCwA=)
at #=zPStLM8uNbp5r2jVO6Uu2DG$uLD4e.#=zv8B1vsOT6z44AvJZdqb0yvU=.#=zb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cbN3gm$HAXty1vkAHCY= #=zyU_TeyOQj1zdqV$79Q==, #=zHnmfZckIarXWaG5mbctTspBdzG4Y[] #=zBl59zZo_eCZX, #=zmeOKY8S4b6mC5AzmuGYZ83pu4b7N #=z81xa1bg=, TimeSpan #=znzCQHig=)
at #=zkr3qXh2Cy0FHootLU90Xc7Q0Bqot.Render(#=zmeOKY8S4b6mC5AzmuGYZ83pu4b7N #=z81xa1bg=, TimeSpan #=znzCQHig=, #=z_oEE_G2TrJ_VCJxf0ax06Z8=[] #=zZKPE8oQ=, #=zc7ChcNr5qq2knk$DROia_q0=[] #=z_9e5oCg=)
at #=zkr3qXh2Cy0FHootLU90Xc7Q0Bqot.Render(#=zmeOKY8S4b6mC5AzmuGYZ83pu4b7N #=z81xa1bg=, TimeSpan #=znzCQHig=, #=zc7ChcNr5qq2knk$DROia_q0=[] #=z_9e5oCg=)
at #=zydOR_DOVqqJq2mMcsn9UWjrjQSma.Render(#=zmeOKY8S4b6mC5AzmuGYZ83pu4b7N #=z81xa1bg=, #=z1GMa$Pg= #=z1RIQoE4=, TimeSpan #=znzCQHig=)
at #=zydOR_DOVqqJq2mMcsn9UWjrjQSma.Render(#=zmeOKY8S4b6mC5AzmuGYZ83pu4b7N #=z81xa1bg=, #=z1GMa$Pg= #=z1RIQoE4=)
at #=zmWICyq19WIuvM9i6ePwd9T77qR6q.#=zf$tdttQ=(Stream #=zbn5rTmdc18TE, Document #=zKj_lKgLX5mp2, HtmlLoadOptions #=zjqkcKb_AEEKT, String #=ziBEDtdP$YmOR)
at #=zmWICyq19WIuvM9i6ePwd9T77qR6q.#=zf$tdttQ=(Stream #=zbn5rTmdc18TE, Document #=zKj_lKgLX5mp2, HtmlLoadOptions #=zjqkcKb_AEEKT)
at Aspose.Pdf.Document.#=zNLf_q1I=(Stream #=zgnMJO48=, LoadOptions #=zEKblAOo=)
at Aspose.Pdf.Document…ctor(Stream input, LoadOptions options)
at ConsoleApp2.Program.Main(String[] args) in C:\Users******\source\repos\ConsoleApp2\ConsoleApp2\Program.cs:line 37

@criaraci

Could you please share the sample source file as well so that we can test the scenario in our environment and address it accordingly.

Hello again, thanks for the help. Here are the files:
Html files.zip (105.3 KB)

@criaraci

We have logged following issues in our issue tracking system for the sake of investigation:

  • PDFNET-49566 - IndexOutOfRangeException
  • PDFNET-49567 - ArgumentOutOfRangeException

We will further look into its details and keep you posted with the status of their correction. Please be patient and spare us some time.

We are sorry for the inconvenience.

The issues you have found earlier (filed as PDFNET-49567) have been fixed in Aspose.PDF for .NET 22.3.

The issues you have found earlier (filed as PDFNET-49412) have been fixed in Aspose.PDF for .NET 22.5.

The issues you have found earlier (filed as PDFNET-49566) have been fixed in Aspose.PDF for .NET 22.7.