public string HTMLToPdf(string HTML, string FileName) { FileName = FileName.Replace(' ', '+').Replace('/', '@');//Here @ char replace due to '/' encrypt id it break the URL to open file in HTMLToPDF string HostingPrefix = Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["hostingPrefix"]); var fileName = System.Web.HttpContext.Current.Server.MapPath("~/Content/eMaintenance/SurveyDownload/") + FileName + ".pdf"; //Render PlaceHolder to temporary stream System.IO.StringWriter stringWrite = new StringWriter(); System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); StringReader reader = new StringReader(HTML); //Create PDF document Document doc = new Document(PageSize.A4); HTMLWorker parser = new HTMLWorker(doc); PdfWriter.GetInstance(doc, new FileStream(fileName, FileMode.Create)); doc.Open(); /********************************************************************************/ var interfaceProps = new Dictionary <string, Object>(); var ih = new ImageHander() { BaseUri = Request.Url.ToString() }; interfaceProps.Add(HTMLWorker.IMG_PROVIDER, ih); foreach (IElement element in HTMLWorker.ParseToList( new StringReader(HTML), null)) { doc.Add(element); } doc.Close(); Response.Clear(); Response.Buffer = true; Response.ContentType = "application/pdf"; Response.AddHeader("content-disposition", "attachment;filename=" + FileName); // to open file prompt Box open or Save file Response.Charset = ""; Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.WriteFile(fileName); Response.End(); return(FileName); }
// ConvertControlToPdf is a HtmlTable, used for brevity. // - HTML conversion for a GridView is **EXACTLY** the same protected void ProcessHtml(object sender, CommandEventArgs e) { Response.ContentType = "application/pdf"; Response.AppendHeader("Content-Disposition", "attachment; filename=table.pdf"); //using (Document document = new Document()) Document document = new Document(); //{ PdfWriter.GetInstance(document, Response.OutputStream); document.Open(); var html = new StringBuilder(); using (var stringWriter = new StringWriter(html)) { using (var htmlWriter = new HtmlTextWriter(stringWriter)) { // replace 'ConvertControlToPdf' with **YOUR** GridView control Id! ConvertControlToPdf.RenderControl(htmlWriter); } } var providers = new Dictionary<string, Object>(); // HTMLWorker does **NOT** understand relative URLs, so // make existing ones in HTML source absolute, and handle // base64 Data URI schemes var ih = new ImageHander() { BaseUri = Request.Url.ToString() }; // dictionary key 'img_provider' is **HARD-CODED**, in // iTextSharp 5.0.0 - 5.0.5, so you may need to use next line // providers.Add("img_provider", ih); providers.Add(HTMLWorker.IMG_PROVIDER, ih); // ^^^^^^^^^^^^^^^^^^^^^^^ - constant added in 5.0.6 using (var sr = new StringReader(html.ToString())) { foreach (IElement element in HTMLWorker.ParseToList( sr, null, providers)) { PdfPTable table = element as PdfPTable; document.Add(element); } } Response.Write(document); // } Response.End(); }