1

I have a requirement to have a small script on a Windows Server using PowerShell to convert files to PDF (from RTF). I've got a prototype using Word that works on my desktop machine, but the servers involved don't have Office/Word installed. Is there a similar way I can call Wordpad to achieve the same thing?

$documents_path = 'c:\projects\pbl\hi.rtf'

$word_app = New-Object -ComObject Word.Application

$document = $word_app.Documents.Open($documents_path)

$pdf_filename = 'c:\projects\pbl\hi.pdf'

$document.SaveAs([ref] $pdf_filename, [ref] 17)

$document.Close()

$word_app.Quit()
Paul
  • 1,041
  • 11
  • 26

2 Answers2

1

You would not be able to accomplish this goal using only WordPad because it has no automation interface like Microsoft Word has. You would need to investigate an alternative solution to achieve this.

Bill_Stewart
  • 22,916
  • 4
  • 51
  • 62
0

This is old thread so no point in addressing OP question as such

However for the benefit of others landing here, this is a "Yes" Answer, I have not run it on a server, but understand there may be issues calling MS Print to PDF -- headless, for several reasons, including it uses the Graphics Device Interface. It works perfectly on a workstation.

It is done as a one line command along these lines, Input could be %1 with %2 for Output.

There are 2 WordPad.exe's to try :-) This is the default American English one:-

%comspec% "C:\Windows\System32\wordpad.exe" /pt "%Input%"  "Microsoft Print to PDF" "Microsoft Print to PDF" "%Output%"

OR my Locale British English one, which I target !

"%ProgramFiles%\Windows NT\Accessories\WORDPAD.EXE" /pt %1  "Microsoft Print to PDF" "Microsoft Print to PDF" %2 

For a drag and drop wrapper see:-

Doc2PDF.cmd - Convert .Doc(x), .ODT, .RTF or .Txt to PDF & open them in SumatraPDF enter image description here

P.S A better replacement to WordPad is Jarte Pro and if you have a Portable WinWord.exe you could look at https://stackoverflow.com/a/2749172/10802527

K J
  • 8,045
  • 3
  • 14
  • 36