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

Generate PDF from Asp.Net Core

18 Answers 2988 Views
General Discussions
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'

18 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
Iron
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
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/.

MANAMOHAN
Top achievements
Rank 1
commented on 24 May 2022, 08:54 AM

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 body
Download
System.IO.FileNotFoundException: Could not load file or assembly 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. The system cannot find the file specified.
File name: 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

 

Todor
Telerik team
commented on 27 May 2022, 05:17 AM

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.

Scott Waye
Top achievements
Rank 2
Veteran
Iron
commented on 28 Jun 2022, 06:43 PM

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

```

Neli
Telerik team
commented on 01 Jul 2022, 11:09 AM

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.

Tags
General Discussions
Asked by
Henrique Duarte
Top achievements
Rank 1
Veteran
Answers by
Silviya
Telerik team
Hossein
Top achievements
Rank 1
Iron
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