iText7 convert HTML to PDF "System.NullReferenceException."
Asked Answered
W

3

8

OLD TITLE: iTextSharp convert HTML to PDF "The document has no pages."

I am using iTextSharp and xmlworker to convert html from a view to PDF in ASP.NET Core 2.1

I tried many code snippets I found online but all generate an exception:

The document has no pages.

Here is my current code:

public static byte[] ToPdf(string html)
{
    byte[] output;
    using (var document = new Document())
    {
        using (var workStream = new MemoryStream())
        {
            PdfWriter writer = PdfWriter.GetInstance(document, workStream);
            writer.CloseStream = false;
            document.Open();
            using (var reader = new StringReader(html))
            {
               XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, reader);
               document.Close();
               output = workStream.ToArray();
            }
        }
   }
   return output;
}

UPDATE 1

Thanks to @Bruno Lowagie's advice, I upgraded to iText7 and pdfHTML, but I can't find much tutorials about it.

I tried this code:

public static byte[] ToPdf(string html)
{
      html = "<html><head><title>Extremely Basic Title</title></head><body>Extremely Basic Content</body></html>";
    
      byte[] output;
    
      using (var workStream = new MemoryStream())
      using (var pdfWriter = new PdfWriter(workStream))
      {
           using (var document = HtmlConverter.ConvertToDocument(html, pdfWriter))
           {
                //Passes the document to a delegated function to perform some content, margin or page size manipulation
                //pdfModifier(document);
           }
    
           //Returns the written-to MemoryStream containing the PDF.   
           return workStream.ToArray();
      }
}

but I get

System.NullReferenceException

when I call HtmlConverter.ConvertToDocument(html, pdfWriter)

Am I missing something?


UPDATE 2

I tried to debug using source code.

This is the stack trace

System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=itext.io
StackTrace: at iText.IO.Font.FontCache..cctor() in S:\Progetti\*****\itext7-dotnet-develop\itext\itext.io\itext\io\font\FontCache.cs:line 76

This is the code that generates the exception:

static FontCache() 
{
    try 
    {
        LoadRegistry();
        foreach (String font in registryNames.Get(FONTS_PROP)) 
        {
            allCidFonts.Put(font, ReadFontProperties(font));
        }
    }
    catch (Exception) { }
}    
registryNames count = 0 and .Get(FONTS_PROP) throws the exception

UPDATE 3

The problem was related to some sort of cache. I can't really understand what, but as you can see in the code the exception was generated when it tried to load fonts from cache.
I realized that, after having tried the same code on a new project where it worked.

So I cleaned the solution, deleted bin, obj, .vs, killed IIS Express, removed and reinstalled all nuget packages then run again, magically it worked.

Then I had to make only one fix to the code:
Instead of HtmlConverter.ConvertToDocument that generates only a 15 bytes document I used HtmlConverter.ConvertToPdf to generate a full PDF.

Here is the complete code:

public static byte[] ToPdf(string html)
{
    using (var workStream = new MemoryStream())
    {
        using (var pdfWriter = new PdfWriter(workStream))
        {                    
            HtmlConverter.ConvertToPdf(html, pdfWriter);
            return workStream.ToArray();
        }
    }
}
Wendell answered 24/7, 2018 at 16:54 Comment(3)
Did you read this question and answer on Stack Overflow: Converting HTML to PDF with iText? The name "iTextSharp" was changed into "iText for .NET" two years ago, when we released iText 7 to replace iText 5. You should throw away your code and start anew with iText 7 for .NET and the pdfHTML add-on. Do not expect much help on the old iText 5 and XML Worker.Armada
Please include the stack trace.Indigenous
Maybe because of your .NET Core version? iText supports .NET Standard 1.6, which is .NET Core 1.0.Tasker
I
13

I had this EXACT same problem, and after digging down all the way to iText7's FontCache object and getting an error when trying to create my OWN FontProgram to use from a raw TTF file (which also failed with the same null reference error), I finally "solved" my problem.

Apparently iText has some internal errors/exceptions that they are just sort of "skipping" and "pushing past", because I realized by accident that I had "Enable Just My Code" in Visual Studios disabled, and so my system was trying to debug iText7's code as well as mine. The moment that I re-enabled it in my Visual Studio settings (Tools > Options > Debugging > General > Enable Just My Code checkbox), the problem magically went away.

Settings in Visual Studio

So I spent four hours trying to troubleshoot a problem that was in THEIR code, but that they apparently found some way to work around and push through the method anyways even on a null reference failure.

My convert to PDF function is now working just fine.

Intuitionism answered 26/5, 2021 at 15:53 Comment(2)
This is the correct workaround. I developed console app before 8 months and it suddenly stopped working and throwing similar exception while reading PDF. But strangely this fixes the problem - I wonder what is happening and how some settings in VS affects our code developed using iTextCore! Thanks for the workaround else I might spend hours to find a work around and may be switch to another paid PDF reader library :)Strepphon
Thanks! This worked for me, using ItextSharp 5.5.13.3!Overblown
M
0

I was getting this error as well, but noticed it was only on the first attempted load of the SvgConverter. So I added this at the top of my class, and it seems to have fixed hidden the bug.

using iText.Kernel.Pdf;
using iText.IO.Font;
public class PdfBuilder {

    static PdfBuilder() {
        try {
            FontCache.GetRegistryNames();
        }
        catch(Exception) {
            // ignored... this forces the FontCache to initialize
        }
    }
    ...
}
Mojave answered 7/12, 2021 at 15:57 Comment(0)
G
0

I was using itext 7 everything works fine in Console application. When I use same code in Web/Function App project, I started getting below error.

System.NullReferenceException
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=itext.html2pdf
  StackTrace:
   at iText.Html2pdf.Attach.Impl.Tags.BrTagWorker..ctor(IElementNode element, ProcessorContext context)
   at iText.Html2pdf.Attach.Impl.DefaultTagWorkerMapping.<>c.<.cctor>b__1_10(IElementNode lhs, ProcessorContext rhs)
   at iText.Html2pdf.Attach.Impl.DefaultHtmlProcessor.Visit(INode node)
   at iText.Html2pdf.Attach.Impl.DefaultHtmlProcessor.Visit(INode node)
   at iText.Html2pdf.Attach.Impl.DefaultHtmlProcessor.Visit(INode node)
   at iText.Html2pdf.Attach.Impl.DefaultHtmlProcessor.Visit(INode node)
   at iText.Html2pdf.Attach.Impl.DefaultHtmlProcessor.Visit(INode node)
   at iText.Html2pdf.Attach.Impl.DefaultHtmlProcessor.Visit(INode node)
   at iText.Html2pdf.Attach.Impl.DefaultHtmlProcessor.Visit(INode node)
   at iText.Html2pdf.Attach.Impl.DefaultHtmlProcessor.ProcessDocument(INode root, PdfDocument pdfDocument)
   at iText.Html2pdf.HtmlConverter.ConvertToPdf(String html, PdfDocument pdfDocument, ConverterProperties converterProperties)
   at iTextSample.ConsoleApp.HtmlToPdfBuilder.RenderPdf() in C:\code\iTextSample.ConsoleApp\HtmlToPdfBuilder.cs:line 227

After some investigation found that <br /> tag was a problem. I removed all <br /> tags and it is working fine.

Guelders answered 19/1, 2023 at 17:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.