Printing PDF Files (Part 2)

In the previous tip we explained how PowerShell can send PDF documents to the default PDF printer. This generic approach is OK for simple scenarios but won’t let you choose a specific printer.

You get more control when you use a specific software because then you can use specific features exposed by that software.

The example below uses Acrobat Reader to print out PDF documents. It shows how you can use the specific options in Acrobat Reader to choose any printer you want, plus Acrobat Reader automatically closes after printing the document.

# PDF document to print out
$PDFPath = "C:docsdocument.pdf"

# find Acrobat Reader
$Paths = @(Resolve-Path -Path "C:Program Files*Adobe*readeracrord32.exe")

# none found
if ($Paths.Count -eq 0)
{
    Write-Warning "Acrobat Reader not installed. Cannot continue."
    return
}

# more than one found, choose one manually
if ($Paths.Count -gt 1)
{
$Paths = @($Paths | 
Out-GridView -Title 'Choose Acrobat Reader' -OutputMode Single)
}

$Software = $Paths[0]

# does it exist?
$exists = Test-Path -Path $Software
if (!$exists)
{
    Write-Warning "Acrobat Reader not found. Cannot continue."
    return
}

# choose printer
$printerName = Get-Printer | 
    Select-Object -ExpandProperty Name | 
    Sort-Object |
    Out-GridView -Title "Choose Printer" -OutputMode Single

$printer = Get-Printer -Name $printerName
$drivername= $printer.DriverName
$portname=$printer.PortName
$arguments='/S /T "{0}" "{1}" "{2}" {3}' -f $PDFPath, $printername, $drivername, $portname

Start-Process $Software -ArgumentList $arguments -Wait

psconf.eu – PowerShell Conference EU 2019 – June 4-7, Hannover Germany – visit www.psconf.eu There aren’t too many trainings around for experienced PowerShell scripters where you really still learn something new. But there’s one place you don’t want to miss: PowerShell Conference EU – with 40 renown international speakers including PowerShell team members and MVPs, plus 350 professional and creative PowerShell scripters. Registration is open at www.psconf.eu, and the full 3-track 4-days agenda becomes available soon. Once a year it’s just a smart move to come together, update know-how, learn about security and mitigations, and bring home fresh ideas and authoritative guidance. We’d sure love to see and hear from you!

Twitter This Tip! ReTweet this Tip!

GD Star Rating
loading...