Converting HTML to PDF - ObjectDisposedException

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.