Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IText 7 Object reference not set to an instance of an object

Tags:

c#

itext

itext7

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?

like image 985
Trevor Daniel Avatar asked Nov 07 '25 10:11

Trevor Daniel


1 Answers

Try using default font provider

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


like image 65
Hammad Shabbir Avatar answered Nov 10 '25 01:11

Hammad Shabbir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!