ReportingController.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. 
  2. using Microsoft.AspNetCore.Mvc;
  3. using DevExpress.XtraReports.UI;
  4. using DevExpress.XtraReports.Web.Extensions;
  5. using System.IO;
  6. namespace MTWorkHR.API.Controllers
  7. {
  8. [ApiController]
  9. [Route("api/[controller]")]
  10. public class ReportController : ControllerBase
  11. {
  12. private readonly ReportStorageWebExtension _reportStorage;
  13. public ReportController(ReportStorageWebExtension reportStorage)
  14. {
  15. _reportStorage = reportStorage;
  16. }
  17. [HttpGet("generate-pdf")]
  18. public async Task<IActionResult> GeneratePdf(string reportUrl)
  19. {
  20. byte[] reportData = _reportStorage.GetData(reportUrl);
  21. // Load the report from the byte array
  22. using (MemoryStream ms = new MemoryStream(reportData))
  23. {
  24. XtraReport report = XtraReport.FromStream(ms, true);
  25. using (MemoryStream pdfStream = new MemoryStream())
  26. {
  27. try
  28. {
  29. if (report.DataSource == null)
  30. {
  31. var m = "The report's DataSource is null.";
  32. }
  33. await report.CreateDocumentAsync();
  34. await report.ExportToPdfAsync(pdfStream);
  35. }
  36. catch(Exception e)
  37. {
  38. var mess = e.Message;
  39. }
  40. return File(pdfStream.ToArray(), "application/pdf", "GeneratedReport.pdf");
  41. }
  42. }
  43. }
  44. }
  45. }