Converting HTML to PDF - ObjectDisposedException

Using the latest Aspose package.

I have a HTML template in which I am trying to convert to a PDF.

Printing the code without the table and rows is fine.

Its when we are printing the table and rows for some data that has a problem, we end up with ObjectDisposedException.

In the 2014 version of Aspose this table was printing fine.

The bit of code is as follows:

    template = "c:\templates\mainpage.html";
    Page section = doc.Pages.Add();
    AddSection(section, template);

    private void AddSection(Page section, string template)
    {
        StreamReader r = File.OpenText(TemplateDirectory + template);
        string html = r.ReadToEnd();
        string htmlText = FillHTML(html);
        HtmlLoadOptions htmlload = new HtmlLoadOptions();
        HtmlFragment mainHTML = new HtmlFragment(htmlText);
        section.Paragraphs.Add(mainHTML);
    }

We are trying to load transactions which come out onto the PDF through HTML text and replace.

I have not shown the full code here.

@usmanpop

Can you please share your sample HTML template in .zip format as well? We will test the scenario in our environment and address it accordingly.

This is the HTML in which we are inserting data into the elements and it fails here. I have tested it and so it seems to fail when it reaches the end of page in the PDF. I thought it should create pages automatically if the content is long.

So fillHTML is bit is nothing but replacing these placeholders with variables which we have labelled in HTML and then the fillHTML calls this code below which also does the same thing based on a list model which contains simple data (numbers, text):

    private string BuildServiceChargeTableData()
    {
        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;

        foreach (Transaction.Display d in Document.Transactions)
        {
            string row = "<tr>";
            row += "<td style=\"width:85px;border: 1px solid black;padding: 0px 5px;font-size:11px;\" >" + d.DateTime.ToString("dd MMM yy").ToUpper() + "</td>";
            row += "<td style=\"width:85px;border: 1px solid black;padding: 0px 5px;font-size:11px;text-align:center;\">" + d.Number + "</td>";
            row += "<td style=\"border: 1px solid black;padding: 0px  5px;font-size:11px;\">" + d.Description + "</td>";
            row += "<td style=\"width:85px;border: 1px solid black;padding: 0px 5px;font-size:11px;text-align:right;\">" + ((d.Charges != 0) ? d.Charges.ToString("0.00") : "") + "</td>";
            row += "<td style=\"width:65px;border: 1px solid black;padding: 0px 5px;font-size:11px;text-align:right;\">" + ((d.Payments != 0) ? d.Payments.ToString("0.00") : "") + "</td>";
            row += "<td style=\"width:85px;border: 1px solid black;padding: 0px 5px;font-size:11px;text-align:right;\">" + ((d.Balance != 0) ? d.Balance.ToString("0.00") : "") + "</td>";
            row += "</tr>";

            table += row;
        }


        return table;
    }

Test where there is a list of 200 plus elements in the array.

@usmanpop

Please check the below complete code snippet that we used and tested the case with 22.11 version of the API and did not notice any issues:

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;
}

var pdfDocument = new Document();
var page = pdfDocument.Pages.Add();
HtmlFragment htmlFragment = new HtmlFragment(table);
page.Paragraphs.Add(htmlFragment);
pdfDocument.Save(dataDir + @"html_test.PDF");

html_test.PDF (270.2 KB)

Can you please try to use the latest version of the API and let us know in case you still notice any issues.

Hi thanks what are you doing differently.

Seems like I am doing the same thing. I have not run your code but literally all is the same. I expect it to break page automatically. I have tried to write to new pages by myself but trying to avoid this.

@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.