create pdf from Report Template designed be using Standalone designer in Telerik Reporting in .net core

2 posts, 0 answers
  1. Sameer
    Sameer avatar
    9 posts
    Member since:
    Jul 2019

    Posted 08 Aug Link to this post

    // following example to create pdf from report template which created using standalone designer template

    public class ReportsController : ReportsControllerBase
     {
         readonly string reportsPath = string.Empty;
            
         private IHostingEnvironment _hostingEnvironment;
            

        public ReportsController(ConfigurationService configSvc)
        {
            this.reportsPath = Path.Combine("PathWhereReportTemplatesStored", "Reports"); //concatenate the path using the OS path delimiter.
            
            this.ReportServiceConfiguration = new ReportServiceConfiguration
            {
                ReportingEngineConfiguration = configSvc.Configuration,
                HostAppId = "Html5DemoAppCore",
                Storage = new FileStorage(),
                ReportResolver = new ReportTypeResolver()
                                    .AddFallbackResolver(new ReportFileResolver(this.reportsPath)),
                
            };
        }

        [HttpGet("reportlist")]
        public IEnumerable<string> GetReports()
        {
            return Directory
                .GetFiles(this.reportsPath)
                .Select(path =>
                    Path.GetFileName(path));
        }
             
        //https://www.telerik.com/support/kb/reporting/styling-and-formatting-reports/details/exporting-a-report-to-pdf-programmatically
        [HttpGet]
        public IActionResult createPdf()
        {
            string fileName = "report_" + DateTime.Now.Ticks + ".pdf";

            // https://docs.telerik.com/reporting/report-sources-viewers
            var uriReportSource = new Telerik.Reporting.UriReportSource();
            
            // Specifying an URL or a file path
            uriReportSource.Uri = reportsPath + "\\reportTemplate.trdp";    // report template designed using standalone report designer
            
            // Optional, pass param values to report, Adding the initial parameter values
            uriReportSource.Parameters.Add(new Telerik.Reporting.Parameter("param1", "value"));
            uriReportSource.Parameters.Add(new Telerik.Reporting.Parameter("param2", "value"));

            ReportProcessor reportProcessor = new ReportProcessor();

            RenderingResult result = reportProcessor.RenderReport("PDF", uriReportSource, null);     

            // to write pdf on disc, omit if not required
            using (FileStream fs = new FileStream("PathWhereYouWantToWritePdfOnDisk" + "pdfName.pdf", FileMode.Create))
            {
                fs.Write(result.DocumentBytes, 0, result.DocumentBytes.Length);
            }    

            return File(result.DocumentBytes, "application/pdf", fileName);    // in case , user clicks on link and download report
        }

     }

  2. Silviya
    Admin
    Silviya avatar
    414 posts

    Posted 13 Aug Link to this post

    Hi Sameer,

    From my understanding, you want to export the report in PDF format programmatically (without using the report viewer) from a .NET Core application. 
    In general, to export a report, you can use the RenderReport method of the ReportProcessor class. This method converts the contents of the report to a byte array in the specified format, which you can then use with other classes such as MemoryStream or FileStream. For more information, please refer to Exporting a report to a single document format help article.

    The provided code snippet seems to be correct. May I ask you to provide us more information about the issues you have. Are there any errors? Additionally, you might open a new support ticket and send us the problematic project to check its settings.

    Best Regards, Silviya
    Progress Telerik

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top