In my job, we only send documents to clients in pdf formats. Sometimes we have to send several documents at once and it takes a good time to open each one and save it as a pdf, it’s not a very pleasant task.
In order to minimize this problem, I’ve created a simple powershell script that opens a Word document and saves it as pdf without any user interaction. With such script, I can convert a lot of files at once, just like this:
ls . *.doc* –Recurse | %{ ~\scripts\doc2ps1.ps1 $_.fullname }
With this command, I list recursively each doc/docx file in a directory and save each one as PDF.
By default, Office 2007 apps are not able to save files as PDF, so you have to install the add-in from Microsoft first to use the script. The add-in is available here.
Bellow is the script, it’s also available in my PowerShell scripts repository at GitHub (http://github.com/dougfernando/utility-scripts/tree/master) as doc2pdf.ps1.param (
[string]$source = $(Throw "You have to specify a source path."))
$extensionSize = 3
if ($source.EndsWith("docx")) {
$extensionSize = 4
}
$destiny = $source.Substring(0, $source.Length - $extensionSize) + "pdf"
$saveaspath = [ref] $destiny
$formatPDF = [ref] 17
$word = new-object -ComObject "word.application"
$doc = $word.documents.open($source)
$doc.SaveAs($saveaspath, $formatPDF)
$doc.Close()
echo "Converted file: $source"
ps winword | kill
An important note, the script closes every Word instance in the end, so save any work previous using it.