How to Convert HTML to PDF in PowerShell?

While working on an application, I was required to convert an HTML file to a PDF using PowerShell. There are various methods to do so. In this tutorial, I will explain how to convert HTML to PDF in PowerShell with examples.

To convert HTML to PDF using PowerShell, you can utilize the wkhtmltopdf tool, which is a popular open-source command-line utility. First, download and install wkhtmltopdf from its official website. Then, use a PowerShell script to specify the paths to the wkhtmltopdf executable, the HTML file, and the output PDF file. You can easily generate a PDF from an HTML file by running the conversion command in PowerShell. Here’s a complete PowerShell script:

$wkhtmltopdfPath = "C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe"
$htmlFilePath = "C:\MyFolder\report.html"
$pdfFilePath = "C:\MyFolder\report.pdf"
& $wkhtmltopdfPath $htmlFilePath $pdfFilePath

Convert HTML to PDF Using PowerShell

PowerShell provides different methods to convert an HTML file to PDF. Let us check each method with examples.

Method 1: Using wkhtmltopdf

One of the most popular ways of converting HTML to PDF in PowerShell is wkhtmltopdf. This open-source command-line tool uses Webkit to render HTML and can be easily integrated with PowerShell.

Installation

First, download and install wkhtmltopdf from the official website.

Once the installation is over, you can use the below PowerShell script to convert an HTML file to PDF using wkhtmltopdf:

# Path to wkhtmltopdf executable
$wkhtmltopdfPath = "C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe"

# Path to the HTML file
$htmlFilePath = "C:\MyFolder\report.html"

# Output PDF file path
$pdfFilePath = "C:\MyFolder\report.pdf"

# Convert HTML to PDF
& $wkhtmltopdfPath $htmlFilePath $pdfFilePath

This script specifies the paths to the wkhtmltopdf executable, the HTML file, and the output PDF file. It then runs the conversion command.

Check out Create an HTML Table from Variables in PowerShell

Method 2: Using iTextSharp Library

Another method of converting HTML to PDF is to use the iTextSharp library, a powerful PDF manipulation library for .NET that can be used in PowerShell.

Installation

You can download the iTextSharp library from NuGet.

Here’s a PowerShell script to convert HTML to PDF using iTextSharp:

Add-Type -Path "C:\MyFolder\itextsharp.dll"

$htmlContent = Get-Content -Path "C:\MyFolder\report.html" -Raw

$pdfFilePath = "C:\MyFolder\report.pdf"

# Create a new PDF document
$pdfDocument = New-Object iTextSharp.text.Document
$pdfWriter = [iTextSharp.text.pdf.PdfWriter]::GetInstance($pdfDocument, [System.IO.File]::Create($pdfFilePath))

$pdfDocument.Open()

# Add HTML content to the PDF document
$htmlParser = New-Object iTextSharp.tool.xml.XMLWorkerHelper
$htmlParser.ParseXHtml($pdfWriter, $pdfDocument, [System.IO.StringReader]::new($htmlContent))

$pdfDocument.Close()

This script reads the HTML content from a file and uses iTextSharp to create a PDF document.

Check out Convert String to HTML Table in PowerShell

Convert HTML to PDF in PowerShell

Method 3: Using PDF.co API

If you prefer a cloud-based solution, the PDF.co API offers a convenient way to convert HTML to PDF. However, you need an API key for this.

API Key

First, sign up for an API key at PDF.co.

Here’s a PowerShell script to convert HTML to PDF using the PDF.co API:

$apiKey = "your_pdfco_api_key"
$htmlFilePath = "C:\MyFolder\report.html"
$pdfFilePath = "C:\MyFolder\report.pdf"

$htmlContent = Get-Content -Path $htmlFilePath -Raw

$body = @{
    "html" = $htmlContent
}

$response = Invoke-RestMethod -Uri "https://api.pdf.co/v1/pdf/convert/from/html" -Method Post -Headers @{ "x-api-key" = $apiKey } -Body ($body | ConvertTo-Json)

if ($response.error -eq $false) {
    Invoke-WebRequest -Uri $response.url -OutFile $pdfFilePath
    Write-Host "PDF saved to $pdfFilePath"
} else {
    Write-Host "Error: $($response.message)"
}

This script sends the HTML content to the PDF.co API and downloads the resulting PDF file.

Conclusion

In this tutorial, I explained how to convert HTML to PDF in PowerShell using several methods, such as using wkhtmltopdf, the iTextSharp library, or a cloud-based solution like PDF.co, etc.

Let me know in the comment below if you face any issues while converting.

You may also like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.