0

I am trying to convert an HTML document to PDF using iTextSharpLGPLv2.Core nuget library.

Problem i am facing is that HTMLWorker does not support in iTextSharper anymore.

Below is my code.

StringReader sr = new StringReader(sb.ToString());            
    Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);

        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
        using (MemoryStream memoryStream = new MemoryStream())
        {
        PdfWriter writer = PdfWriter.GetInstance(pdfDoc, memoryStream);
        pdfDoc.Open();

        htmlparser.Parse(sr);
        pdfDoc.Close();

        byte[] bytes = memoryStream.ToArray();
        memoryStream.Close();



            FileStream fs = new FileStream(@"d:\REPORT.pdf", FileMode.OpenOrCreate);
            fs.Write(pdfBytes, 0, pdfBytes.Length);
            fs.Close();
        }

Any alternatives to iTextSharper for .Net core? or Any other way to fix this issue?

  • If you read the question [Converting HTML to PDF using iText](https://stackoverflow.com/questions/47895935), you'll get an overview of the flaws of `HTMLWorker`, which flaws were fixed in XML Worker, but also why XML Worker was abandoned in favor of iText 7 and the pdfHTML add-on. That question explains why your issue can't be fixed with the *unofficial iText fork* you're using, and that a recent version of the *real, official iText* is the best alternative for converting HTML to PDF. – Bruno Lowagie May 21 '18 at 14:04

1 Answers1

3

Please note that it is not allowed to ask people to recommend a tool or a library on Stack Overflow. Your question risks being closed as off-topic for that reason.

There's also another problem with your question: there is no such thing as iTextSharper. I created the iText library (written in Java) and I released version 0.30 of that PDF library in the year 2000. A C# version was created a couple of years later. It was initially called iTextSharp (NOT iTextSharper!). However, when trying to trademark those names, we obtained the trademark for the name iTextⓇ, but a company named Sharp didn't want us to continue to use the name iTextSharp. Hence we changed the name iTextSharp to iText for .NET.

Today, we have iText for Java, and iText for .NET. We no longer talk about iTextSharp, and neither should you. I also don't understand why people talk about iTextSharper. iText is a Trademarked name; there should be no confusion.

Several people forked iText, and iTextSharpLGPLv2.Core is one of those forks. People using such a fork (such as you) are often victim of the fact that people creating forks often overestimate themselves and underestimate the effort maintaining a software library. My advice? Stay away from forks if the original project is still actively maintained. Forks are only useful when the original project owners abandoned their project.

To make a long story short: you should use the real thing, not the copy or the fork.

If you want to convert HTML to PDF, you can use iText 7 and the pdfHTML add-on. Please consult the official web site, and you'll find an HTML to PDF tutorial. All the examples in this tutorial are in Java, but it's very easy for a .NET developer to convert those examples to C#. It's mainly a matter of changing methods that start with a lower-case character to methods with the same name that start with an upper-case character.

IMPORTANT: I am the original developer of the iText PDF library and the founder of the iText companies.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165