ASP.NET : How to Generate PDF from HTML with iTextSharp

ASP.NET : How to Generate PDF from HTML with iTextSharp

We intended this article in order to make global asp.net MVC development community aware of the technique to generate PDF file from HTML with the help of iTextSharp. iTextSharp is a .net PDF library that enables developers to produce PDF file. How it makes the things possible, let’s find out.

ITextSharp is a .NET PDF library which allows you to generate PDF (Portable Document Format). There are many other feature of ITextSharp but currently we are implementing the feature to generate PDF from HTML content in ASP.NET MVC.

So first of all we need to add reference of iTextSharp in our project.

We can get iTextSharp reference using package manager, we just need to execute following command to download and add reference.

PM> Install-Package iTextSharp

After executing this command in Package Manager Console you can find it in solution explorer under Reference like this:

Package Manager Console - iTextSharp

Now you need to create a method which will give you byte array of PDF content, so our code will be .

public byte[] GetPDF(string pHTML) {
    byte[] bPDF = null;

    MemoryStream ms = new MemoryStream();
    TextReader txtReader = new StringReader(pHTML);

    // 1: create object of a itextsharp document class
    Document doc = new Document(PageSize.A4, 25, 25, 25, 25);

    // 2: we create a itextsharp pdfwriter that listens to the document and directs a XML-stream to a file
    PdfWriter oPdfWriter = PdfWriter.GetInstance(doc, ms);

    // 3: we create a worker parse the document
    HTMLWorker htmlWorker = new HTMLWorker(doc);

    // 4: we open document and start the worker on the document
    doc.Open();
    htmlWorker.StartDocument();

    // 5: parse the html into the document
    htmlWorker.Parse(txtReader);

    // 6: close the document and the worker
    htmlWorker.EndDocument();
    htmlWorker.Close();
    doc.Close();

    bPDF = ms.ToArray();

    return bPDF;
}

So this method will returns byte array of PDF and you can use that according to your requirement.

For example if you want to download this pdf in ASP.NET MVC application then do the following code.

public void DownloadPDF() {
    string HTMLContent = "Hello <b>World</b>";

    Response.Clear();
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=" + "PDFfile.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.BinaryWrite(GetPDF(HTMLContent));
    Response.End();
}

Set this method in controller and call this method by executing following URL to download the PDF file:

http://www.yourdomainname.com/controllername/DownloadPDF

This article is written by Ethan Millar. He shared the technique to generate PDF from HTML using iTextSharp with asp.net MVC development platform. You can try it and share your experience with us. Also, if you face any issue while performing, you can leave the comment and get expert assistance for your query.

Disclosure: Some of our articles may contain affiliate links; this means each time you make a purchase, we get a small commission. However, the input we produce is reliable; we always handpick and review all information before publishing it on our website. We can ensure you will always get genuine as well as valuable knowledge and resources.

Recommended Content for you:

Related Articles You May Like:

Article Published by Souvik Banerjee

Web Developer & SEO Specialist with 10+ years of experience in Open Source Web Development, specialized in Joomla & WordPress development. He is also the moderator of this blog "RS Web Solutions".

Comments are closed.

