Create and Save PDF using Rotativa in MVC Application

<div style=”text-align:right;padding-top:20px!important;”>@Html.ActionLink(“Print About Page”,”PrintAboutPage”)</div><div><h2>@ViewBag.Title.</h2><h3>@ViewBag.Message</h3><p>Use this area to provide additional information.</p></div>
public ActionResult About(){ViewBag.Message = “Your application description page.”;return View();}public ActionResult PrintAboutPage(){var report = new Rotativa.ActionAsPdf(“About”);return report;}
<div style=”text-align:right;padding-top:20px!important;”>@Html.ActionLink(“Print Contact Page”, “PrintContactPage”)</div><div><h2>@ViewBag.Title.</h2><h3>@ViewBag.Message</h3></div>
public ActionResult PrintContactPage(){var report = new Rotativa.ActionAsPdf(“Contact”, new { name = “Vaibhav” });return report;}
<div style=”text-align:right;padding-top:20px!important;”>@Html.ActionLink(“Print About Page”,”PrintAboutPage”)</div><div style=”text-align:right;padding-top:20px!important;”>@Html.ActionLink(“Save About Page”, “SaveAboutPage”)</div><div><h2>@ViewBag.Title.</h2><h3>@ViewBag.Message</h3><p>Use this area to provide additional information.</p></div>
public ActionResult SaveAboutPage(){string fileName = “Test.pdf”;string fullPath = @”C:\Users\Vaibhav\Documents\RotativaPdf\” + fileName;var report = new Rotativa.ActionAsPdf(“About”){PageOrientation = Rotativa.Options.Orientation.Portrait,PageSize = Rotativa.Options.Size.A4,PageMargins = new Margins(0, 0, 0, 0),};if (!System.IO.File.Exists(fullPath)){var byteArray = report.BuildPdf(ControllerContext);var fileStream = new FileStream(fullPath, FileMode.Create, FileAccess.Write);fileStream.Write(byteArray, 0, byteArray.Length);fileStream.Close();}return report;}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store