Thiyagarajan E

Thiyagarajan E

  • 1.1k
  • 866
  • 42.6k

Export to pdf from fastreport in mvc asp .net

Sep 27 2019 5:22 AM
Hi,
 
Please add the below code in the controller.
 
 
  1. private void CreateDataSet()  
  2. {  
  3.     // create simple dataset with one table  
  4.     FDataSet = new DataSet();  
  5.   
  6.     DataTable table = new DataTable();  
  7.     table.TableName = "Employees";  
  8.     FDataSet.Tables.Add(table);  
  9.   
  10.     table.Columns.Add("ID"typeof(int));  
  11.     table.Columns.Add("Name"typeof(string));  
  12.   
  13.     table.Rows.Add(1, "Andrew Fuller");  
  14.     table.Rows.Add(2, "Nancy Davolio");  
  15.     table.Rows.Add(3, "Margaret Peacock");  
  16. }  
  17.   
  18.   
  19. public void SaveMemoryStream(MemoryStream ms, string FileName)  
  20. {  
  21.     FileStream outStream = System.IO.File.OpenWrite(FileName);  
  22.     ms.WriteTo(outStream);  
  23.     outStream.Flush();  
  24.     outStream.Close();  
  25. }  
  26.   
  27.   
  28. [HttpPost]  
  29. public JsonResult GetFile()  
  30. {  
  31.     FastReport.Utils.Config.WebMode = true;  
  32.     Report rep = new Report();  
  33.     rep.Load(Request.PhysicalApplicationPath + "App_Data/report.frx");  
  34.   
  35.     CreateDataSet();  
  36.   
  37.     rep.RegisterData(FDataSet);  
  38.   
  39.     if (rep.Report.Prepare())  
  40.     {  
  41.         // Set PDF export props  
  42.         FastReport.Export.Pdf.PDFExport pdfExport = new FastReport.Export.Pdf.PDFExport();  
  43.         pdfExport.ShowProgress = false;  
  44.         pdfExport.Subject = "Subject";  
  45.         pdfExport.Title = "xxxxxxx";  
  46.         pdfExport.Compressed = true;  
  47.         pdfExport.AllowPrint = true;  
  48.         pdfExport.EmbeddingFonts = true;  
  49.   
  50.         MemoryStream strm = new MemoryStream();  
  51.         rep.Report.Export(pdfExport, strm);  
  52.         rep.Dispose();  
  53.         pdfExport.Dispose();  
  54.         strm.Position = 0;  
  55.   
  56.         string dateTime = DateTime.Now.ToString("yyyyddmmhhMMss") + ".pdf";  
  57.   
  58.         string filePath = Server.MapPath("\\Content\\Downloads\\");  
  59.   
  60.         if (!Directory.Exists(filePath))  
  61.         {  
  62.             Directory.CreateDirectory(filePath);  
  63.         }  
  64.   
  65.   
  66.         SaveMemoryStream(strm, filePath + dateTime);  
  67.   
  68.         //FileStream file = new FileStream("\\Content\\Downloads\\", FileMode.Create, FileAccess.Write);  
  69.         //strm.WriteTo(file);  
  70.         //file.Close();  
  71.   
  72.         // return stream in browser  
  73.         return Json(filePath + dateTime);  
  74.     }  
  75.     else  
  76.     {  
  77.         return null;  
  78.     }  
  79. }  
 
Thanks....