Skip to main content
Skip table of contents

How to deploy an iText License for Azure PowerShell?

When deploying Azure Functions using PowerShell and integrating the iText SDK, end-users might encounter licensing issues during runtime, particularly manifesting as failures in loading the iText license.

In this article, we'll walk through the steps to resolve such issues, and ensure your Azure Function correctly loads the iText license so that PDF operations will work as expected.

Prerequisites

Example Issue

An Azure Function written in PowerShell 7.5 is intended to generate PDFs using the iText SDK. However, the function fails at the point of loading the iText license, reporting errors related to missing dependencies or improperly loaded assemblies.

Solution

Step 1: Identifying the Required Dependencies

This issue often occurs because the transitive dependencies required for iText to function properly within a PowerShell environment are not correctly loaded during runtime.

To resolve this issue, explicitly load all necessary assemblies required by the iText SDK:

The following list applies to the current iText release at the time of writing (9.2.0).

If you are using any iText Core add-ons, ensure to explicitly load these DLLs in addition to those listed above.

Step 2: Adding the Assemblies Explicitly

Place these DLL files within a dedicated lib folder, and explicitly reference them within your Azure Function's profile.ps1 as follows:

POWERSHELL
# Set path to DLLs
$libPath = "$PSScriptRoot\lib"

# Load required assemblies
Add-Type -Path "$libPath\itext.kernel.dll"
Add-Type -Path "$libPath\itext.io.dll"
Add-Type -Path "$libPath\itext.layout.dll"
Add-Type -Path "$libPath\itext.commons.dll"
Add-Type -Path "$libPath\itext.bouncy-castle-connector.dll"
Add-Type -Path "$libPath\itext.licensing.base.dll"

Add-Type -Path "$libPath\System.Text.Json.dll"
Add-Type -Path "$libPath\Microsoft.Extensions.Logging.dll"
Add-Type -Path "$libPath\Microsoft.Extensions.Options.dll"
Add-Type -Path "$libPath\Microsoft.Extensions.DependencyInjection.Abstractions.dll"
Add-Type -Path "$libPath\Microsoft.Extensions.Primitives.dll"
Add-Type -Path "$libPath\System.Diagnostics.DiagnosticSource.dll"

Step 3: Loading the License and Verifying its Status

After referencing the necessary DLLs, you should load your iText license as below:

POWERSHELL
try {
    # Load license file
    $licenseFile = [System.IO.FileInfo]::new("$libPath\ADD UR LICENSE HERE.JSON")
    [iText.Licensing.Base.LicenseKey]::LoadLicenseFile($licenseFile)
	
    Write-Host "License loaded successfully." 
	$infoList = [iText.Licensing.Base.LicenseKey]::GetLoadedLicensesInfo()
	foreach($info in $infoList) {
	Write-Host "License Info :"
	Write-Host ("Product        : " + $info.GetProduct())
    Write-Host ("")
	
	 # Output PDF path
    $outputPath = "$libPath\SP125022.pdf"

    # Create PDF writer and document
    $writer = [iText.Kernel.Pdf.PdfWriter]::new($outputPath)
    $pdfDoc = [iText.Kernel.Pdf.PdfDocument]::new($writer)
    $doc = [iText.Layout.Document]::new($pdfDoc)

    # Writing Text on the PDF
    $para = [iText.Layout.Element.Paragraph]::new("Creating a PDF using Azure Function in Powershell 7.5")
    $doc.Add($para)

    # Close document
    $doc.Close()
	}
	
}
catch {
   $ex = $_.Exception
    while ($ex) {
        Write-Host "Exception Type: $($ex.GetType().FullName)"
        Write-Host $ex.Message
        Write-Host $ex.StackTrace
        Write-Host ""
        $ex = $ex.InnerException
	}
}

Once the license has been loaded successfully, you can verify the license status and then generate PDFs without errors.

To get a full stack trace of the Exception we need to use the following line of code so that end-users can get a full stack trace. Otherwise, it will not process through the entire stack.

POWERSHELL
$ex = $_.Exception
    while ($ex) {
        Write-Host "Exception Type: $($ex.GetType().FullName)"
        Write-Host $ex.Message
        Write-Host $ex.StackTrace
        Write-Host ""
        $ex = $ex.InnerException
	}

Conclusion

Loading dependencies explicitly and accurately verifying license status are essential steps for ensuring the robust deployment of iText Licenses within PowerShell Azure Functions.


Resources

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.