Sunday, 14 June 2015

itextsharp html to pdf


1.  Before starting the code for conversting the HTML to PDF using ITextSharp you need to download the ITextsharp Dll and placed it into the Bin folder. You can get the ItextSharp Dll from below path;


2.  Now you want to generate the PDF, by clicking any of the button in your page, You have to place the below code on your C# file and  there it will pass the HTML content to the Export method where it is using the ItextSharp methods for converting the HTML to PDF..

protected void btnGeneratePDF_Click(object sender, EventArgs e)
        {
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            this.Page.RenderControl(hw);
            //   string redirectUrl = HttpContext.Current.Request.Url.AbsoluteUri;
          


            Export(sw.ToString(), "APD" + DateTime.Now.Year + "_" + DateTime.Now.Month + "_" + DateTime.Now.Date + "_" + DateTime.Now.Hour + "_" + DateTime.Now.Minute + "_" + DateTime.Now.Second + ".pdf", "~/css/PdfFilesStyle.css");
        }

3.  This is the main code for conerting Your HTML to PDF format. There diffrent settings are providing for PDF converstion for your HTMl page. If there is any image attached on your page than you need to call the below methods also.
  public static void Export(string html, string fileName, string linkCss, bool useChinaFont = false)
        {
            ////reset response
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ContentType = "application/pdf";

            ////define pdf filename
            HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" + fileName);


            //Generate PDF
            using (var document = new Document(PageSize.A4, 10, 10, 10, 10))
            {
                html = FormatImageLinks(html);

                ////define output control HTML
                var memStream = new MemoryStream();
                //TextReader xmlString = new StringReader(html);
                TextReader xmlString = new StringReader(html);

                PdfWriter writer = PdfWriter.GetInstance(document, memStream);

                //open doc
                document.Open();

                // register all fonts in current computer
                FontFactory.RegisterDirectories();


                // Set factories
                var htmlContext = new HtmlPipelineContext(null);
                htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());

                // Set css
                ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(false);
                cssResolver.AddCssFile(HttpContext.Current.Server.MapPath(linkCss), true);
                // cssResolver.AddCssFile(HttpContext.Current.Server.MapPath("~/Assets/css/bootstrap.css"), true);


                // Export
                IPipeline pipeline = new CssResolverPipeline(cssResolver,
                                                             new HtmlPipeline(htmlContext,
                                                                              new PdfWriterPipeline(document, writer)));
                var worker = new XMLWorker(pipeline, true);
                var xmlParse = new XMLParser(true, worker);
                xmlParse.Parse(xmlString);
                xmlParse.Flush();

                document.Close();
                document.Dispose();

                HttpContext.Current.Response.BinaryWrite(memStream.ToArray());
            }

            HttpContext.Current.Response.End();
            HttpContext.Current.Response.Flush();
        }

4.  This method use to convert your Images of HTML into the PDF format. If you don’t place the below code images of the page is not converting to the HTML. It is Itextsharp code only to convert the images of HTML to PDF.

        public static string FormatImageLinks(string input)
        {
            if (input == null)
                return string.Empty;
            string tempInput = input;
            const string pattern = @"<img(.|\n)+?>";
            HttpContext context = HttpContext.Current;

            //Change the relative URL's to absolute URL's for an image, if any in the HTML code.
            foreach (Match m in Regex.Matches(input, pattern, RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.RightToLeft))
            {
                if (m.Success)
                {
                    string tempM = m.Value;
                    string pattern1 = "src=[\'|\"](.+?)[\'|\"]";
                    Regex reImg = new Regex(pattern1, RegexOptions.IgnoreCase | RegexOptions.Multiline);
                    Match mImg = reImg.Match(m.Value);

                    if (mImg.Success)
                    {
                        string src = mImg.Value.ToLower().Replace("src=", "").Replace("\"", "").Replace("\'", "");

                        if (!src.StartsWith("http://") && !src.StartsWith("https://"))
                        {
                            //Insert new URL in img tag
                            src = "src=\"" + context.Request.Url.Scheme + "://" +
                                  context.Request.Url.Authority + src + "\"";
                            try
                            {
                                tempM = tempM.Remove(mImg.Index, mImg.Length);
                                tempM = tempM.Insert(mImg.Index, src);

                                //insert new url img tag in whole html code
                                tempInput = tempInput.Remove(m.Index, m.Length);
                                tempInput = tempInput.Insert(m.Index, tempM);
                            }
                            catch (Exception)
                            {

                            }
                        }
                    }
                }
            }
            return tempInput;
        }
      

