Search This Blog

Wednesday 5 September 2018

Render Razor View to String in ASP.NET MVC

public string RenderRazorViewToString(string viewName, object model = null)
        {
            ViewData.Model = model;
            using (var sw = new StringWriter())
            {
                var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
                var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
                viewResult.View.Render(viewContext, sw);
                viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View);
                return sw.GetStringBuilder().ToString();
            }
        }

Note:-
2nd Parameter is optional you may pass the model or not.


Render Razor View to String and Generate PDF from using iTextSharp in ASP.NET MVC

Note:- 

Use NuGet Package Manager to download 

iTextSharp.dll and itextsharp.xmlworker.dll

Step 1: - Create following class in Models Folder

    public class ReceiptModel
    {
        public string Name { getset; }
        public string Address { getset; }
        public IEnumerable<ItemModel> Items { getset; }
        public float GrossAmountTotal { getset; }
        public float DiscountAmountTotal { getset; }
        public float NetAmountTotal { getset; }
    }

    public class ItemModel
    {
       public int SrNo { getset; }
        public string Description { getset; }
        public int Qty { getset; }
        public float GrossAmount { getset; }
        public float DiscountAmount { getset; }
        public float NetAmount { getset; }
    }

Step 2: - Create DemoController in Controller Folder and copy past following actions

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult ExportToPDFWithoutModel()
        {
            string strData = RenderRazorViewToString(@"~\Views\Demo\ReceiptWithoutModelTemplate.cshtml");

            using (MemoryStream stream = new System.IO.MemoryStream())
            {
                StringReader sr = new StringReader(strData);
                Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
                PdfWriter writer = PdfWriter.GetInstance(pdfDoc, stream);
                pdfDoc.Open();
                XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
                pdfDoc.Close();
                return File(stream.ToArray(), "application/pdf""Report.pdf");
            }
        }

        public ActionResult ExportToPDFWithModel()
        {
            ReceiptModel receiptModel = new ReceiptModel();
            receiptModel.Name = "Ram G";
            receiptModel.Address = "101, Eastern Plaza, Malad (east), Maharashtra, Mumbai-400097";
            receiptModel.Items = new List<ItemModel>
            {
                new ItemModel
                {
                    SrNo = 1,
                    Description = "Parle G 140g",
                    Qty = 12,
                   GrossAmount = 140,
                    DiscountAmount = 12,
                    NetAmount = 128
                },
                new ItemModel
                {
                    SrNo = 2,
                    Description = "Britannia 50-50 80g",
                    Qty = 6,
                    GrossAmount = 60,
                    DiscountAmount = 6,
                    NetAmount = 54
                },
                new ItemModel
                {
                    SrNo = 3,
                    Description = "Britannia Treat-Jim Jam 100g",
                    Qty = 3,
                    GrossAmount = 60,
                    DiscountAmount = 6,
                    NetAmount = 54
                },
                new ItemModel
                {
                    SrNo = 4,
                    Description = "Sunfeast Yippee Noodles-Magic Masala 280g",
                    Qty = 1,
                    GrossAmount = 45,
                    DiscountAmount = 1,
                    NetAmount = 44
                }
            };
            receiptModel.GrossAmountTotal = 305;
            receiptModel.DiscountAmountTotal = 25;
            receiptModel.NetAmountTotal = 280;

            string strData = RenderRazorViewToString(@"~\Views\Demo\ReceiptWithModelTemplate.cshtml", receiptModel);

            using (MemoryStream stream = new System.IO.MemoryStream())
            {
                StringReader sr = new StringReader(strData);
                Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
                PdfWriter writer = PdfWriter.GetInstance(pdfDoc, stream);
                pdfDoc.Open();
                XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
                pdfDoc.Close();
                return File(stream.ToArray(), "application/pdf""Report.pdf");
            }
        }

        public string RenderRazorViewToString(string viewName, object model = null)
        {
            ViewData.Model = model;
            using (var sw = new StringWriter())
            {
                var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
                var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
                viewResult.View.Render(viewContext, sw);
                viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View);
                return sw.GetStringBuilder().ToString();
            }
        }

Step 3: - Create Index.cshtml inside the Demo Folder i.e. reside in View Folder

    @Html.ActionLink("Export To PDF Without Model""ExportToPDFWithoutModel""Demo")
    @Html.ActionLink("Export To PDF With Model""ExportToPDFWithModel""Demo")

Step 4: - Create ReceiptWithoutModelTemplate.cshtml inside the Demo Folder i.e. reside in View Folder

<style type="text/css">
    table
    {
        font-familyConsolas;
        font-size9pt;
    }
