T H
T H

Reputation: 395

Interacting with a .pdf file using IronPdf causes internal CLR error (0x80131506)

I am currently trying to interact with PDF files using a library called IronPdf. I created a simple Console Application and managed to follow their tutorial by creating a HTML file, writing to it, saving it as a PDF and accessing that PDF.

However whenever I try to further work with that pdf I always get an ExecutionEngineException with the following message:

Fatal error. Internal CLR error. (0x80131506)
 at IronPdf.Pdfium.NativeMethods.FPDFDOC_ExitFormFillEnvironment(IntPtr)
 at IronPdf.Pdfium.PdfFile.Dispose(Boolean)
 at IronPdf.Pdfium.PdfDocument.Dispose(Boolean)
 at IronPdf.Pdfium.PdfDocument.Dispose()
 at IronPdf.PdfDocument.lymdps(System.Collections.Generic.IEnumerable`1<Int32>)
 at IronPdf.PdfDocument.ExtractTextFromPages(Int32, Int32)
 at IronPdf.PdfDocument.ExtractAllText()
 at IronPdfDemo.Program.Main(System.String[])

This is my code:

using IronPdf;
...
    static void Main(string[] args)
        {
            //create new html file
            var htmlToPdf = new HtmlToPdf();
            htmlToPdf.PrintOptions.FirstPageNumber = 1;

            //add text to html and save as pdf
            htmlToPdf.RenderHtmlAsPdf("Hello World").SaveAs("html-string.pdf");

            //get pdf
            var pdf = PdfDocument.FromFile("html-string.pdf");

            //print out number of pages, should be 1
            Console.WriteLine(pdf.PageCount);

            //write text from pdf
            Console.WriteLine(pdf.ExtractAllText());
        }

The exception gets thrown on the last line, when I'm trying to extract text from the file. I have already tried to look for some clues online and it led me to this post however nothing seems to work.

My questions would be:

  1. Why does this error get thrown?
  2. How do I fix it?
  3. What does 0x80131506 stand for?

Upvotes: 1

Views: 2501

Answers (1)

Stephanie
Stephanie

Reputation: 610

0x80131506 is Fatal Internal CLR error normally stemming from Interop and is not try/catchable.

The bug is caused by the use of an experimental .NET runtime .NET 5.07 and above currently have a known issue / breaking change with Interop.

.NET 5 latest does not have LTS Long Term Support and is a balance of bleeding edge technology over stability.

2 ways to fix it:

  1. IronPdf now have a fix on their main branch: https://www.nuget.org/packages/IronPdf/

  2. Target a more stable .NET run time ( .Net Core 5.0 or 3.1 LTS) .NET 6 will be an LTS. Until then .Net 3.1 has long term support and is stable. https://blog.inedo.com/dotnet/demystifying-lts

Upvotes: 1

Related Questions