Вы можете попробовать что-то вроде этого,
$TxtFilePath = "C:\folder\txtfile.txt"
Start-Process notepad.exe $TxtFilePath -NoNewWindow -Wait #Assuming user save the file
Get-Content -Path $TxtFilePath| Out-Printer PDFCreator #PDFCreator is the printer name for PDF
--EDIT--
Я не мог понять, прямой подход, чтобы сохранить имя файла, тип написал хак с мс слово,
Function Save-TxtAsPDF
{
Param([string] $FilePath, [string] $OutFolder)
# Required Word Variables
$ExportASPDFConstant = 17
$DoNotSaveConstant = 0
# Create a hidden Word window
$word = New-Object -ComObject word.application
$word.visible = $false
# Add a Word document
$doc = $word.documents.add()
# Put the text into the Word document
$txt = Get-Content $FilePath
$selection = $word.selection
$selection.typeText($txt)
# Set the page orientation to landscape
$doc.PageSetup.Orientation = 1
$FileName = (Split-Path -Path $FilePath -Leaf).Replace(".txt",".pdf")
$DestinationPath = Join-Path $OutFolder $FileName
# Export the PDF file and close without saving a Word document
$doc.ExportAsFixedFormat($DestinationPath,$ExportASPDFConstant)
$doc.close([ref]$DoNotSaveConstant)
$word.Quit()
}
$TxtFilePath = "C:\folder\tt.txt"
$OutFolder = "C:\Outputfolder"
Start-Process notepad.exe $TxtFilePath -NoNewWindow -Wait #Assuming user save the file
Save-TxtAsPDF -FilePath $TxtFilePath -OutFolder $OutFolder
Посмотрите, поможет ли это!
Это отлично работает. Однако я задаюсь вопросом, есть ли способ сохранить исходное имя текстового файла при сохранении pdf? – Eric
Обновлен ответ, не удалось найти прямой подход! Посмотрите, поможет ли это! –