I’m writing a script that uses the Powershell Module PSWritePDF, but it causes an exception when run in Powershell Universal.
Convert-PDFToText - Processing document <filepath> failed with error: Exception calling ".ctor" with "1" argument(s): "The type initializer for 'iText.Commons.Actions.EventManager' threw an exception."
Finding the inner stacktrace, it seems like the issue is that they use iText7, which in turns tries to load an assembly that is already loaded:
---> System.TypeInitializationException: The type initializer for 'iText.Commons.Actions.ProductEventHandler' threw an exception.
---> System.IO.FileLoadException: Could not load file or assembly 'Microsoft.Extensions.Logging.Abstractions, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. Could not find or load a specific file. (0x80131621)
File name: 'Microsoft.Extensions.Logging.Abstractions, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'
---> System.IO.FileLoadException: Assembly with same name is already loaded
at System.Runtime.Loader.AssemblyLoadContext.LoadFromPath(IntPtr ptrNativeAssemblyLoadContext, String ilPath, String niPath, ObjectHandleOnStack retAssembly)
at System.Runtime.Loader.AssemblyLoadContext.LoadFromAssemblyPath(String assemblyPath)
at System.Reflection.Assembly.LoadFrom(String assemblyFile)
at System.Reflection.Assembly.LoadFromResolveHandler(Object sender, ResolveEventArgs args)
at System.Runtime.Loader.AssemblyLoadContext.InvokeResolveEvent(ResolveEventHandler eventHandler, RuntimeAssembly assembly, String name)
at System.Runtime.Loader.AssemblyLoadContext.OnAssemblyResolve(RuntimeAssembly assembly, String assemblyFullName)
at iText.Commons.Actions.ProductEventHandler..cctor()
--- End of inner exception stack trace ---
at iText.Commons.Actions.EventManager..ctor()
at iText.Commons.Actions.EventManager..cctor()
--- End of inner exception stack trace ---
at iText.Commons.Actions.EventManager.GetInstance()
at iText.Kernel.Pdf.PdfDocument.Open(PdfVersion newPdfVersion)
at iText.Kernel.Pdf.PdfDocument..ctor(PdfReader reader, DocumentProperties properties)
at CallSite.Target(Closure , CallSite , Type , Object )
I remember there has been threads about different modules before, but I can’t find them again, was there a way to work around this?
Product: PowerShell Universal
Version: 3.7.9