0

I am new to Powershell. I have one htlm file and I want to convert that into pdf file using powershell script.

having this error.

Unable to find type [iText.Html2Pdf.HtmlConverter].
At C:\Users\Z004APNA\Desktop\Htms_to_Pdf\Generate-HTML_to_PDF.ps1:32 char:1
+ [iText.Html2Pdf.HtmlConverter]::ConvertToPdf($source, $dest)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (iText.Html2Pdf.HtmlConverter:TypeName) [], RuntimeException
    + FullyQualifiedErrorId : TypeNotFound

I am using this code.

$InputLocation = "C:\Users\Z004APNA\Desktop\Htms_to_Pdf"

$htmls = get-childitem  -filter *.htm? -path $InputLocation

#$htmls = Get-ChildItem -Path $InputLocation -Include *.html -Recurse
foreach($html in $htmls)
{
    $filename = $html.FullName
    $basepath = "$($_.DirectoryName)"
    $pdf = $html.FullName.split('.')[0] + '.pdf'
}

Add-Type -Path ($InputLocation+"\BouncyCastle.Crypto.dll")
Add-Type -Path ($InputLocation+"\Common.Logging.Core.dll")
Add-Type -Path ($InputLocation+"\Common.Logging.dll")
Add-Type -Path ($InputLocation+"\itext.io.dll")
Add-Type -Path ($InputLocation+"\itext.kernel.dll")
Add-Type -Path ($InputLocation+"\itext.forms.dll")
Add-Type -Path ($InputLocation+"\itext.layout.dll")
Add-Type -Path ($InputLocation+"\itext.styledxmlparser.dll")
Add-Type -Path ($InputLocation+"\itext.svg.dll")
Add-Type -Path ($InputLocation+"\itext.html2pdf.dll")

$source = [System.IO.FileInfo]::new($filename)
$dest = [System.IO.FileInfo]::new($pdf)
[iText.Html2Pdf.HtmlConverter]::ConvertToPdf($source, $dest)
James Z
  • 12,209
  • 10
  • 24
  • 44
Avin Verma
  • 11
  • 1
  • 6
  • 1
    Searching around, iText is a Java/C# library you bought/dowloaded right? You probably need to look into how to use [C#/.NET assemblies in PowerShell](https://stackoverflow.com/questions/3079346/how-to-reference-net-assemblies-using-powershell) – Viet Than Jul 12 '21 at 13:19

1 Answers1

0

Posting comment as answer:

Searching around, iText is a Java/C# library you bought/dowloaded right? You probably need to look into how to use C#/.NET assemblies in PowerShell

With the two top answers being use:

[Reflection.Assembly]::LoadFile("c:\temp\tempLib.dll")

or use the built in Cmdlet Add-Type:

Add-Type -Path foo.dll
Viet Than
  • 164
  • 1
  • 10
  • Can you please share the link where I can get the dll libraries – Avin Verma Jul 13 '21 at 14:32
  • uhhhh, how did you know to use `iText` anyway? i mean, it's wherever you downloaded/install the library – Viet Than Jul 13 '21 at 20:12
  • Now I am able get the libraries. While executing, I am getting this exception – Avin Verma Jul 14 '21 at 12:20
  • Exception calling "ConvertToPdf" with "2" argument(s): "The type initializer for 'iText.IO.Util.EncodingUtil' threw an exception." At C:\Users\Z004APNA\Desktop\Htms_to_Pdf\Generate-HTML_to_PDF.ps1:51 char:1 + [iText.Html2Pdf.HtmlConverter]::ConvertToPdf("C:\Users\Z004APNA\Deskt ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : TypeInitializationException – Avin Verma Jul 14 '21 at 12:21
  • This looks promising [these](https://stackoverflow.com/questions/18801440/powershell-load-dll-got-error-add-type-could-not-load-file-or-assembly-webdr). I suggest googling specific parts of the error message to figure out if anyone else got a similar problem. My search terms were `"The type initializer for 'iText.IO.Util.EncodingUtil' threw an exception.` or `~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : TypeInitializationException` – Viet Than Jul 15 '21 at 13:12