5.  Apply below CSS to formatting and providing the style for your PDF generated files;we are passing this style path on button click; "~/css/PdfFilesStyle.css

body {
}



.HeaderStyleTD {
    border-top-style: solid;
    border-top-width: 1px;
    border-top-color: darkgray;
    border-bottom-style: solid;
    border-bottom-width: 1px;
    border-bottom-color: darkgray;
    background-color: #f5f5f5;
    padding-top: 3px;
    padding-bottom: 3px;
    padding-left: 4px;
    padding-right: 4px;
    font-size: 15px;
    font-weight: bold;
    vertical-align: bottom;
    height: 20px;
}

.muted {
    color: #777;
}

.gridColums {
    padding-top: 2px;
    padding-bottom: 2px;
    vertical-align: bottom;
    border-top-style: solid;
    border-top-width: 1px;
    border-top-color: darkgray;
    font-size: 14px;
    padding-left: 4px;
    padding-right: 4px;
    background-color: #f9f9f9;
}

.totaltext {
    vertical-align: bottom;
    border-bottom-style: solid;
    border-bottom-width: 2px;
    border-bottom-color: darkgray;
    background-color: lightgrey;
    padding-top: 2px;
    padding-bottom: 2px;
    font-size: 15px;
    font-weight: bold;
    padding-left: 4px;
    padding-right: 4px;
}

/* Form PDF Style*/
.tableStyle {
    font-family: Arial;
    font-size: 10px;
    border-bottom-style: solid;
    border-bottom-width: 1px;
    border-bottom-color: black;
    border-top-style: solid;
    border-top-width: 1px;
    border-top-color: black;
    border-left-style: solid;
    border-left-width: 1px;
    border-left-color: black;
    border-right-style: solid;
    border-right-width: 1px;
    border-right-color: black;
    align-items: center;
}

tdStyle1 {
}

.tdStyle2 {
      font-family: Arial;
    font-size: 10px;
    text-align: left;
    padding-left: 3px;
    padding-right: 3px;
    padding-top: 3px;
    padding-bottom: 3px;

     border-bottom-style: solid;
    border-bottom-width: 1px;
    border-bottom-color: black;
  /*margin-left:5px;
  margin-right:5px;
   margin-top:5px;
  margin-bottom:5px;*/

 
    border-right-style: solid;
    border-right-width: 1px;
    border-right-color: black;
}

.tdStyle2Right {
      font-family: Arial;
    font-size: 10px;
    text-align: left;

 
    border-right-style: solid;
    border-right-width: 1px;
    border-right-color: black;
}
.tdStyle2Left {
      font-family: Arial;
    font-size: 10px;
    text-align: left;

 
    border-left-style: solid;
    border-left-width: 1px;
    border-left-color: black;
}

.textBoxStyle {
     font-family: Arial;
    font-size: 10px;
     border-top-style: solid;
    border-top-width: 1px;
    border-top-color: #cccccc;

    border-bottom-style: solid;
    border-bottom-width: 1px;
    border-bottom-color: #cccccc;

     border-right-style: solid;
    border-right-width: 1px;
    border-right-color: #cccccc;

     border-left-style: solid;
    border-left-width: 1px;
    border-left-color: #cccccc;
    padding: 1px 1px 1px 1px;
    background-color :#e8e2e2;
   
}

.tdStyle2Grey {
      font-family: Arial;
    font-size: 10px;
    text-align: left;
 
 

     border-bottom-style: solid;
    border-bottom-width: 1px;
    border-bottom-color: black;
 

  background-color:#d3d3d3;
    border-right-style: solid;
    border-right-width: 1px;
    border-right-color: black;
}

.gridColumsForm {
    padding-top: 2px;
    padding-bottom: 2px;
    vertical-align: bottom;
    border-top-style: solid;
    border-top-width: 1px;
    border-top-color: darkgray;
    font-size: 14px;
  
     font-family: Arial;
    font-size: 10px;
    text-align: left;
    /*background-color: #f9f9f9;*/
}

.tdStyle3 {
      font-family: Arial;
    font-size: 10px;
    text-align: left;
  
   

 
    border-right-style: solid;
    border-right-width: 1px;
    border-right-color: black;
}


2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. I need to export a div of a .aspx with externals css to .pdf , can you send to my email a complete example ?, please my email is pgpalmeros1@gmail.com , thanks for the help .

    ReplyDelete