Home > front end >  Generate a pdf from powershell
Generate a pdf from powershell

Time:03-15

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

CodePudding user response:

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()

CodePudding user response:

The combination of PowerShell dependencies can be problematic as they need to be of a known working group in time (you can try to update later, after testing), 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 enter image description here

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. Good Luck.

  • Related