ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.[Solved] Converting HTML to PDF generated blank PDF using iTextSharp in ASP.Net<p>Hi <a class="username" rel="svibuk">svibuk</a>,</p> <p>Please take reference to the below code and correct your code.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;div id=&#34;divpdf&#34; runat=&#34;server&#34;&gt; &lt;/div&gt; &lt;asp:Button ID=&#34;btnHTML&#34; Text=&#34;Create HTML&#34; runat=&#34;server&#34; OnClick=&#34;btnHTML_Click&#34; /&gt;</pre> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Text; using System.IO; using iTextSharp.text; using iTextSharp.text.html.simpleparser; using iTextSharp.text.pdf;</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">public void htmlstr() { &nbsp;&nbsp;&nbsp; StringBuilder sb = new StringBuilder(); &nbsp;&nbsp;&nbsp; string filename = "A"; &nbsp;&nbsp;&nbsp; sb.Append(" &lt;html&gt;&lt;body&gt;&lt;table width='100%' cellspacing='0' cellpadding='2'&nbsp; &gt;"); &nbsp;&nbsp;&nbsp; sb.Append("&lt;tr&gt;&lt;td colspan='4'&gt;&lt;/td&gt;&lt;/tr&gt;"); &nbsp;&nbsp;&nbsp; sb.Append("&lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;th&gt;&lt;img id='alogo'&nbsp; height='75' width=75' src=" + Server.MapPath("~/images/ logo.png") + "&gt;&lt;/th&gt;&lt;td&gt;&lt;img id='xlogo'&nbsp; height='75' width=75' src=" + Server.MapPath("~/images/ logo.png") + "&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;"); &nbsp;&nbsp;&nbsp; sb.Append("&lt;tr&gt;&lt;td colspan='4'&gt;Date :" + DateTime.Now.ToString("dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture) + "&lt;/td&gt;&lt;/tr&gt;"); &nbsp;&nbsp;&nbsp; sb.Append("&lt;tr&gt;&lt;td colspan='4'&gt;&lt;/td&gt;&lt;/tr&gt;"); &nbsp;&nbsp;&nbsp; sb.Append("&lt;tr&gt;&lt;td colspan='4'&gt;Hello&lt;/td&gt;&lt;/tr&gt;"); &nbsp;&nbsp;&nbsp; sb.Append("&lt;tr&gt;&lt;td colspan='4'&gt;&lt;/td&gt;&lt;/tr&gt;"); &nbsp;&nbsp;&nbsp; sb.Append("&lt;tr&gt;&lt;td colspan='4'&gt;&lt;/td&gt;&lt;/tr&gt;"); &nbsp;&nbsp;&nbsp; sb.Append("&lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;td colspan='2'&gt;test matter&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;"); &nbsp;&nbsp;&nbsp; sb.Append("&lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;td colspan='2'&gt;test matter&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;"); &nbsp;&nbsp;&nbsp; sb.Append("&lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;td colspan='2'&gt;test matter&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;"); &nbsp;&nbsp;&nbsp; sb.Append("&lt;tr&gt;&lt;td colspan='4'&gt;&lt;/td&gt;&lt;/tr&gt;"); &nbsp;&nbsp;&nbsp; sb.Append("&lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;td colspan='2'&gt; test&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;"); &nbsp;&nbsp;&nbsp; sb.Append("&lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;td colspan='2'&gt;test&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;"); &nbsp;&nbsp;&nbsp; sb.Append("&lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;td colspan='2'&gt;test&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;"); &nbsp;&nbsp;&nbsp; sb.Append("&lt;tr&gt;&lt;td colspan='4'&gt;&lt;/td&gt;&lt;/tr&gt;"); &nbsp;&nbsp;&nbsp; sb.Append("&lt;tr&gt;&lt;td colspan='4'&gt;&lt;/td&gt;&lt;/tr&gt;"); &nbsp;&nbsp;&nbsp; sb.Append("&lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;td colspan='2'&gt;Test footer&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;"); &nbsp;&nbsp;&nbsp; sb.Append(" &lt;/table&gt;&lt;/body&gt;&lt;/html&gt;"); &nbsp;&nbsp;&nbsp; divpdf.InnerHtml = sb.ToString(); } protected void btnHTML_Click(object sender, EventArgs e) { &nbsp;&nbsp;&nbsp; htmlstr(); &nbsp;&nbsp;&nbsp; string filename = "A"; &nbsp;&nbsp;&nbsp; Response.ContentType = "application/pdf"; &nbsp;&nbsp;&nbsp; Response.AddHeader("content-disposition", "attachment;filename=" + filename + ".pdf"); &nbsp;&nbsp;&nbsp; Response.Cache.SetCacheability(HttpCacheability.NoCache); &nbsp;&nbsp;&nbsp; StringWriter sw = new StringWriter(); &nbsp;&nbsp;&nbsp; HtmlTextWriter hw = new HtmlTextWriter(sw); &nbsp;&nbsp;&nbsp; divpdf.RenderControl(hw); &nbsp;&nbsp;&nbsp; StringReader sr = new StringReader(sw.ToString()); &nbsp;&nbsp;&nbsp; Document pdfDoc = new Document(PageSize.A4.Rotate(), 10f, 10f, 100f, 0f); &nbsp;&nbsp;&nbsp; HTMLWorker htmlparser = new HTMLWorker(pdfDoc); &nbsp;&nbsp;&nbsp; PdfWriter.GetInstance(pdfDoc, Response.OutputStream); &nbsp;&nbsp;&nbsp; pdfDoc.Open(); &nbsp;&nbsp;&nbsp; htmlparser.Parse(sr); &nbsp;&nbsp;&nbsp; pdfDoc.Close(); &nbsp;&nbsp;&nbsp; Response.Write(pdfDoc); &nbsp;&nbsp;&nbsp; Response.End(); }</pre>https://www.aspforums.net:443/Threads/116263/Solved-Converting-HTML-to-PDF-generated-blank-PDF-using-iTextSharp-in-ASPNet/https://www.aspforums.net:443/Threads/116263/Solved-Converting-HTML-to-PDF-generated-blank-PDF-using-iTextSharp-in-ASPNet/Thu, 05 Jul 2018 05:47:01 GMT