-->

Could not load type 'iTextSharp.text.html.Html

2019-01-26 00:21发布

问题:

see this link converting html to pdf I got this version error in webconfig let some genius find and solve the qustion.

My Model

 public class Customer
  {
    public int CustomerID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
  }

My Controller this is normal code

 public ActionResult Index()
    {
        List<Customer> customers = new List<Customer>();

        for (int i = 1; i <= 10; i++)
        {
            Customer customer = new Customer
            {
                CustomerID = i,
                FirstName = string.Format("FirstName{0}", i.ToString()),
                LastName = string.Format("LastName{0}", i.ToString())
            };
            customers.Add(customer);
        }
        return View(customers);
    }

this is for pdf convert controller

public ActionResult PDF()
    {
        List<Customer> customers = new List<Customer>();

        for (int i = 1; i <= 10; i++)
        {
            Customer customer = new Customer
            {
                CustomerID = i,
                FirstName = string.Format("FirstName{0}", i.ToString()),
                LastName = string.Format("LastName{0}", i.ToString())
            };
            customers.Add(customer);
        }

        return new RazorPDF.PdfResult(customers, "PDF");
    }

My webconfig

 <dependentAssembly>
    <assemblyIdentity name="itextsharp" publicKeyToken="8354ae6d2174ddca" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-5.5.5.0" newVersion="5.5.5.0" />
  </dependentAssembly>

回答1:

You've got a couple of problems.

First, you have a version binding redirect in place:

<bindingRedirect oldVersion="0.0.0.0-5.5.5.0" newVersion="5.5.5.0" />

This is a giant blanket statement that assumes no API changes have taken place between version 0.0.0.0 and 5.5.5.0. However, some/many/most/all libraries out there increment their major and minor version numbers when there is an API change.

Second, but related to the first, between iTextSharp 4.1.6 (the last released iTextSharp in the 4.x series, ported from the Java 2.x series) and 5 there was in fact some API changes. In your very specific case, the class iTextSharp.text.html.HtmlParser was removed which is why are getting that exception.

There's a couple of ways to fix this.

Option #1 - The Good Way

  1. Get rid of RazorPDF. It hasn't been updated in two and a half years, it requires an obsolete version of iTextSharp and uses an obsolete HTML parser.

  2. Switch to using iTextSharp's newer HTML parsing XmlWorker. See this (long winded) answer for how to use it.

Option #2 - The Bad Way

  1. Read the fourth box on the official iText website's sales FAQ page title "Why shouldn't I use iText 2.x (or iTextSharp 4.x)?"

  2. Download the iTextSharp 4.1.6 source code. You'll need to look for this on your own. Don't bother asking where to get it as this version is not supported by the community or even the makers of the software.

  3. Have your legal counsel inspect the source code, line by line, to ensure that it complies with your jurisdiction's laws as well as any international treaties regarding copyrights. Seriously.

  4. If your legal counsel approves the source code, compile it, remove the binding redirect and drop the DLL into your project.

  5. Accept the fact that version 4.1.6's parser is very, very limited and has a couple of known issues that will throw exceptions for what you would consider perfectly valid HTML. Also accept that if you ask for any support for these problems you will be told two things, to upgrade to the most recent version and to switch from HTMLWorker to XmlWorker.

Option #3 - The Ugly Way (for Bruno)

  1. Download the official iTextSharp source.

  2. Re-implement the iTextSharp.text.html.HtmlParser and all other missing classes, methods and properties using either the 4.1.6 logic or your own.

  3. Compile and link