HTML-Renderer icon indicating copy to clipboard operation
HTML-Renderer copied to clipboard

When using version PDFSharp 1.50.4545.0 type initializer throws an exception for adapters

Open JordynRachelThompson opened this issue 6 years ago • 15 comments

Works fine when I revert back to PDFSharp version 1.32.3057. However, as soon as I update to version 1.50 I get type initializer error for: var pdf = TheArtOfDev.HtmlRenderer.PdfSharp.PdfGenerator.GeneratePdf("

text

", PdfSharp.PageSize.A4);

The error says "adapters are obsolete and not implemented anymore"

JordynRachelThompson avatar Jan 18 '19 14:01 JordynRachelThompson

I also get this issue when using the HTMLRenderer. I will try reverting to PDFSharp version 1.32.3057 to see if I can work around this issue.

ghost avatar Feb 25 '19 20:02 ghost

HtmlRenderer 1.51 should work with PdfSharp 1.50. When I say "should" I mean I am actively using it without those errors. If you really want help, post the full exception information showing the stack trace.

bradleypeet avatar Mar 19 '19 20:03 bradleypeet

I'm getting a similar error. Here is the stack trace:

[InvalidOperationException: Obsolete and not implemted any more.] PdfSharp.Drawing.XFontFamily.get_Families() +46 TheArtOfDev.HtmlRenderer.PdfSharp.Adapters.PdfSharpAdapter..ctor() in d:\source\github\HTML-Renderer\Source\HtmlRenderer.PdfSharp\Adapters\PdfSharpAdapter.cs:44 TheArtOfDev.HtmlRenderer.PdfSharp.Adapters.PdfSharpAdapter..cctor() in d:\source\github\HTML-Renderer\Source\HtmlRenderer.PdfSharp\Adapters\PdfSharpAdapter.cs:33

[TypeInitializationException: The type initializer for 'TheArtOfDev.HtmlRenderer.PdfSharp.Adapters.PdfSharpAdapter' threw an exception.] TheArtOfDev.HtmlRenderer.PdfSharp.Adapters.PdfSharpAdapter.get_Instance() in d:\source\github\HTML-Renderer\Source\HtmlRenderer.PdfSharp\Adapters\PdfSharpAdapter.cs:57 TheArtOfDev.HtmlRenderer.PdfSharp.HtmlContainer..ctor() in d:\source\github\HTML-Renderer\Source\HtmlRenderer.PdfSharp\HtmlContainer.cs:43 TheArtOfDev.HtmlRenderer.PdfSharp.PdfGenerator.GeneratePdf(String html, PdfGenerateConfig config, CssData cssData, EventHandler1 stylesheetLoad, EventHandler1 imageLoad) in d:\source\github\HTML-Renderer\Source\HtmlRenderer.PdfSharp\PdfGenerator.cs:110 Uid.Web.Controllers.<Export>d__8.MoveNext() in C:\Users\dansmith\Code\Uid.Web\Controllers\PersonasController.cs:122 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +99 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58 ...

Using ILSpy I can see that version 1.50 of PdfSharp throws an exception on PdfSharp.Drawing.XFontFamily.Families, and has "Use platform API directly" in the method's Obsolete attribute. Version 1.32 has valid code in the property accessor.

Upgrading HtmlRenderer.* to 1.5.1-beta1 does not generate this error.

djsmith avatar Mar 28 '19 16:03 djsmith

I am also getting the same error. Anyone got help or fixed it ?

sunnykoundal avatar Apr 01 '19 09:04 sunnykoundal

I reverted back to PDFSharp version 1.32.3057. It is working fine now.

Also I am using HtmlRenderer.PdfSharp version 1.5.0.6.

sunnykoundal avatar Apr 01 '19 10:04 sunnykoundal

I don't think you guys use using the very latest HtmlRenderer code. 1.5.0.6 did not work with PdfSharp 1.50. There was a change made in 2016 to fix the exception in the PdfSharpAdapter constructor. The code in the constructor should look like this:

    private PdfSharpAdapter()
    {
        AddFontFamilyMapping("monospace", "Courier New");
        AddFontFamilyMapping("Helvetica", "Arial");

        var families = new InstalledFontCollection();

        foreach (var family in families.Families)
        {
            AddFontFamily(new FontFamilyAdapter(new XFontFamily(family.Name)));
        }
    }

Again, you need HtmlRenderer 1.5.1-beta1 or later to use PdfSharp 1.50. Note the latest PdfSharp, last I checked, was 1.50-beta5.

bradleypeet avatar Apr 01 '19 17:04 bradleypeet

Would like to upgrade to PdfSharp 1.50, but at the moment I can't because of this issue, and through policy can't update to a beta version. 1.5.1-beta1 has been out for 2.5 years so far - is it possible that any issues will have been shaken out by now and a full release built? Thanks for the effort in producing this library, it is appreciated, and although it would be nice to see this, I understand it may take time...

sizenineelm avatar May 10 '19 13:05 sizenineelm

Was this ever resolved? Thanks

unitedsoftwork avatar Sep 11 '19 14:09 unitedsoftwork

Also wondering if this was ever resolved?

It seems reverting back to PDFSharp version 1.32.3057 should work? (I haven't tested this yet, just assuming this was the fix).

Thanks

CarlosRangel17 avatar Sep 30 '19 13:09 CarlosRangel17

Hi all, I resolved this problem just by updating HtmlRenderer.PdfSharp like this: Install-Package HtmlRenderer.PdfSharp -Version 1.5.1-beta1

mbo2olivier avatar Jan 03 '20 12:01 mbo2olivier

I'm having the same problem using the newest versions of both PdfSharp(1.50.5147) and HtmlRenderer(1.5.0.6) using .NET 4.7.2

GabrielBFurlani avatar Feb 08 '22 19:02 GabrielBFurlani

I'm having the same problem using the newest versions of both PdfSharp(1.50.5147) and HtmlRenderer(1.5.0.6) using .NET 4.7.2

thanks that was useful

devabady avatar Sep 28 '22 21:09 devabady

If anyone has this error using this nuget in docker (Linux) then you have this error because PDFSharp uses System.Drawing.Common which works only in Windows. Fortunately there is a solution to this. Firstly install latest version of HtmlRenderer.PdfSharp.NetStandard2 (for me 1.5.1.1). Then look at this comment: https://stackoverflow.com/a/73977430/14332640 In summary you need to install libgdiplus inside docker and add <RuntimeHostConfigurationOption Include="System.Drawing.EnableUnixSupport" Value="true" /> inside <ItemGroup> tags. inside every .csproj file of solution (one per project). Works like a charm!

WojGrab avatar Feb 01 '23 10:02 WojGrab

Still the problem: using the newest versions of PdfSharp(1.50.5147) and HtmlRenderer(1.5.0.6) using .NET 4.8.0 This failure looks like verry olde one. Any date of fixing? I cannot downgrade because of using PDFsharp-MigraDoc

Adi-18 avatar Mar 27 '23 12:03 Adi-18

I'm now running into the same problem, this is open for quite a while, any new news beside downgrading or going to a beta?

omgitsjan avatar Mar 14 '24 11:03 omgitsjan