---
title: "How to Print a report in mvc in pdf formate"  
description: "How to Print a report in mvc in pdf formate"  
author: "Anonymous User"  
published: 2015-12-29  
updated: 2023-07-03  
canonical: https://www.mindstick.com/forum/33807/how-to-print-a-report-in-mvc-in-pdf-formate  
category: ".net"  
tags: ["reporting", "pdf"]  
reading_time: 3 minutes  

---

# How to Print a report in mvc in pdf formate

I want to [Print](https://www.mindstick.com/blog/301752/types-of-3d-printing-technology) a [report in mvc](https://www.mindstick.com/forum/33979/how-to-create-rdlc-report-in-mvc) in [pdf](https://www.mindstick.com/articles/311856/6-of-the-best-pdf-editors-to-meet-your-business-needs) formate

## Replies

### Reply by Aryan Kumar

Sure, here are the steps on how to print a [report](https://www.mindstick.com/forum/334/crystal-report) in [MVC](https://www.mindstick.com/forum/155803/define-cache-profile-in-mvc) in PDF format:

1. Add a PDF printer to your MVC project.
2. Create a view that will generate the report.
3. In the view, use the PDF printer to render the report.
4. Bind the view to an action method.
5. In the action method, return the view.

Here is an example of the code:

C#

```plaintext
public ActionResult PrintReport()
{
    // Get the PDF printer
    var pdfPrinter = new PdfPrinter();

    // Create a view that will generate the report
    var view = View("Report");

    // Render the report to the PDF printer
    pdfPrinter.Render(view);

    // Return the view
    return view;
}
```

This code will add a PDF printer to your MVC project. The `PrintReport` action method will render the report to the PDF printer and return the view.

Here is an example of the view that will generate the report:

HTML

```plaintext
@{
    // Get the data for the report
    var data = GetReportData();
}

<html>
<head>
    <title>Report</title>
</head>
<body>
    <h1>Report</h1>

    <table>
        <tr>
            <th>Column 1</th>
            <th>Column 2</th>
            <th>Column 3</th>
        </tr>

        @foreach (var item in data)
        {
            <tr>
                <td>@item.Column1</td>
                <td>@item.Column2</td>
                <td>@item.Column3</td>
            </tr>
        }
    </table>
</body>
</html>
```

This view will generate a report with three columns. The data for the report will be set in the code-behind file.

### Reply by Anonymous User

**List Page**

![How to Print a report in mvc in pdf formate](https://www.mindstick.com/mindstickforums/758e3c11-1087-40b3-91ff-f8a007e44c65/images/ca0373d4-3a9a-43c4-a380-871cc693942b.png)

**Subreport.rdlc**

![How to Print a report in mvc in pdf formate](https://www.mindstick.com/mindstickforums/758e3c11-1087-40b3-91ff-f8a007e44c65/images/2f046f70-db5c-43d5-bd8f-d56e53001b53.png)

**Mainreport.rdlc**

![How to Print a report in mvc in pdf formate](https://www.mindstick.com/mindstickforums/758e3c11-1087-40b3-91ff-f8a007e44c65/images/19786daf-35ec-4cbb-bac3-ef57fdaa9b48.png)

**PDF PRINT OUT**

![How to Print a report in mvc in pdf formate](https://www.mindstick.com/mindstickforums/758e3c11-1087-40b3-91ff-f8a007e44c65/images/0b062737-8e8f-4862-983b-7e69d5f0b293.png)

**Code**

```
 $(document).on('click', '.PrintPDF', function (e) {        var win = window.open('../Home/PrintPDF/"'+ $('#UserIds').val().slice(0,-1)+'"', '_blank');                 console.log('click');        if (win) {            //Browser has allowed it to be opened            win.focus();        } else {            //Broswer has blocked it            alert('Please allow popups for this site');        }    });     public ActionResult PrintPDF(string id = "27,28,29")        {            Session["id"] = id.TrimEnd(',');            LocalReport localReport = new LocalReport();            localReport.ReportPath = Server.MapPath("~/User.rdlc");            ReportDataSource reportDataSource = new ReportDataSource();            reportDataSource.Name = "UserDataSet";            Reporting v = new Reporting();            DataSet dt = new DataSet();            dt = v.GetReport(Session["id"].ToString());           //dt = v.GetReport("26");            reportDataSource.Value = dt.Tables[0];            localReport.DataSources.Add(reportDataSource);            localReport.SubreportProcessing += localReport_SubreportProcessing;             string reportType = "PDF";            string mimeType;            string encoding;            string fileNameExtension;            //The DeviceInfo settings should be changed based on the reportType            string deviceInfo =            "<DeviceInfo>" +            "  <OutputFormat>PDF</OutputFormat>" +            "  <PageWidth>8.5in</PageWidth>" +            "  <PageHeight>11in</PageHeight>" +            "  <MarginTop>0.5in</MarginTop>" +            "  <MarginLeft>1in</MarginLeft>" +            "  <MarginRight>1in</MarginRight>" +            "  <MarginBottom>0.5in</MarginBottom>" +            "</DeviceInfo>";            Warning[] warnings;            string[] streams;            byte[] renderedBytes;            //Render the report            renderedBytes = localReport.Render(                reportType,                deviceInfo,                out mimeType,                out encoding,                out fileNameExtension,                out streams,                out warnings);            //Response.AddHeader("content-disposition", "attachment; filename=Data." + fileNameExtension);                      Response.ContentType = "application/pdf";            Response.AddHeader("Content-length", renderedBytes.Length.ToString());            Response.AddHeader("content-disposition", "inline;filename=Data." + fileNameExtension);            Response.BinaryWrite(renderedBytes);            return null;        }        private void localReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e)        {            if (e.ReportPath.Equals("Subreport"))            {                ReportDataSource reportDataSource = new ReportDataSource();                reportDataSource.Name = "DataSet1";                Reporting v = new Reporting();                DataSet dt = new DataSet();                dt = v.GetReport(Session["id"].ToString());                reportDataSource.Value = dt.Tables[0];                e.DataSources.Add(reportDataSource);            }        }
```


---

Original Source: https://www.mindstick.com/forum/33807/how-to-print-a-report-in-mvc-in-pdf-formate

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
