Search code examples
c#asp.net-mvcfastreport

Export a pdf using FastReport.net and asp.net


How is it possible to export a pdf using FastReport.net and asp.net? I would like to export the File in a Controller. I tried it this way supported on the FastReport Website:

public FileResult GetFile()
        {
            WebReport webReport = new WebReport();

            // bind data
            System.Data.DataSet dataSet = new System.Data.DataSet();
            dataSet.ReadXml(report_path + "nwind.xml");
            webReport.Report.RegisterData(dataSet, "NorthWind");

            // load report
            webReport.ReportFile = this.Server.MapPath("~/App_Data/report.frx");

            // prepare report
            webReport.Report.Prepare();

            // save file in stream
            Stream stream = new MemoryStream();
            webReport.Report.Export(new PDFExport(), stream);
            stream.Position = 0;

            // return stream in browser
            return File(stream, "application/zip", "report.pdf");
        }

but then the size of the pdf is always 0 bytes.

Does someone know a solution to my problem?


Solution

  • Ok, now I found a solution. Just use the normal Report (not WebReport) and set WebMode to true. The other settings on pdf-Export are just for fun.

    So, this will do the trick:

    public FileResult GetFile(Dataset dataset1)
    {
        FastReport.Utils.Config.WebMode = true;
        Report rep = new Report();
        rep.Load(Request.PhysicalApplicationPath + "App_Data/report.frx");    
    
        rep.RegisterData(dataset1);
    
        if (rep.Report.Prepare())
        {
            // Set PDF export props
            FastReport.Export.Pdf.PDFExport pdfExport = new FastReport.Export.Pdf.PDFExport();
            pdfExport.ShowProgress = false;
            pdfExport.Subject = "Subject";
            pdfExport.Title = "xxxxxxx";
            pdfExport.Compressed = true;
            pdfExport.AllowPrint = true;
            pdfExport.EmbeddingFonts = true;
    
            MemoryStream strm = new MemoryStream();
            rep.Report.Export(pdfExport, strm);
            rep.Dispose();
            pdfExport.Dispose();
            strm.Position = 0;
    
            // return stream in browser
            return File(strm, "application/pdf", "report.pdf");
        }
        else
        {
            return null;
        } 
    }
    

    It's a pity that such code templates are wrong on the official site of the developer.