
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'
18 Answers, 1 is accepted
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.
As of R1 2019, Telerik Reporting engine can be integrated in pure .NET Core projects - .NET Core Support.
Regards,
Silviya
Progress Telerik

Hi Silviya
Can you please provide a sample code for Rendering a Report as PDF problematically ? (for Asp.Net Core)
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

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?
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

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.


[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
[/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

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
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

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);
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).

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.
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).


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'

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'
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/.
Can you please provide a sample code for Rendering a Report as PDF problematically ? (for Asp.Net Core)
I am trying to render the report programmatically. but getting the exception
Error: Internal Server Error Response bodyDownload
|
Hi MANAMOHAN,
The error seems related to the .NET Framework WinForms assembly. It is not clear why the project looks for it. It is definitely not expected for a .NET Core project.
Can you specify how you have referenced the Telerik Reporting assembly? Directly, as a reference, or through a Nuget package? Make sure you have referenced the assembly for .NET Standard.
I don't know if its the same problem, but I have this error in a Asp.Net Core when attempting to deserialize a trdx file that contains a picture box.
```
System.Private.CoreLib.dll!System.Reflection.RuntimeMethodInfo.get_Signature.__LazyCreateSignature|24_0() Unknown
System.Private.CoreLib.dll!System.Reflection.RuntimeMethodInfo.FetchNonReturnParameters() Unknown
System.Private.CoreLib.dll!System.Reflection.RuntimePropertyInfo.GetIndexParametersNoCopy() Unknown
System.Private.CoreLib.dll!System.Reflection.RuntimePropertyInfo.GetIndexParameters() Unknown
System.ComponentModel.TypeConverter.dll!System.ComponentModel.ReflectTypeDescriptionProvider.ReflectGetProperties(System.Type type) Unknown
System.ComponentModel.TypeConverter.dll!System.ComponentModel.ReflectTypeDescriptionProvider.ReflectedTypeData.GetProperties() Unknown
System.ComponentModel.TypeConverter.dll!System.ComponentModel.TypeDescriptor.TypeDescriptionNode.DefaultTypeDescriptor.System.ComponentModel.ICustomTypeDescriptor.GetProperties() Unknown
System.ComponentModel.TypeConverter.dll!System.ComponentModel.TypeDescriptor.GetPropertiesImpl(object component, System.Attribute[] attributes, bool noCustomTypeDesc, bool noAttributes) Unknown
Telerik.Reporting.dll!Telerik.Reporting.ReportSerialization.Serializable<Telerik.Reporting.PictureBox>.AddComponentProperties(System.Collections.Generic.ICollection<System.ComponentModel.PropertyDescriptor> collection) Unknown
Telerik.Reporting.dll!Telerik.Reporting.ReportSerialization.Serializable<Telerik.Reporting.PictureBox>.GetProperties() Unknown
System.ComponentModel.TypeConverter.dll!System.ComponentModel.TypeDescriptor.MergedTypeDescriptor.System.ComponentModel.ICustomTypeDescriptor.GetProperties() Unknown
System.ComponentModel.TypeConverter.dll!System.ComponentModel.TypeDescriptor.GetPropertiesImpl(object component, System.Attribute[] attributes, bool noCustomTypeDesc, bool noAttributes) Unknown
Telerik.Reporting.dll!Telerik.Reporting.Serialization.ObjectReader.ReadProperties(object obj) Unknown
Telerik.Reporting.dll!Telerik.Reporting.Serialization.ObjectReader.ReadObject(System.Type type) Unknown
Telerik.Reporting.dll!Telerik.Reporting.Serialization.ObjectReader.ReadXmlElement(string name) Unknown
Telerik.Reporting.dll!Telerik.Reporting.Serialization.ObjectReader.ReadCollection(object collection) Unknown
Telerik.Reporting.dll!Telerik.Reporting.Serialization.ObjectReader.ReadProperties(object obj) Unknown
Telerik.Reporting.dll!Telerik.Reporting.Serialization.ObjectReader.ReadObject(System.Type type) Unknown
Telerik.Reporting.dll!Telerik.Reporting.Serialization.ObjectReader.ReadXmlElement(string name) Unknown
Telerik.Reporting.dll!Telerik.Reporting.Serialization.ObjectReader.ReadCollection(object collection) Unknown
Telerik.Reporting.dll!Telerik.Reporting.Serialization.ObjectReader.ReadProperties(object obj) Unknown
Telerik.Reporting.dll!Telerik.Reporting.Serialization.ObjectReader.ReadObject(System.Type type) Unknown
Telerik.Reporting.dll!Telerik.Reporting.Serialization.ObjectReader.ReadXmlElement(string name) Unknown
Telerik.Reporting.dll!Telerik.Reporting.Serialization.ObjectReader.Deserialize(Telerik.Reporting.Serialization.IResourceHandler handler) Unknown
Telerik.Reporting.dll!Telerik.Reporting.XmlSerialization.XmlSerializerBase.Deserialize(System.Xml.XmlReader reader, Telerik.Reporting.Serialization.IResourceHandler resourceHandler) Unknown
Telerik.Reporting.dll!Telerik.Reporting.XmlSerialization.XmlSerializerBase.Deserialize(System.IO.Stream stream, Telerik.Reporting.Serialization.IResourceHandler resourceHandler) Unknown
Telerik.Reporting.dll!Telerik.Reporting.XmlSerialization.ReportXmlSerializer.Deserialize(System.IO.Stream stream) Unknown
uest) Unknown
```
Hi Scott,
Can you send us a sample runnable application that reproduces the problem, so I can inspect it on my end?
I will be looking forward to receiving an update from you.