3

I need to convert some html to a PDF. I came across IText7 and it looks a good solution.

I am developing in .net core 3.1

But, even using a basic implementation of it I am getting an error:

string basicPage = "<html><body><h1>My First Heading</h1><p>My first paragraph.</p></body></html>";

using (FileStream pdfDest = File.Open(@"C:\Users\Trevo\Desktop\output.pdf", FileMode.OpenOrCreate))
{
    ConverterProperties converterProperties = new ConverterProperties();
    HtmlConverter.ConvertToPdf(basicPage, pdfDest, converterProperties);
}

The error is everyone's favourite of:

Object reference not set to an instance of an object.

The inner exception is null and the only clue is the SerializationStackTraceString is:

at iText.IO.Font.FontCache..cctor()

So, it's pointing towards something to do with font?

The only suggestion I have found was to clean various directories and caches. I have created a clean solution from scratch and the same error occurs.

Can anyone offer any help please?

Trevor Daniel
  • 3,785
  • 12
  • 53
  • 89
  • 1
    This isn't a resolution. More of a troubleshooting tip. iText may be looking to resolve an assembly that is not there. If you subscribe to the `AppDomain.CurrentDomain.AssemblyResolve` event, you can see if the AppDomain is trying to resolve (look for) an assembly that it cannot find. Here is the link: https://learn.microsoft.com/en-us/dotnet/api/system.appdomain.assemblyresolve?view=netcore-3.1 – Glenn Ferrie Jan 14 '21 at 14:49

2 Answers2

3

Try using default font provider

                var converterProperties = new ConverterProperties();
                converterProperties.SetFontProvider(new DefaultFontProvider(true, true, true));


Hammad Shabbir
  • 722
  • 1
  • 6
  • 13
3

Please see this:

Null Reference Exception when calling iText7 PdfAcroForm.GetAcroForm() in .Net Core 3.1 class library

And this:

iText7 convert HTML to PDF "System.NullReferenceException."

I would like to be able to flag any of them as duplicates, because they are all the same issue - internal issues with iText7's own code that we essentially have to ignore. However, I'm unable to flag them as duplicates because that functionality will only work if one of the answers is accepted.

But I just wanted you to know, it's not really your problem, it's theirs, so until they fix it, make sure you have "Just My Code" enabled in your settings.

Westley Bennett
  • 554
  • 4
  • 19