Search code examples
powershell-4.0itext7

Powershell Convert html file to pdf using itext.html2pdf


How to convert html to pdf file in Powershell using itext.html2pdf?

I just want to take my input.html file and get an output.pdf file

I am using iText 7 pdfHTML vewrsion 2.1.3

Here is a C# code in the itext website, but how to convert it to Powershell?.

static void Main(string[] args)
{
 using (FileStream htmlSource = File.Open("input.html", FileMode.Open))
 using (FileStream pdfDest = File.Open("output.pdf", FileMode.OpenOrCreate))
 {
   ConverterProperties converterProperties = new ConverterProperties();
   HtmlConverter.ConvertToPdf(htmlSource, pdfDest, converterProperties);
 }
}

Thank you in advance for your help.


Solution

  • Make sure to unpack binaries from the following dependencies for .NET environment into the working directory:

    https://www.nuget.org/packages/BouncyCastle/1.8.1 https://www.nuget.org/packages/itext7.pdfhtml/ https://www.nuget.org/packages/itext7/ https://www.nuget.org/packages/Common.Logging/ https://www.nuget.org/packages/Common.Logging.Core/

    Then, use the following PowerShell code:

    Add-Type -Path "D:\temp\BouncyCastle.Crypto.dll"
    Add-Type -Path "D:\temp\Common.Logging.Core.dll"
    Add-Type -Path "D:\temp\Common.Logging.dll"
    Add-Type -Path "D:\temp\itext.io.dll"
    Add-Type -Path "D:\temp\itext.kernel.dll"
    Add-Type -Path "D:\temp\itext.forms.dll"
    Add-Type -Path "D:\temp\itext.layout.dll"
    Add-Type -Path "D:\temp\itext.styledxmlparser.dll"
    Add-Type -Path "D:\temp\itext.svg.dll"
    Add-Type -Path "D:\temp\itext.html2pdf.dll"
    
    
    $source = [System.IO.FileInfo]::new("D:\temp\input.html")
    $dest = [System.IO.FileInfo]::new("D:\temp\output.pdf")
    [iText.Html2Pdf.HtmlConverter]::ConvertToPdf($source, $dest)