简体   繁体   English

将 PDF 打印到 PowerShell 中的文件中

[英]Print a PDF to a file in PowerShell

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.我需要自动将 PDF 文件打印到 .network 文件夹上的文件(需要打印机驱动程序设置所有打印选项,如装订、双面打印等),以便其他员工可以从 .networked 打印机打印 .prn 文件。

After a fair bit of searching I have found that it is possible to have PowerShell print the PDF using经过一番搜索后,我发现可以使用 PowerShell 打印 PDF

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.它调用适当的应用程序来打印 PDF 但不允许我选中“打印到文件”框。

I could set the default printer's port to FILE:, but then this requires user interaction to specify the destination.prn file name.我可以将默认打印机的端口设置为 FILE:,但这需要用户交互来指定 destination.prn 文件名。

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?一个相关的问题( Print to File Programatically using Adobe Acrobat )似乎表明 C# 是可能的,但我无法找到 PowerShell 的任何内容。如果 PowerShell 可以做到这一点(我不知道 C#),那将是理想的选择还是我坚持以编程方式与“保存到文件”对话框进行交互?

Grateful for any hint(s).感谢任何提示。

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 有很多属性:

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

I had the same problem and found a solution here: Control output location when using Powershell Out-Printer to a File我遇到了同样的问题并在这里找到了解决方案: Control output location when using Powershell Out-Printer to a File

Note that you'll need a virtual printer that uses the FILE: port.请注意,您需要一个使用 FILE: 端口的虚拟打印机。

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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM