Rendering .rdlc reports with ASP .NET Core

2019-07-30 03:04发布

Is it possible to render .rdlc reports with ASP.NET Core? Currently this only seems to be possible if I target the .NET Framework as opposed to .NET Core.

I don't need a report viewer I just need to render the results of an .rdlc report as a byte array.

2条回答
等我变得足够好
2楼-- · 2019-07-30 03:26

You very well can render rdlc into a byte array. Please see a related question I asked a while back. RDLC Local report viewer for ASP.NET Core and Angular(>2.0).

Eventually a creative discussion on that thread resulted in an angular package(https://www.npmjs.com/package/ng2-pdfjs-viewer - Disclosure; I am the author) with consumable rdlc byte array functionality on client side. Of course, instead of this package, you may choose another javascript library to display the byte array.

A simple usage on angular would be like this. Please note, most of the code can be reused even if you are using plain js or another framework.

The below code demonstrates

1. Spitting byte array using RDLC report viewer control on aspnet core action method(on server side) and sending it over wire using http. (Code is in C#)
2. Processing response's byte array into a blob object (Js)
3. Feeding blob object into ng2-pdfjs-viewer.
4. ng2-pdfjs-viewer internally uses Mozilla's PDFJS to accomplish the feat of displaying the PDF on browser.
(FYI.. I took code from samples provided on ng2-pdfjs-viewer package. Replace step 3 and 4 if you are using another library or plain javascript)

<!-- your.component.html -->
<button (click)="showPdf();">Show</button>
<div style="width: 800px; height: 400px">
  <ng2-pdfjs-viewer #pdfViewer></ng2-pdfjs-viewer>
</div>

export class MyComponent implements OnInit {
  @ViewChild('pdfViewer') pdfViewer
  ...

  private downloadFile(url: string): any {
    return this.http.get(url, { responseType: ResponseContentType.Blob }).map(
      (res) => {
        return new Blob([res.blob()], { type: "application/pdf" });
      });
  }

  public showPdf() {
    let url = "http://localhost/api/GetMyPdf";
    this.downloadFile(url).subscribe(
    (res) => {
        this.pdfViewer.pdfSrc = res; // <---- pdfSrc can be Blob or Uint8Array
        this.pdfViewer.refresh(); // Ask pdf viewer to load/reresh pdf
      }
    );
  }

[HttpGet]
[Route("MyReport")]
public IActionResult GetReport()
{
   var reportViewer = new ReportViewer {ProcessingMode = ProcessingMode.Local};
   reportViewer.LocalReport.ReportPath = "Reports/MyReport.rdlc";

   reportViewer.LocalReport.DataSources.Add(new ReportDataSource("NameOfDataSource1", reportObjectList1));
   reportViewer.LocalReport.DataSources.Add(new ReportDataSource("NameOfDataSource2", reportObjectList1));

   Warning[] warnings;
   string[] streamids;
   string mimeType;
   string encoding;
   string extension;

   var bytes = reportViewer.LocalReport.Render("application/pdf", null, out mimeType, out encoding, out extension, out streamids, out warnings);

   return File(bytes, "application/pdf")
}
查看更多
Anthone
3楼-- · 2019-07-30 03:52

You can not render .rdlc reports in .NET Core, using .rdlc as a byte array.

查看更多
登录 后发表回答