18 thoughts on “ASP.NET : How to Generate PDF from HTML with iTextSharp

  1. Hi!

    Whenever i am using anchar tag with href attribute in HTML, it’s not able to parse and error is “Unable to cast object of type ‘iTextSharp.text.html.simpleparser.CellWrapper’ to type ‘iTextSharp.text.Paragraph’.”.

    I am trying to create an action link within one pdf from the thumbnail image to original image. Please help me.

  2. htmlWorker.Parse(txtReader);
    at this point my code is not executing showing input string was not in a correct format.when converting a string to datetime,parse the string to take the date before putting each variable into datetime object.
    below is my code.

    private void btncreatepdf_Click(object sender, EventArgs e)
    {
    // GeneratePDF();
    // ConvertHTMLtoPDF();
    //ConvertHTMLtoPDF();
    string HTMLData = prepareHTML();
    GetPDF(HTMLData);
    // createpdf(HTMLData);

    }

    string prepareHTML()
    {
    DataTable dt = new DataTable();
    dt = ds.Tables[0];
    //string Data;
    StringBuilder sb = new StringBuilder();
    try
    {

    sb.Append(“”);

    for (int i = 0; i < dt.Rows.Count-1 ; i++)
    {
    sb.Append("” + Convert.ToString(dt.Rows[i][2]).Replace(” “,””) + “”);
    sb.Append(“” + Convert.ToString(dt.Rows[i][3]).Replace(” “, “”) + “”);
    sb.Append(“” + Convert.ToString(dt.Rows[i][4]).Replace(” “,””) + “”);
    }

    sb.Append(“”);

    }
    catch (Exception ex)
    {

    throw ;
    }
    return sb.ToString();
    }

    public byte[] GetPDF(string pHTML)
    {
    try
    {

    byte[] bPDF = null;

    MemoryStream ms = new MemoryStream();
    TextReader txtReader = new StringReader(pHTML);

    // 1: create object of a itextsharp document class
    Document doc = new Document(PageSize.A4, 25, 25, 25, 25);
    //Document doc = new Document(PageSize.A4);

    // 2: we create a itextsharp pdfwriter that listens to the document and directs a XML-stream to a file
    PdfWriter oPdfWriter = PdfWriter.GetInstance(doc, ms);

    // 3: we create a worker parse the document
    HTMLWorker htmlWorker = new HTMLWorker(doc);

    // 4: we open document and start the worker on the document
    doc.Open();
    htmlWorker.StartDocument();
    // 5: parse the html into the document
    // htmlWorker.Parse(txtReader);
    htmlWorker.Parse(txtReader);

    // 6: close the document and the worker
    htmlWorker.EndDocument();
    htmlWorker.Close();
    doc.Close();

    bPDF = ms.ToArray();
    System.IO.File.WriteAllBytes(@”D:/1.pdf”, bPDF);
    return bPDF;

    }
    catch (Exception ex)
    {
    return null;
    throw ;
    }
    }

  3. hi

    its really a good article but i want to save this pdf byte array to some path, while doing I am not getting proper pdf.

    Can you please tell me how can I do that?

  4. Hi

    I’m using ASP.Net Core and iTextSharp is not compatible with Core …. any idea about generating PDF’s from Core ?

    Thanks

  5. Hi,

    How to send html (DIV) content to Action as (HTMLContent):

    @using (@Html.BeginForm()) {
    ….
    ….
    ….
    }

    • The following should work for you:

      Controller action:

      ViewBag.HtmlString = "<Your HTML Code>";

      View:

      @Html.Raw(ViewBag.HtmlString)
  6. Please do not use the deprecated HTMLWorker. Refer to the official documentation and use XMLWorker!

  7. public byte[] GetPDF(string pHTML) {
    byte[] bPDF = null;

    MemoryStream ms = new MemoryStream();
    TextReader txtReader = new StringReader(pHTML);

    // 1: create object of a itextsharp document class
    Document doc = new Document(PageSize.A4, 25, 25, 25, 25);

    // 2: we create a itextsharp pdfwriter that listens to the document and directs a XML-stream to a file
    PdfWriter oPdfWriter = PdfWriter.GetInstance(doc, ms);

    // 3: we create a worker parse the document
    HTMLWorker htmlWorker = new HTMLWorker(doc);

    // 4: we open document and start the worker on the document
    doc.Open();
    htmlWorker.StartDocument();

    // 5: parse the html into the document
    htmlWorker.Parse(txtReader);

    // 6: close the document and the worker
    htmlWorker.EndDocument();
    htmlWorker.Close();
    doc.Close();

    bPDF = ms.ToArray();

    return bPDF;
    }

    In: // 5: parse the html into the document
    htmlWorker.Parse(txtReader);

    return error: The URI prefix is not recognized.
    help me plz

    • Please make sure that the URI string must start with a proper protocol like “http://” or “https://”.