Create a PDF file on server using FastReport.NET

I am using FastReport.NET version 2021.4.8. I would like to create a report PDF file, give it a name, and save it to the Temp folder on the server. This will allow me to pop it open to display to the user. Can this be done and if so, can you give me some guidance on how to code it?

Thanks,

Ramon Weston

Comments

  • What I have been doing is to create the Report on the server using a WebReport object and then use PDFSimple (FastReport.OpenSource.Export.PdfSimple) to create and save the PDF file to the server drive.


            WebReport webReport = GetWebReport(ReportData, reportDef);

            _webReport.Report.Prepare();

            using (MemoryStream ms = new MemoryStream())

            {

              PDFSimpleExport pdfExport = new PDFSimpleExport();

              pdfExport.Export(_webReport.Report, ms);


              string dir_path = <<YOUR PATH>>;

              bool exists = System.IO.Directory.Exists(dir_path);

              if (!exists)

              {

                Logger.Information($"Creating directory {dir_path}");

                // This throws an exception if it fails

                System.IO.Directory.CreateDirectory(dir_path);

              }


              string filename = Path.Combine(dir_path, FileName ?? $"{ReportName}.pdf");

              exists = System.IO.File.Exists(filename);

              using (FileStream file = new FileStream(filename, FileMode.Create, FileAccess.Write))

              {

                ms.WriteTo(file);

                file.Close();

                ms.Flush();

              }

            }

  • Hello!

    Yes, you can generate a PDF with any name and save it to the server’s temporary folder (or send it directly to the client from memory); in fact, you don’t even need to create a WebReport to generate the PDF:

    using FastReport;
    using FastReport.Export.Pdf;
    
    using var report = new Report();
    report.Load(templatePath);
    report.Prepare();
    
    string tempPdf = Path.Combine(Path.GetTempPath(), $‘Report_{Guid.NewGuid():N}.pdf’);
    using (var pdf = new PDFExport { ShowProgress = false })
    report.Export(pdf, tempPdf);
    

    If you are already working with WebReport, call it in the same way: webReport.Report.Prepare(); webReport.Report.Export(pdf, tempPdf); (or export directly to a MemoryStream and output via File(bytes, ‘application/pdf’) — this way you won’t have to clear temporary files from the disk).

This discussion has been closed.