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

Generate PDF from Asp.Net Core

19 Answers 520 Views
This is a migrated thread and some comments may be shown as answers.
Henrique Duarte
Top achievements
Rank 1
Veteran
Henrique Duarte asked on 30 Jan 2019, 08:21 AM

How can I generate PDF from a .Net Core WebApplication?
I'm using the following code:

var reportProcessor = new Telerik.Reporting.Processing.ReportProcessor();
var uriReportSource = new Telerik.Reporting.UriReportSource();
 
uriReportSource.Uri = "Report.tdrp";
 
var result = reportProcessor.RenderReport("PDF", uriReportSource, null);
 
var output =  result.DocumentBytes;
 
return File(output, "application/pdf");

 

But it gives me an exception:

System.BadImageFormatException: Could not load file or assembly 'System.Windows.Forms'

19 Answers, 1 is accepted

Sort by
0
Silviya
Telerik team
answered on 01 Feb 2019, 03:09 PM
Hi Henrique,

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, t
o 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.

As of R1 2019, Telerik Reporting engine can be integrated in pure .NET Core projects - .NET Core Support.

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
0
Hossein
Top achievements
Rank 1
answered on 21 Aug 2019, 11:03 PM

Hi Silviya

Can you please provide a sample code for Rendering a Report as PDF problematically ?  (for Asp.Net Core) 

 

0
Silviya
Telerik team
answered on 26 Aug 2019, 10:44 AM

Hi Hossein,

The previously linked article contains a code snippet how to export reports programmatically - Embedded Report Engine. You can create a method in your application which will produce the PDF document.

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
0
Mario
Top achievements
Rank 1
answered on 15 Jan 2020, 07:28 PM

The code you posted works perfectly for ASP.NET applications, but it doesn't work for ASP.NET CORE applications.
Here is the exception:

FileLoadException: Could not load file or assembly 'System.Configuration.ConfigurationManager, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The located assembly's manifest definition does not match the assembly reference. (0x80131040)
Telerik.Reporting.Processing.RenderingExtensionManager.get_RenderingExtensions() in RenderingExtensionManager.cs, line 165

Can you tell me if there is any sample code for ASP.NET CORE applications?

0
Todor
Telerik team
answered on 20 Jan 2020, 11:40 AM

Hi Mario,

You need to provide the configuration settings from the 'appsettings.json' file to the ReportProcessor - check How to pass configuration settings to ReportProcessor in ASP.NET Core application that does not use REST Service KB article. You will have to use the ReportProcessor constructor overload that takes this configuration as a parameter. This constructor is available only for .NET Core applications.

Regards,
Todor
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
0
Andrea
Top achievements
Rank 1
Veteran
answered on 06 May 2020, 05:31 PM

Hi Todor,

I don't have parameters in ReportProcessor constructor. I'm using reporting 2019 R3.

I have a net core 3.1 webapi that reference a .net 4.7.2 class library with inside reports and a method for export reports in pdf, but on RenderReport() I have same error: Could not load file or assembly 'System.Configuration.ConfigurationManager, Version=4.0.3.0

I tried call the export method from a net core 3.1 winforms application and it works fine.

Thank you very much.

 

0
Andrea
Top achievements
Rank 1
Veteran
answered on 06 May 2020, 05:50 PM
Ok sorry, I moved the exporter method on the webapi and referenced telerik.reporting.dll under netstandard2.0 forlder. Now I have constructor with configuration parameter.
0
Andrea
Top achievements
Rank 1
Veteran
answered on 08 May 2020, 12:42 PM

 

[quote]Todor said:

Hi Mario,

You need to provide the configuration settings from the 'appsettings.json' file to the ReportProcessor - check How to pass configuration settings to ReportProcessor in ASP.NET Core application that does not use REST Service KB article. You will have to use the ReportProcessor constructor overload that takes this configuration as a parameter. This constructor is available only for .NET Core applications.

Regards,
Todor
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

[/quote]

 

If I pass configuration to ReportProcessor and the report has NeedDataSource event, can I access to configuration (connection string) from report Constructor or NeedDataSource event handler or Processing.Report object ?

Thank you

0
Todor
Telerik team
answered on 11 May 2020, 08:32 AM

Hello,

Regards,
Todor
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
0
MIS
Top achievements
Rank 1
answered on 11 Aug 2020, 09:48 PM

Hello, did you resolve the error ?, I got the same about ConfigurationManager. I did the steps on the post but no luck. I am using .Net Core 3.1 API project.

Thanks 

0
Todor
Telerik team
answered on 14 Aug 2020, 01:54 PM

Hello,

The sample code in the KB article that is referenced in the thread is valid for .NET Core 2 and the older Reporting versions. However, the general approach is the same - you need to pass the configuration to the ReportProcessor constructor. For example, you may read the appsettings.json configuration file with the helper class we have described in How to Host Reports Service in ASP.NET Core 3+ / Add Configuration Settings and then use this helper to pass the configuration to the Reporting engine. For example, in the HomeController of an ASP.NET Core 3.1 MVC application:

