use ABCpdf dll, here in text area we can write html code and on a button click corresponding pdf will be shown.ABCpdf trail version is easily available for downloding, adding linlk to download the ABCpdf dll https://www.websupergoo.com/download.htm
index.cshtml
@using (Html.BeginForm("covertopdf", "simple", FormMethod.Post)) { <p style="margin-top:50px"> Input Html: @Html.TextArea("Htmlcontent", new { @class = "form-control",@cols="160" , @rows="20"})<br /> <input type="submit" class="btn-primary" value="Convertopdf" /> </p> }
SimpleController.cs
public class SimpleController : Controller { public class FileViewModel { public byte[] Content { get; set; } public string Extension { get; set; } public string FileName { get; set; } } [HttpPost] [ValidateInput(false)] public FileStreamResult covertopdf(string Htmlcontent) //public FileStreamResult covertopdf(file fo) { var result = ExecuteAction(() => { var fileViewmodel = new FileViewModel { Content = ConvertHtmlToPdf(Htmlcontent), //Content= ConvertHtmlToPdf(fo.cont), Extension = "application/pdf", FileName = "Policy Information.pdf" }; return fileViewmodel; }, "covertopdf"); // return result; HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK); // Content is the file Stream stream = new MemoryStream(result.Content); return new FileStreamResult(stream, "application/pdf") { }; } public T ExecuteAction<T>(Func<T> action, string method) { try { return action.Invoke(); } catch (Exception ex) { return default(T); } } protected byte[] ConvertHtmlToPdf(string html, string header = null, string footer = null, bool isPageNumberInFooter = false) { // Create ABCpdf Doc object var doc = new Doc(); if (header == null && footer == null) doc.Rect.Inset(20, 20); else doc.Rect.String = "0 70 600 760"; /*padding from left, padding from bottom, width from left, height from bottom*/ // Add html to Doc //html = "<html><head></head><body></body></html>"; int theId = doc.AddImageHtml(html); // Loop through document to create multi-page PDF while (true) { if (!doc.Chainable(theId)) break; doc.Page = doc.AddPage(); theId = doc.AddImageToChain(theId); } var count = doc.PageCount; /*****************Footer area******************/ if (footer != null) { var newfooter = ""; doc.Rect.String = "40 20 580 50"; for (int i = 1; i <= count; i++) { doc.PageNumber = i; if (isPageNumberInFooter) { newfooter = footer.Replace("PageNumber", "Page " + i.ToString() + " of " + count.ToString()); int id = doc.AddImageHtml(newfooter); while (true) { if (!doc.Chainable(id)) break; id = doc.AddImageToChain(id); } } else doc.AddText(footer); } } /*****************Footer area******************/ // Flatten the PDF for (int i = 1; i <= doc.PageCount; i++) { doc.PageNumber = i; doc.Flatten(); } var pdf = doc.GetData(); doc.Clear(); // Get PDF as byte array. Couls also use .Save() to save to disk return pdf; } }