</style>
<div style="padding: 30px;">
    <p style="text-align: center;">
        <b>Report generated from ReceiptWithoutModelTemplate.cshtml</b></p>
    <br />
    <table style="border-collapse: collapse; width: 100%">
        <tr>
            <td style="border-top: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000;
                padding: 6px 6px;">
                <b>Bill To:</b>
            </td>
            <td style="border-top: 1px solid #000000; border-right: 1px solid #000000; padding: 6px 6px;">
                <b>Ship To:</b>
            </td>
        </tr>
        <tr>
            <td style="border-top: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000;
                padding: 6px 6px;">
                Name: Ram G
            </td>
            <td style="border-top: 1px solid #000000; border-right: 1px solid #000000; padding: 6px 6px;">
                Name: Ram G
            </td>
        </tr>
        <tr>
            <td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000;
                border-right: 1px solid #000000; padding: 6px 6px;">
                Address: 101, Eastern Plaza, Malad (east), Maharashtra, Mumbai-400097.
            </td>
            <td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-right: 1px solid #000000;
                padding: 6px 6px;">
                Address: 101, Eastern Plaza, Malad (east), Maharashtra, Mumbai-400097.
            </td>
        </tr>
    </table>
    <br />
    <table style="border-collapse: collapse; width: 100%">
        <tr>
            <th style="border-top: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000;
                text-align: center; padding: 6px 6px;">
                Sr No.
            </th>
            <th style="border-top: 1px solid #000000; border-right: 1px solid #000000; text-align: center;
                padding: 6px 6px;">
                Description
            </th>
            <th style="border-top: 1px solid #000000; border-right: 1px solid #000000; text-align: center;
                padding: 6px 6px;">
                Qty
            </th>
            <th style="border-top: 1px solid #000000; border-right: 1px solid #000000; text-align: center;
                padding: 6px 6px;">
                Gross Amount
            </th>
            <th style="border-top: 1px solid #000000; border-right: 1px solid #000000; text-align: center;
                padding: 6px 6px;">
                Discount Amount
            </th>
            <th style="border-top: 1px solid #000000; border-right: 1px solid #000000; text-align: center;
                padding: 6px 6px;">
                Net Amount
            </th>
        </tr>
        <tr>
            <td style="border-top: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000;
                text-align: center; padding: 6px 6px;">
                1
            </td>
            <td style="border-top: 1px solid #000000; border-right: 1px solid #000000; padding: 6px 6px;">
                Parle G 140g
            </td>
            <td style="border-top: 1px solid #000000; border-right: 1px solid #000000; text-align: center;
                padding: 6px 6px;">
                12
            </td>
            <td style="border-top: 1px solid #000000; border-right: 1px solid #000000; text-align: right;
                padding: 6px 6px;">
                140
            </td>
            <td style="border-top: 1px solid #000000; border-right: 1px solid #000000; text-align: right;
                padding: 6px 6px;">
                12
            </td>
            <td style="border-top: 1px solid #000000; border-right: 1px solid #000000; text-align: right;
                padding: 6px 6px;">
                128
            </td>
        </tr>
        <tr>
            <td style="border-top: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000;
                text-align: center; padding: 6px 6px;">
                2
            </td>
            <td style="border-top: 1px solid #000000; border-right: 1px solid #000000; padding: 6px 6px;">
                Britannia 50-50 80g
            </td>
            <td style="border-top: 1px solid #000000; border-right: 1px solid #000000; text-align: center;
                padding: 6px 6px;">
                6
            </td>
            <td style="border-top: 1px solid #000000; border-right: 1px solid #000000; text-align: right;
                padding: 6px 6px;">
                60
            </td>
            <td style="border-top: 1px solid #000000; border-right: 1px solid #000000; text-align: right;
                padding: 6px 6px;">
                6
            </td>
            <td style="border-top: 1px solid #000000; border-right: 1px solid #000000; text-align: right;
                padding: 6px 6px;">
                54
            </td>
        </tr>
        <tr>
            <td style="border-top: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000;
                text-align: center; padding: 6px 6px;">
                3
            </td>
            <td style="border-top: 1px solid #000000; border-right: 1px solid #000000; padding: 6px 6px;">
                Britannia Treat-Jim Jam 100g
            </td>
            <td style="border-top: 1px solid #000000; border-right: 1px solid #000000; text-align: center;
                padding: 6px 6px;">
                3
            </td>
           <td style="border-top: 1px solid #000000; border-right: 1px solid #000000; text-align: right;
                padding: 6px 6px;">
                60
            </td>
            <td style="border-top: 1px solid #000000; border-right: 1px solid #000000; text-align: right;
                padding: 6px 6px;">
                6
            </td>
            <td style="border-top: 1px solid #000000; border-right: 1px solid #000000; text-align: right;
                padding: 6px 6px;">
                54
            </td>
        </tr>
        <tr>
            <td style="border-top: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000;
                text-align: center; padding: 6px 6px;">
                4
            </td>
            <td style="border-top: 1px solid #000000; border-right: 1px solid #000000; padding: 6px 6px;">
                Sunfeast Yippee Noodles-Magic Masala 280g
            </td>
            <td style="border-top: 1px solid #000000; border-right: 1px solid #000000; text-align: center;
                padding: 6px 6px;">
                1
            </td>
            <td style="border-top: 1px solid #000000; border-right: 1px solid #000000; text-align: right;
                padding: 6px 6px;">
                45
            </td>
            <td style="border-top: 1px solid #000000; border-right: 1px solid #000000; text-align: right;
                padding: 6px 6px;">
                1
            </td>
            <td style="border-top: 1px solid #000000; border-right: 1px solid #000000; text-align: right;
                padding: 6px 6px;">
                44
            </td>
        </tr>
        <tr>
            <td colspan="3" style="border-top: 1px solid #000000; border-bottom: 1px solid #000000;
                border-left: 1px solid #000000; border-right: 1px solid #000000; text-align: right;
                padding: 6px 6px;">
                <b>Total:</b>
            </td>
            <td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-right: 1px solid #000000;
                text-align: right; padding: 6px 6px;">
                305
            </td>
            <td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-right: 1px solid #000000;
                text-align: right; padding: 6px 6px;">
                25
            </td>
            <td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-right: 1px solid #000000;
                text-align: right; padding: 6px 6px; font-weight: bold;">
                280
            </td>
        </tr>
    </table>
