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();
}
}