'Generate a pdf from powershell
I'm trying to generate a PDF with powershell but I don't know how to proceed. I already tried to use Itext 7 but I don't know how to make it work.
When i try to install Itext7 on powershell i have this error message :
No match found for the specified search criteria and the itext7 package name. Use
Get-PackageSource to display for all available registered package sources.
Could I have some help?
Thanks in advance
Solution 1:[1]
The combination of PowerShell dependencies can be problematic as they need to be of a known working group in time and 7.1.14 was touted as a light solution so see later TL;DR edits or others comments below, and run as Admin perhaps different to a normal user. So follow these steps carefully as some may downgrade your current settings.
MOST IMPORTANT use a project directory and watch that your prompt is located in that folder to ensure you are not running in the default PowerShell directory. I use a shortcut where the target directory is "blank/empty" thus defaults to the current working folder.
1st Check:-
project folder>[Net.ServicePointManager]::SecurityProtocol
should return either Tls12 or Tls13 we need it to be 1.2 so keep a note if yours is set to Tls13 and run this line.
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
we may need to possibly change package provider so first check if nuget includes https://www.nuget.org/api/v2/:-
> Get-PackageSource
Name ProviderName IsTrusted Location
---- ------------ --------- --------
nuget.org NuGet False https://www.nuget.org/api/v2/
PSGallery PowerShellGet False https://www.powershellgallery.com/api/v2
If not you can add it as
Register-PackageSource nuget.org https://www.nuget.org/api/v2/ -ProviderName NuGet
now you should be able to install the dlls as follows
Install-Package -Name "itext7" -ProviderName NuGet -RequiredVersion 7.1.14 -Destination . -SkipDependencies
Install-Package -Name Portable.BouncyCastle -ProviderName NuGet -RequiredVersion 1.8.9.0 -Destination . -SkipDependencies
Install-Package -Name Common.Logging -ProviderName NuGet -RequiredVersion 3.4.1.0 -Destination . -SkipDependencies
Install-Package -Name Common.Logging.Core -ProviderName NuGet -RequiredVersion 3.4.1.0 -Destination . -SkipDependencies
Double check your folder has the correct structure

Note the order and location of the script is critical for correct loading
Add-Type -Path (Join-Path $PSScriptRoot ".\Common.Logging.Core.3.4.1\lib\net40\Common.Logging.Core.dll")
Add-Type -Path (Join-Path $PSScriptRoot ".\Common.Logging.3.4.1\lib\net40\Common.Logging.dll")
Add-Type -Path (Join-Path $PSScriptRoot ".\Portable.BouncyCastle.1.8.9\lib\net40\BouncyCastle.Crypto.dll")
Add-Type -Path (Join-Path $PSScriptRoot ".\itext7.7.1.14\lib\net40\itext.io.dll")
Add-Type -Path (Join-Path $PSScriptRoot ".\itext7.7.1.14\lib\net40\itext.layout.dll")
Add-Type -Path (Join-Path $PSScriptRoot ".\itext7.7.1.14\lib\net40\itext.kernel.dll")
$pdfDocuFilename = (join-path $PSScriptRoot "My1st.pdf")
$pdfWriter = [iText.Kernel.Pdf.PdfWriter]::new($pdfDocuFilename)
$pdfdocument = [iText.Kernel.Pdf.PdfDocument]::new($pdfWriter)
$pdfdocument.AddNewPage()
$pdfdocument.Close()
This will produce an empty file but proves all is well, and you can start running other examples such as the one suggested by S_G, so after the loading Add-Type block replace my blank example with
[string] $DEST = "HelloWorld.pdf"
$pdfWriter = [iText.Kernel.Pdf.PdfWriter]::new($DEST)
$pdf = [iText.Kernel.Pdf.PdfDocument]::new($pdfWriter)
$document = [iText.Layout.Document]::new($pdf)
$document.Add([iText.Layout.Element.Paragraph]::new("Hello World! from Powershell"))
$pdf.Close()
... Good Luck.
- The versions above were for a fixed point in time when user blogs had verified that 7.1 mixes worked without much conflict, the aim is to produce a group of standalone files working within Net40 environment, but time moves on and you should ensure you are using a newer mix. HOWEVER everything changed in 7.1.15 as a phenomenally greater list of dependencies is now required for Net 4.5 and now 4.6.1 although, packages/itext7/7.2.1 itself still works with packages/Portable.BouncyCastle/1.8.9 + and common logging is still 3.4.1
Solution 2:[2]
Below is the code for a PowerShell Script which outputs a PDF with "Hello World!" written on it. It mirrors the functionality of iText 7 basic Hello World example. You can change it as per your requirement.
Add-Type -Path "C:\temp\Common.Logging.Core.dll"
Add-Type -Path "C:\temp\Common.Logging.dll"
Add-Type -Path "C:\temp\BouncyCastle.Crypto.dll"
Add-Type -Path "C:\temp\itext.io.dll"
Add-Type -Path "C:\temp\itext.layout.dll"
Add-Type -Path "C:\temp\itext.kernel.dll"
[string] $DEST = "C:\files\HelloWorldPowerShell.pdf"
$pdfWriter = [iText.Kernel.Pdf.PdfWriter]::new($DEST)
$pdf = [iText.Kernel.Pdf.PdfDocument]::new($pdfWriter)
$document = [iText.Layout.Document]::new($pdf)
$document.Add([iText.Layout.Element.Paragraph]::new("Hello World!"))
$pdf.Close()
Solution 3:[3]
Just my 2 cents but the above code does NOT work with Itext7 7.2.1 (after modifying for proper paths).
Wish I'd seen this post last week - wasted most of several days pulling hair out over 7.2.1 not behaving itself. :(
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | DocZerø |
| Solution 3 | Tim Leigh |
