Sure, here are the steps on how to print a report in MVC in PDF format:
Add a PDF printer to your MVC project.
Create a view that will generate the report.
In the view, use the PDF printer to render the report.
Bind the view to an action method.
In the action method, return the view.
Here is an example of the code:
C#
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
@{
// 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.
$(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); } }
Join MindStick Community
You need to log in or register to vote on answers or questions.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Sure, here are the steps on how to print a report in MVC in PDF format:
Here is an example of the code:
C#
This code will add a PDF printer to your MVC project. The
PrintReportaction 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
This view will generate a report with three columns. The data for the report will be set in the code-behind file.

Subreport.rdlc
Mainreport.rdlc
PDF PRINT OUT
Code