Converting HTML to PDF - ObjectDisposedException

@usmanpop

Have you tried and tested it with 22.11 version? Please share a sample console application that can be used to reproduce the issue so that we can test the scenario in our environment and address it accordingly. Also, please share your complete environment details.

Yes the version is 22.11.

Testing your script in isolation works.

When printing my own 2 pages (front and back html) it works. When injecting your test page in between it does not work it comes up with the same exception message.

.NET 6.0
Razor ASPX project.

Save output Aspose is to the directory and that does work. Except when the exception happens.

@usmanpop

Would you please share a sample console application that shows the scenario in which issue is occurring at your end and reproduces the exception? We will use it to reproduce the error and address it accordingly.

1 Like

CodePDFGenAsposeHTML.zip (3.6 KB)

Attached is what we are doing to create a HTML to PDF.

To clarify. The first and back page do properly print out (convert to PDF). Nothing wrong with replacing the placeholders in HTML either. In the previous 2014 version of Aspose it was working fine.

Let us know what you can find.

Both the HTML files are loaded using index values please just assume everything prior to the call is correct (creation of the object and calling GeneratePDF).

Thanks very much

@usmanpop

We are checking it and will get back to you shortly.

1 Like

Any luck with this one?

@usmanpop

We tried to run the program that you shared but we could not execute it as it has undefined variables. Could you kindly provide a complete sample console application with sample values and document that we can run without any error and reproduce the exact issue in our environment. We apologize for the inconvenience being faced.

1 Like

Just replace the variables with data.

Create an instance of that class.
Remove the instance variable that requires Document list stuff.
Set the FillHTML values which require replacing to something hardcoded.
That’s it.

@usmanpop

The error is occurring due to closed stream of the image at your side. You are saving document that has an image stream in it as background and that stream should be closed after the document is saved. Please change the part of your code snippet as below to prevent the error:

using (FileStream stream = File.OpenRead(dataDir + "aspose_pdf-for-net.png"))
{
 foreach (Page page in doc.Pages)
 {
  stream.Seek(0, SeekOrigin.Begin);
  BackgroundArtifact bga = new BackgroundArtifact();
  bga.BackgroundImage = stream;
  bga.ArtifactVerticalAlignment = VerticalAlignment.Center;
  bga.ArtifactHorizontalAlignment = HorizontalAlignment.Center;
  page.Artifacts.Add(bga);

}
 doc.Save(dataDir + "outputDownload.pdf", SaveFormat.Pdf);
}
1 Like
        using (FileStream stream = File.OpenRead(Path.Combine(AssetsDirectory, "images", "pdf_background.png")))
        {
            foreach (Page page in doc.Pages)
            {
                stream.Seek(0, SeekOrigin.Begin);
                BackgroundArtifact bga = new BackgroundArtifact();
                bga.BackgroundImage = stream;
                bga.ArtifactVerticalAlignment = VerticalAlignment.Center;
                bga.ArtifactHorizontalAlignment = HorizontalAlignment.Center;
                page.Artifacts.Add(bga);

            }
            stream.Close();
        }

I have tried this and the result is the same. Same exception.

The using block actually handles the closure of the stream without you having to explicitly doing so.

I am not sure if what your saying makes sense. Since the other pages are printing fine.

You may be on to something. I have tried removing that entire block (commenting out). It works and breaks the pages automatically.

Let me know what you think I will try to find a solution.

@usmanpop

Here is the complete code snippet that we tried in our environment and did not notice any issues. Generated output is also attached for your kind reference:

