1

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)
1
  • 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 Commented Jul 12, 2021 at 13:19

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

5 Comments

Can you please share the link where I can get the dll libraries
uhhhh, how did you know to use iText anyway? i mean, it's wherever you downloaded/install the library
Now I am able get the libraries. While executing, I am getting this exception
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
This looks promising these. 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.