private IWebHostEnvironment webHostEnvironment; public HomeController(IWebHostEnvironment webHostEnvironment) { this.webHostEnvironment = webHostEnvironment;

... } ... public FileContentResult PrintInvoice(Product products) { ... InstanceReportSource instanceReportSource = new InstanceReportSource() { ReportDocument = report }; var configuration = ConfigurationHelper.ResolveConfiguration(webHostEnvironment); ReportProcessor reportProcessor = new ReportProcessor(configuration); RenderingResult result = reportProcessor.RenderReport("PDF", instanceReportSource, deviceInfo); return File(result.DocumentBytes, "application/pdf"); }

 

Regards,
Todor
Progress Telerik

0
David
Top achievements
Rank 1
answered on 05 Sep 2020, 08:57 PM

I'm doing the steps for the AspNetCore 3.0 and passing the IConfiguration generated from the IWebHostEnvironment to the ReportProcessor constructor. Still getting the error "Could not load file or assembly 'System.Configuration.ConfigurationManager, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.". This happens on the RenderReport line. 

I'm in a dotnet core 3.0 project referencing a Net Framework 4.7 Report Library and I'm trying to generate a PDF programatically.

var reportProcessor = new ReportProcessor(_configuration);
var instanceReportSource = new InstanceReportSource();
var monthlyInvoiceReport = new MemberInvoice();
monthlyInvoiceReport.DataSource = viewModel;
instanceReportSource.ReportDocument = monthlyInvoiceReport;
var result = reportProcessor.RenderReport("PDF", instanceReportSource, null);

0
Todor
Telerik team
answered on 09 Sep 2020, 04:25 PM

Hello David,

I suspect that the problem is in the mismatch of the frameworks, i.e. instantiating a .NET class in a .NET Core 3 project.

You may add the CS report in a .NET Core ClassLibrary project and try to use the report from this project in your main .NET Core project.

Regards,
Todor
Progress Telerik

Five days of Blazor, Angular, React, and Xamarin experts live-coding on twitch.tv/CodeItLive , special prizes and more, for FREE?! Register now for DevReach 2.0(20).

0
David
Top achievements
Rank 1
answered on 09 Sep 2020, 04:54 PM

Do you happen to have some articles that you can point me to that would allow me to do this in a .NET Core 3 project? 

1. Report with data as an ObjectDataSource. The ViewModel object is defined in the Report Class Library project.

2. Generate report as a PDF and return a FileStream so that I can store it on the file system to be retrieved at a later date?

When you say "add the CS report", what do you mean exactly? Copy the report from the full .NET class library project to the .NET core project and try to render it? I can try but the error happens on the ReportProcessor.RenderReport method, not exactly sure that will help.

0
Todor
Telerik team
answered on 14 Sep 2020, 12:01 PM

Hi David,

I have attached a sample solution with a .NET Standard 2.1. ClassLibrary project hosting an ObjectDataSource and a CS report, and a .NET Core 3.1 Web project utilizing the ReportProcessor for export. There are two endpoints that export reports:

  • Invoice/TrdpInvoice for the TRDP version that is in the WebCore31/Reports folder
  • Invoice/CsInvoice for the CSharp version that is in the ClassLibrary project

Indeed, you may include an already designed CS report in a .NET Core/Standard library and build it against the corresponding framework. Note that the ObjectDataSource should also be compiled with the same or compatible framework.

Regards,
Todor
Progress Telerik

Five days of Blazor, Angular, React, and Xamarin experts live-coding on twitch.tv/CodeItLive , special prizes and more, for FREE?! Register now for DevReach 2.0(20).

0
David
Top achievements
Rank 1
answered on 14 Sep 2020, 03:19 PM
Thank you very much for the provided sample. I will give it a try.
0
Alex
Top achievements
Rank 1
answered on 11 Nov 2020, 02:03 AM

I tried your zip file and when RenderReport executes I get the following error:

 

System.TypeLoadException: 'Could not load type 'Telerik.Reporting.HtmlRendering2.HtmlReport' from assembly 'Telerik.Reporting, Version=14.1.20.618, Culture=neutral, PublicKeyToken=a9d7983dfcc261be'

0
Alex
Top achievements
Rank 1
answered on 11 Nov 2020, 10:44 PM

Todor,

I tried your sample as is and I get an exception saying: System.TypeLoadException: 'Could not load type 'Telerik.Reporting.HtmlRendering2.HtmlReport' from assembly 'Telerik.Reporting, Version=14.1.20.618, Culture=neutral, PublicKeyToken=a9d7983dfcc261be'

0
Todor
Telerik team
answered on 13 Nov 2020, 03:59 PM

Hello Alex,

Here is a short silent video demonstrating how the report works on our end after I downloaded it from the thread - .NET Core 3.1 demo.

Note that the project references the NuGet package Telerik.Reporting version 14.1.20.618. You need to make sure the package can be downloaded when building from our private NuGet repository or from a local repository. If you have the Trial or another version of Telerik Reporting, you may need to change/upgrade the package to the version that you have.

Regards,
Todor
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/.

Asked by
Henrique Duarte
Top achievements
Rank 1
Veteran
Answers by
Silviya
Telerik team
Hossein
Top achievements
Rank 1
Mario
Top achievements
Rank 1
Todor
Telerik team
Andrea
Top achievements
Rank 1
Veteran
MIS
Top achievements
Rank 1
David
Top achievements
Rank 1
Alex
Top achievements
Rank 1
Share this question
or