在 PowerShell 中将 PDF 打印到文件 [英] Print a PDF to a file in PowerShell
问题描述
我需要将 PDF 文件自动打印到网络文件夹上的文件(需要让打印机驱动程序设置所有打印选项,如装订、双面打印等),以便其他员工可以从网络打印机打印 .prn 文件.
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.
经过大量搜索后,我发现可以使用 PowerShell 打印 PDF
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
调用适当的应用程序来打印 PDF,但不允许我选中打印到文件"框.
which invokes the appropriate application to print the PDF but doesn't allow me to check the "Print to file" box.
我可以将默认打印机的端口设置为 FILE:,但这需要用户交互以指定目标 .prn 文件名.
I could set the default printer's port to FILE:, but then this requires user interaction to specify the destination .prn file name.
一个相关问题(使用 Adobe Acrobat 以编程方式打印到文件)似乎表明使用 C# 是可能的,但我还没有能够为 PowerShell 找到任何东西.如果使用 PowerShell(我不懂 C#)可以做到这一点,或者我是否坚持以编程方式与保存到文件"对话框进行交互,那将是理想的选择?
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?
感谢任何提示.
推荐答案
这应该可以帮助您入门:
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()
如果您查看 $printDocument.PrinterSettings 有很多属性:
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
这篇关于在 PowerShell 中将 PDF 打印到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!