This is a migrated thread and some comments may be shown as answers.

How to generate PDF from Standalone report?

1 Answer 820 Views
Report Designer (standalone)
This is a migrated thread and some comments may be shown as answers.
LTA
Top achievements
Rank 1
LTA asked on 28 Oct 2020, 05:02 PM

Hi,

 

I have created one Standalone report with an Object Person (Which contains properties Name, Number and DOB).

 

Then, I'm using below code to generate PDF from this report.

 

            var reportProcessor = new ReportProcessor();
            var deviceInfo = new System.Collections.Hashtable();

            var reportSource1 = new UriReportSource();
            reportSource1.Uri = "/report.trdp";
            var reportSource = new InstanceReportSource();
            var report = new Telerik.Reporting.Report();
            var objectDataSource1 = new Telerik.Reporting.ObjectDataSource();
            objectDataSource1.DataSource = new Person() { DOB = DateTime.Now, Number = 1293737, Name = "test" };
            objectDataSource1.Name = "PersonDataSet";
            report.DataSource = objectDataSource1;
            reportSource.ReportDocument = report;
            reportSource.ReportDocument.ReportSources.Append(reportSource1);

            RenderingResult result = reportProcessor.RenderReport("PDF", reportSource, deviceInfo);

            string fileName = result.DocumentName + "." + result.Extension;
            string path = System.IO.Path.GetTempPath();
            string filePath = System.IO.Path.Combine(path, fileName);

            using (System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Create))
            {
                fs.Write(result.DocumentBytes, 0, result.DocumentBytes.Length);
            }
        }

 

But, It generates only empty report.

 

Can anyone help me to resolve this?

 

Thanks,

Aravindh 

1 Answer, 1 is accepted

Sort by
0
Mads
Telerik team
answered on 02 Nov 2020, 12:46 PM

Hello, Aravindh 

Attached to this reply you will find a sample project where I have tried re-creating what you are trying to achieve with your code, but a few changes has been made to make it work:

  1. We created a Standalone report with 3 simple textboxes with the values Fields.Name, Fields.DOB and Fields.Number. The report does not need to be based on any Object Data Source in this solution.
  2. In the code, we read the TRDP report-definition and then apply the DataSource to this report.

I believe you have read and used "Embedded Report Engine" article-examples to create the code which you sent us. Instead of using any of the 3 suggested report sources, we used ReportPackager to read the TRDP file and convert it with the following code:

var reportPackager = new ReportPackager();
var reportSource = new InstanceReportSource();
using (var sourceStream = System.IO.File.OpenRead("../../SampleReport.trdp"))
{
    var report = (Report)reportPackager.UnpackageDocument(sourceStream);
    report.DataSource = new Person("Test Person", 24242424, DateTime.Now);
    reportSource.ReportDocument = report;
}

Then using the rest of the "Embedded Report Engine" example to create RenderingResult and write the file.

I hope you find this information useful. If this does not achieve what you are aiming for, or if you have any other questions, please do not hesitate to contact us.

Regards, Mads Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
Report Designer (standalone)
Asked by
LTA
Top achievements
Rank 1
Answers by
Mads
Telerik team
Share this question
or