How to create PDF in asp.net mvc5
Ask Question
By
Junaid A
13-Nov-2024
1
1) Install nuget using SelectPdf;
Select.HtmlToPdf.17.3.0
2) Create html page
pass that html page to pdf page using following way
public ActionResult MyPDF(int id)
{
var ticketDetail = _uow.Tickets.GetById(id);
var body = _uow.EmailHtmlTemplates.GetHTML(Convert.ToInt32(EmailTemplateEnum.CustomerTicket));
//string body = System.IO.File.ReadAllText(Server.MapPath("~/EmailTemplates/MyTicket.html"));
body = body.Replace("__PRICE__", Convert.ToInt32(ticketDetail.Ad.TicketPrice).ToString());
body = body.Replace("__ORDER ID__", ticketDetail.Order_Id.ToString());
HtmlToPdf converter = new HtmlToPdf();
//PdfPageSize pageSize = (PdfPageSize)Enum.Parse(typeof(PdfPageSize), "A6", true);
PdfPageSize pageSize = (PdfPageSize)Enum.Parse(typeof(PdfPageSize), "B5", true);
PdfPageOrientation pdfOrientation =
(PdfPageOrientation)Enum.Parse(typeof(PdfPageOrientation),
"Portrait", true); // Landscape Portrait
converter.Options.PdfPageSize = pageSize;
// converter.Options.PdfPageOrientation = pdfOrientation;
converter.Options.WebPageWidth = 595;
converter.Options.WebPageHeight = 300;
//converter.Options.MarginTop = 20;
//converter.Options.AutoFitHeight = HtmlToPdfPageFitMode.AutoFit;
//converter.Options.AutoFitWidth = HtmlToPdfPageFitMode.AutoFit;
PdfDocument doc = converter.ConvertHtmlString(body);
var bytes = doc.Save(); //convert doc to byte array 2480px*1064
return File(bytes, "application/pdf");
//return View(ticketDetail);
}
Solutions