0

I need to automatically print a PDF file to a file (need to have printer driver set all the print options like stapling, duplexing, etc) on a network folder so other employees can print the .prn file from networked printers.

After a fair bit of searching I have found that it is possible to have PowerShell print the PDF using

Start-Process -FilePath document.pdf -Verb Print

which invokes the appropriate application to print the PDF but doesn't allow me to check the "Print to file" box.

I could set the default printer's port to FILE:, but then this requires user interaction to specify the destination .prn file name.

A related question (Print to File Programatically using Adobe Acrobat) seems to show it is possible with C# but I have not been able to find anything for PowerShell. It would be ideal if this is possible with PowerShell (I don't know C#) or am I stuck with programmatically interacting with the "Save to File" dialog box?

Grateful for any hint(s).

Colin Wu
  • 611
  • 7
  • 25
  • Here's another thought: if I use `start-process -FilePath file.pdf -Verb PrintTo("special printer")` where "special printer" has its port set to "FILE:", a "save file" dialog will pop up and I have to enter a file name. Is there a way to programmatically interact with that? – Colin Wu Oct 15 '18 at 01:12

2 Answers2

1

This should help you get started:

$PrintDocument = New-Object System.Drawing.Printing.PrintDocument
$PrintDocument.DocumentName = "c:\temp\yourPdf.pdf"
$printDocument.PrinterSettings.PrintToFile = $true
$printDocument.PrinterSettings.PrintFileName = 'c:\temp\test.txt'
$PrintDocument.Print()

if you look at the $printDocument.PrinterSettings there are quite a few properties:

($PrintDocument.PrinterSettings | gm -MemberType Property ).Name -join ','

CanDuplex,Collate,Copies,DefaultPageSettings,Duplex,FromPage,IsDefaultPrinter,IsPlotter,IsValid,LandscapeAngle,MaximumCopies,MaximumPage,MinimumPage,PaperSizes,PaperSources,PrinterName,PrinterResolutions,PrintFileName,PrintRange,PrintToFile,SupportsColor,ToPage
Thom Schumacher
  • 1,469
  • 13
  • 24
  • Thanks, Thom. That is indeed a good start; however, when I examine the print file I only see the PJL commands whether I print a .pdf or a .txt file, using a PCL6 or PS driver. What am I missing? – Colin Wu Oct 12 '18 at 23:58
  • But that (missing itextsharp.dll) doesn't explain why .txt files don't print either. If I set the PrintToFile to $false I only get a blank page for .txt and for .pdf. Must be something else I'm missing. – Colin Wu Oct 13 '18 at 16:48
  • Are you trying to get a copy of this pdf or what is your intent for the print to file? Is the goal that the user can just double click the prn file and it will automatically print? – Thom Schumacher Oct 13 '18 at 17:05
  • The intent is to have print options, such as stapling, duplexing, etc, embedded in the file so that users at remote offices can retrieve and print the .prn on their networked printers without having to launch an application on their desktop. The retrieving and printing of .prn files portion works. I'm having trouble generating the .prn. – Colin Wu Oct 14 '18 at 01:37
  • This page (https://learn.microsoft.com/en-us/dotnet/api/system.drawing.printing.printdocument.print?view=netframework-4.7.2) seems to say I need to provide my own OnPrintPage event handler to render each page to be printed, even for .txt files??? – Colin Wu Oct 14 '18 at 02:27
  • Seems for what you want to do PrintTicket + PrintQueue class might be the way to go see this article: https://stackoverflow.com/questions/33239716/how-to-duplex-and-staple-a-print-job/33239717#33239717 The print ticket seems to have the duplex and stapling. Which seems to be based on the printer to be used. – Thom Schumacher Oct 15 '18 at 15:01
-1

I had the same problem and found a solution here: Control output location when using Powershell Out-Printer to a File

Note that you'll need a virtual printer that uses the FILE: port.

The script basically simulates the user interaction, so it'll occupy the machine it's running on when executed. It's not pretty, but it works for me. I'm running it on a VM.

$VerbosePreference = "Continue"
   add-type -AssemblyName microsoft.VisualBasic
   add-type -AssemblyName System.Windows.Forms
   $rootdir = "\YOUR\OUTPUTPATH"
   $newext = ".prn"
   $Directory = "\YOUR\INPUTPATH"
   $files = Get-ChildItem -Path $Directory –File
   $focus = 3000
      $array = @((Get-ChildItem -Path $Directory -Name ))
  for ($i=0; $I -lt $array.length; $i++) {
   $newfile = $files[$i].BaseName + $newext     
Start-Process $files[$i].FullName -verb Print | out-printer -name "\\PRINTSERVER\PRINTER"  
       start-sleep -Milliseconds $focus
   [System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
   start-sleep -Milliseconds 3700
   [System.Windows.Forms.SendKeys]::SendWait($(Join-Path $rootdir $newfile))
   start-sleep -Milliseconds 3000
   [System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
   start-sleep -Milliseconds 2000
   $focus = 250
 }
Write-Verbose "Print Files Generated"
waxzR
  • 25
  • 5