private static void HTMLtoPDF(string dataDir)
        {
            Document doc = new Document();
            MarginInfo marginInfo = new MarginInfo(5, 10, 5, 7);
            doc.PageInfo.Margin = marginInfo;

            Page singlePage;
            string firstPage = "<b>First Page</b>";
            string lastPage = "<b>Last Page</b>";

            singlePage = doc.Pages.Add();
            AddPage(singlePage, firstPage);

            var pages = doc.Pages.Add();
            HtmlFragment htmlFragment = new HtmlFragment(BuildServiceChargeTableData(""));
            pages.Paragraphs.Add(htmlFragment);

            singlePage = doc.Pages.Add();
            AddPage(singlePage, lastPage);

            using (FileStream stream = File.OpenRead(dataDir + "aspose_pdf-for-net.png"))
            {
                foreach (Page page in doc.Pages)
                {
                    stream.Seek(0, SeekOrigin.Begin);
                    BackgroundArtifact bga = new BackgroundArtifact();
                    bga.BackgroundImage = stream;
                    bga.ArtifactVerticalAlignment = VerticalAlignment.Center;
                    bga.ArtifactHorizontalAlignment = HorizontalAlignment.Center;
                    page.Artifacts.Add(bga);

                }
                doc.Save(dataDir + "outputDownload.pdf", SaveFormat.Pdf);
            }
        }

        private static void AddPage(Page page, string template)
        {
            HtmlFragment mainHTML = new HtmlFragment(template);
            page.Paragraphs.Add(mainHTML);
        }

        private static string FillHTML(string html)
        {

            html = html.Replace("[{PROPERTYADDRESS}]", "Address");
            html = html.Replace("[{PROPERTYADDRESSLINE}]", "Address Line");
            html = html.Replace("[{ACCOUNTNUMBER}]", "Account Number");
            html = html.Replace("[{ACCOUNTTYPE}]", "Account Type");
            html = html.Replace("[{DATE}]", DateTime.Now.ToString("dd/MM/yyyy"));
            html = html.Replace("[{AMMOUNT}]", "0.0");
            html = html.Replace("[{DIRECTION}]", "credit/arrears");
            html = html.Replace("[{FULLNAME}]", "Aspose Pty Ltd");
            html = html.Replace("[{STARTDATE}]", DateTime.Now.ToString("dd/MM/yyyy"));
            html = html.Replace("[{ENDDATE}]", DateTime.Now.ToString("dd/MM/yyyy"));
            html = html.Replace("[{BALANCEBROUGHTFORWARD}]", "0.0");
            html = html.Replace("[{BALANCECARRIEDFORWARD}]", "0.0");
            return html;
        }

        private static string BuildServiceChargeTableData(string html)
        {

            string table = "<table cellspacing=\"0\" cellpadding=\"0\" style=\"width:440px;border-collapse: collapse;\">";

            string header = "<tr>";
            header += "<td style=\"width:85px;border: 1px solid black;padding: 0px 5px;\" ><strong>Transaction Date</strong></td>";
            header += "<td style=\"width:85px;border: 1px solid black;padding: 0px 5px;\"><strong>Transaction Number</strong></td>";
            header += "<td style=\"border: 1px solid black;padding: 0px 5px;\"><strong>Description</strong></td>";
            header += "<td style=\"width:85px;border: 1px solid black;padding: 0px 5px;\"><strong>Charges (£)</strong></td>";
            header += "<td style=\"width:65px;border: 1px solid black;padding: 0px 5px;\"><strong>Paid (£)</strong></td>";
            header += "<td style=\"width:85px;border: 1px solid black;padding: 0px 5px;\"><strong>Running Balance (£)</strong></td>";
            header += "</tr>";

            table += header;

            for (int i = 0; i < 200; i++)
            {
                string row = "<tr>";
                row += "<td style=\"width:85px;border: 1px solid black;padding: 0px 5px;font-size:11px;\" >" + i + "</td>";
                row += "<td style=\"width:85px;border: 1px solid black;padding: 0px 5px;font-size:11px;text-align:center;\">" + i + "</td>";
                row += "<td style=\"border: 1px solid black;padding: 0px  5px;font-size:11px;\">" + i + "</td>";
                row += "<td style=\"width:85px;border: 1px solid black;padding: 0px 5px;font-size:11px;text-align:right;\">" + i + "</td>";
                row += "<td style=\"width:65px;border: 1px solid black;padding: 0px 5px;font-size:11px;text-align:right;\">" + i + "</td>";
                row += "<td style=\"width:85px;border: 1px solid black;padding: 0px 5px;font-size:11px;text-align:right;\">" + i + "</td>";
                row += "</tr>";

                table += row;
            }
            table += "</table>";
            return table;
        }

outputDownload.pdf (365.5 KB)

1 Like

@usmanpop

Which .NET Framework Version are you using?

1 Like

NET 6.0

Your solution seems to be working fine.

I did not catch on to that but still seem dumbfounded because I thought we were accessing different streams. Did not see why one would have an issue with the other one.

This exact code was working in the previous version (Aspose) which was from 2014/2015. Of course a few things have changed as to the way the DOM works and so that was adjusted.

@usmanpop

The version in which you were using this approach previously is quite old. It had legacy Aspose.Pdf.Generator approach that had been obsolete and discontinued. The new DOM approach keeps the complete document and its resources i.e. images, pages, etc. in the memory until it is finally saved. The streams being used as resources need to stay open and accessible. That is why we suggested the code changes in order to prevent the issue. In case you still have any confusion or concerns, please feel free to share.

1 Like

Thank for that. Very helpful.