Search code examples
rdlcasp.net-core-3.0asp.net-core-3.1

Render RDLC as PDF or Excel in Aspnet Core


I heard of rendering RDLC being impossible (or not easy) in aspnet core 2.

Is it possible to render an RDLC as PDF or Excel in Aspnet Core 3.0 or 3.1 ?

PS: But I have a working code in Aspnet MVC 5 targeting .Net Framework.


Solution

  • Found a solution and I post this for using the following packages

    <PackageReference Include="AspNetCore.Reporting" Version="2.1.0" /> 
    <PackageReference Include="System.CodeDom" Version="4.7.0" />
    <PackageReference Include="System.Data.SqlClient" Version="4.8.2" />
    <PackageReference Include="System.Drawing.Common" Version="4.7.0" />
    <PackageReference Include="System.Text.Encoding.CodePages" Version="4.7.1" />
    

    but still, for the report designer to work, I had to create an .net standard library project, where I added the .rdcl file for design purpose.

    After that I moved final file a Reports directory (not inside wwwroot) and I could be able to generate a PDF file using the following code (in asp.net core 3.1)

        string rdlcFilePath = Path.Combine(hostEnvironment.ContentRootPath, 
              "Reports", "UserDetails.rdlc");
            // return Ok(rdlcFilePath);
            Dictionary<string, string> parameters = new Dictionary<string, string>();
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
            Encoding.GetEncoding("windows-1252");
            LocalReport report = new LocalReport(rdlcFilePath);
    
            List<UserDto> userList = getUserList;
            report.AddDataSource("dsUsers", userList);
            var result = report.Execute(GetRenderType("pdf"), 1, parameters);
            return File(result.MainStream, 
                   System.Net.Mime.MediaTypeNames.Application.Octet,
                   "users.pdf");
    

    Now you can put an href to a element to download the file or you can receive it as a blob if you want inside your angular service.

    Thank you!