We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Stackoverflowexception when converting html to pdf


Our production service is having an issue converting certain html documents to pdf and is throwing a StackOverflowException. This exception can not be caught and is shutting down the whole service without warning or proper exception handling.

I am using the following snippet:

Aspose.Pdf.Generator.Pdf pdf = new Aspose.Pdf.Generator.Pdf();

// add the section to PDF document sections collection
Aspose.Pdf.Generator.Section section = pdf.Sections.Add();

// Read the contents of HTML file into StreamReader object
using (StreamReader r = File.OpenText(srcFile))
//Create text paragraphs containing HTML text
Aspose.Pdf.Generator.Text text2 = new Aspose.Pdf.Generator.Text(section, r.ReadToEnd());
// enable the property to display HTML contents within their own formatting
text2.IsHtmlTagSupported = true;
//Add the text paragraphs containing HTML text to the section
//Save the pdf document and causes the StackOverflowException
pdf.Save(Path.ChangeExtension(destFile, “.pdf”));

I attached the problem html document. As you can see it contains a lot of scripting and the pdf document it refers to is not present. Not properly converting this document is not the issue but it should not crash the whole service. Is there a way to prevent this error from occurring or is there something I can check inside the html to prevent it from being processed.

Best Regards

Hi Johan,

Thanks for your inquiry. I have tested the scenario using new DOM approach for HTML to PDF conversion and unable to notice any issue. Please use new DOM it will resolve the issue.

Moreover, please note it is recommended to use new DOM(Aspose.Pdf) as it is more improved and efficient than old(Aspose.Pdf.Generator). It can create a PDF from scratch and manipulate existing PDF as well.

Please feel free to contact us for any further assistance.

Best Regards,