ASP.NET MVC 5 - AngularJS, Create PDF File Using Microsoft Report

Introduction

This article walks you through the steps of creating a report in Word or PDF format using Microsoft Report, without using Report Viewer.

STEP 1 - Create ASP.NET Web Application

Check the link below, to see all the steps of creating a Web API wtih Entity Framework code-first implementation.

http://social.technet.microsoft.com/wiki/contents/articles/26795.asp-net-webapi-entity-framework-code-first.aspx

STEP 2 - Install Microsoft Report Viewer Runtime into Machine

To have the ability to create reports into your project, you need to install the Runtime of Microsoft Report Viewer.

Follow the link below and download it according to your Visual Studio version:

http://www.microsoft.com/en-us/download/details.aspx?id=35747

ASP.NET

STEP 3 - Create Report

Add new item to your project of type Report.

For that, select Reporting option on the left menu and then select Report item as shown in the image below.

Let's call it "Contacts.rdlc".

ASP.NET

Create new datasource associated with your connection string defined in web.config.

Select "Next" button.

ASP.NET

In this demo, we will use table "Contacts" defined in our database.

ASP.NET

Rename the dataset to DataSetContacts, and press the OK option.

ASP.NET

After the creation of dataset, we need to design our report. For that, create a table with three columns, as in the image below.

This will display the Name, Address, and City of each contact that exists in the database table.

ASP.NET

STEP 4 - PDF Generate Class

Create new Controller called ReportControllers and have the GetPDFreport method:

C#

  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.Data.Entity;   
  4. using System.Data.Entity.Infrastructure;   
  5. using System.Linq;   
  6. using System.Net;   
  7. using System.Net.Http;   
  8. using System.Web.Http;   
  9. using SampleEF6.Models;   
  10. using System.Threading.Tasks;   
  11. using System.Web;   
  12. using System.IO;   
  13. using System.Net.Http.Headers;   
  14.    
  15. namespace SampleEF6.Controllers   
  16. {   
  17.     public class ReportController : ApiController   
  18.     {   
  19.         // GET api/<controller>   
  20.         [HttpGet]   
  21.         public async Task<HttpResponseMessage> GetPDFReport()   
  22.         {   
  23.             string fileName = string.Concat("Contacts.pdf");   
  24.             string filePath = HttpContext.Current.Server.MapPath("~/Report/" + fileName);   
  25.    
  26.             ContactController contact = new ContactController();   
  27.             List<Contact> contacList = contact.Get().ToList();   
  28.    
  29.             await SampleEF6.Report.ReportGenerator.GeneratePDF(contacList, filePath);   
  30.    
  31.             HttpResponseMessage result = null;   
  32.             result = Request.CreateResponse(HttpStatusCode.OK);   
  33.             result.Content = new StreamContent(new FileStream(filePath, FileMode.Open));   
  34.             result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");   
  35.             result.Content.Headers.ContentDisposition.FileName = fileName;   
  36.    
  37.             return result;   
  38.         }   
  39.     }   
  40. }   

Create the GeneratePDF method like this.

C#

  1. using Microsoft.Reporting.WebForms;   
  2. using SampleEF6.Models;   
  3. using System;   
  4. using System.Collections.Generic;   
  5. using System.IO;   
  6. using System.Linq;   
  7. using System.Reflection;   
  8. using System.Threading.Tasks;   
  9. using System.Web;   
  10.    
  11. namespace SampleEF6.Report   
  12. {   
  13.     public class ReportGenerator   
  14.     {   
  15.         public static string Report = "SampleEF6.Report.Contacts.rdlc";   
  16.    
  17.         public static Task GeneratePDF(List<Contact> datasource, string filePath)   
  18.         {   
  19.             return Task.Run(() =>   
  20.             {   
  21.                 string binPath = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "bin");   
  22.                 var assembly = Assembly.Load(System.IO.File.ReadAllBytes(binPath + "\\SampleEF6.dll"));   
  23.    
  24.                 using (Stream stream = assembly.GetManifestResourceStream(Report))   
  25.                 {   
  26.                     var viewer = new ReportViewer();   
  27.                     viewer.LocalReport.EnableExternalImages = true;   
  28.                     viewer.LocalReport.LoadReportDefinition(stream);   
  29.    
  30.                     Warning[] warnings;   
  31.                     string[] streamids;   
  32.                     string mimeType;   
  33.                     string encoding;   
  34.                     string filenameExtension;   
  35.    
  36.                     viewer.LocalReport.DataSources.Add(new ReportDataSource("DataSetContacts", datasource));   
  37.    
  38.                     viewer.LocalReport.Refresh();   
  39.    
  40.                     byte[] bytes = viewer.LocalReport.Render(   
  41.                         "PDF"null, out mimeType, out encoding, out filenameExtension,   
  42.                         out streamids, out warnings);   
  43.    
  44.                     using (FileStream fs = new FileStream(filePath, FileMode.Create))   
  45.                     {   
  46.                         fs.Write(bytes, 0, bytes.Length);   
  47.                     }   
  48.                 }   
  49.             });   
  50.         }   
  51.     }   
  52. }   

STEP 5 - Run Application

ASP.NET

Resources

Some good resources about Windows Azure can be found here,

  • My personal blog: http://joaoeduardosousa.wordpress.com/


Similar Articles