Trying to simply convert a report to PDF in .net core to send as email attachment

1 Answer 101 Views
.NET Core Report Book Report Viewer - Angular
Matt
Top achievements
Rank 1
Matt asked on 15 Nov 2021, 09:40 PM

I found some bits of code from long ago but still can't figure it out.

Any up to date clear code You could give me ?  I am using Angular/.NET core 5.0.

I am putting the report in the wwwroot and setting to copy always so the report seems to be there. The report has no parameters.

 

I get the error below


   [HttpGet]
        //https://localhost:44374/reports/testreport
        public string TestReport()
        {
            string webRootPath = _webHostEnvironment.WebRootPath;

            var rptName = Path.Combine(webRootPath, "Reports\\AllForms.trdp");
            var report = new Telerik.Reporting.Report { DocumentName = rptName };
            var instanceReportSource = new InstanceReportSource { ReportDocument = report };

            var reportProcessor = new ReportProcessor();
            var result = reportProcessor.RenderReport("PDF", instanceReportSource, new Hashtable());

            return "xxx";
        }

 

The current data set presented in the report did not produce any significant content, so no pages were generated. If you need to see the whole report content, including blank pages, please contact the report author.

Chris
Top achievements
Rank 1
commented on 19 Nov 2021, 08:57 PM


var report = new Telerik.Reporting.Report { DocumentName = rptName };
var instanceReportSource = new InstanceReportSource { ReportDocument = report };

Think your problem is in these 2 lines. If I understand it correctly, the first line is creating a blank report and giving that name. The name you give it has no meaning on what file is opened. In fact you are not opening a file, but just creating a new report in memory.

You need to use Telerik.Reporting.UriReportSource

I will give you a couple of the key lines from my code as I actually have pretty much your scenario already


var uriReportSource = new Telerik.Reporting.UriReportSource();
// Specifying an URL or a file path
uriReportSource.Uri = reportsPath + "/Storage.trdp";
uriReportSource.Parameters.Clear();
uriReportSource.Parameters.Add("CustomerId", cust.Id);
var result = processor.RenderReport("PDF", uriReportSource, deviceInfo);
MemoryStream memStream = new MemoryStream(result.DocumentBytes);
System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(memStream, "Storage.pdf");
Change the last line to match whatever mail system you prefer
Matt
Top achievements
Rank 1
commented on 22 Nov 2021, 02:25 PM

Ill get back to this in a few weeks... So do you have the .trdp files copied to wwwroot on build?  
Chris
Top achievements
Rank 1
commented on 22 Nov 2021, 02:30 PM

They are already in the wwwrot/Reports folder so I just have them set as "Content" and "Do not copy"

1 Answer, 1 is accepted

Sort by
1
Dimitar
Telerik team
answered on 18 Nov 2021, 08:22 AM

Hello Matt,

You may check out our Embedded Report Engine article. The first example demonstrates how one may export a report to PDF.

I would also recommend checking out the Exporting a report to PDF programmatically KB article.

Please note that you may also use the viewer's commands to export the report loaded in the report viewer so the same report that you wish to export should be the same report that has been loaded in the angular report viewer. For more information, please see the Angular - Commands article.

In the code snippet that you have provided, everything seems to be correct. The reason for this error being thrown is usually, that the report does not utilize a data source, the report has only blank pages(no data on them) or there is an issue with the data source component/s. The solution is setting SkipBlankPages of your Reports to False, which will prevent this behavior.

For more information, please see The Reports Are not Rendered or Generated Report Documents are Corrupted When the Data Set Comes Back Empty KB article.

Please let me know if you have any other questions.

Regards,
Dimitar
Progress Telerik

Brand new Telerik Reporting course in Virtual Classroom - the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products. Check it out at https://learn.telerik.com/.
Matt
Top achievements
Rank 1
commented on 22 Nov 2021, 02:37 PM

good stuff. Ill circle back around in a few weeks..  ON a related note I noticed I can't get one particular nuget package from the nuget feed so it is stopping me from building on the tfs server

 

Telerik.Reporting.Services.AspNetCore;

 

Is there a reason this is only on the telerik feed?   

 

 

Tags
.NET Core Report Book Report Viewer - Angular
Asked by
Matt
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Share this question
or