</div>

Step 5: - Create ReceiptWithModelTemplate.cshtml inside the Demo Folder i.e. reside in View Folder

@model MvcApplication1.Models.ReceiptModel
<style type="text/css">
    table
    {
        font-familyConsolas;
        font-size9pt;
    }
</style>
<div style="padding: 30px;">
    <p style="text-align: center;">
        <b>Report generated from ReceiptWithModelTemplate.cshtml</b></p>
    <br />
    <table style="border-collapse: collapse; width: 100%">
        <tr>
            <td style="border-top: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000;
                padding: 6px 6px;">
                <b>Bill To:</b>
            </td>
            <td style="border-top: 1px solid #000000; border-right: 1px solid #000000; padding: 6px 6px;">
                <b>Ship To:</b>
            </td>
        </tr>
        <tr>
            <td style="border-top: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000;
                padding: 6px 6px;">
                Name: @Model.Name
            </td>
            <td style="border-top: 1px solid #000000; border-right: 1px solid #000000; padding: 6px 6px;">
                Name: @Model.Name
            </td>
        </tr>
        <tr>
            <td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000;
                border-right: 1px solid #000000; padding: 6px 6px;">
                Address: @Model.Address
            </td>
            <td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-right: 1px solid #000000;
                padding: 6px 6px;">
                Address: @Model.Address
            </td>
        </tr>
    </table>
    <br />
    <table style="border-collapse: collapse; width: 100%">
        <tr>
            <th style="border-top: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000;
                text-align: center; padding: 6px 6px;">
                Sr No.
            </th>
            <th style="border-top: 1px solid #000000; border-right: 1px solid #000000; text-align: center;
                padding: 6px 6px;">
                Description
            </th>
            <th style="border-top: 1px solid #000000; border-right: 1px solid #000000; text-align: center;
                padding: 6px 6px;">
                Qty
            </th>
            <th style="border-top: 1px solid #000000; border-right: 1px solid #000000; text-align: center;
                padding: 6px 6px;">
                Gross Amount
            </th>
            <th style="border-top: 1px solid #000000; border-right: 1px solid #000000; text-align: center;
                padding: 6px 6px;">
                Discount Amount
            </th>
            <th style="border-top: 1px solid #000000; border-right: 1px solid #000000; text-align: center;
                padding: 6px 6px;">
                Net Amount
            </th>
        </tr>
        @{
   
            foreach (var item in Model.Items)
            {
            <tr>
                <td style="border-top: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000;
                    text-align: center; padding: 6px 6px;">
                    @item.SrNo
                </td>
                <td style="border-top: 1px solid #000000; border-right: 1px solid #000000; padding: 6px 6px;">
                    @item.Description
                </td>
                <td style="border-top: 1px solid #000000; border-right: 1px solid #000000; text-align: center;
                    padding: 6px 6px;">
                    @item.Qty
                </td>
                <td style="border-top: 1px solid #000000; border-right: 1px solid #000000; text-align: right;
                    padding: 6px 6px;">
                    @item.GrossAmount
                </td>
                <td style="border-top: 1px solid #000000; border-right: 1px solid #000000; text-align: right;
                    padding: 6px 6px;">
                    @item.DiscountAmount
                </td>
                <td style="border-top: 1px solid #000000; border-right: 1px solid #000000; text-align: right;
                    padding: 6px 6px;">
                    @item.NetAmount
                </td>
            </tr>
            }
        }
        <tr>
            <td colspan="3" style="border-top: 1px solid #000000; border-bottom: 1px solid #000000;
                border-left: 1px solid #000000; border-right: 1px solid #000000; text-align: right;
                padding: 6px 6px;">
                <b>Total:</b>
            </td>
            <td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-right: 1px solid #000000;
                text-align: right; padding: 6px 6px;">
                @Model.GrossAmountTotal
            </td>
            <td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-right: 1px solid #000000;
                text-align: right; padding: 6px 6px;">
                @Model.DiscountAmountTotal
            </td>
            <td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-right: 1px solid #000000;
                text-align: right; padding: 6px 6px; font-weight: bold;">
                @Model.NetAmountTotal
            </td>
        </tr>
    </table>
</div>

Output