Skip to content

Instantly share code, notes, and snippets.

@kstrauss
Last active July 22, 2020 03:42
Show Gist options
  • Save kstrauss/cb0118d28c227981fdcec6645ff9541d to your computer and use it in GitHub Desktop.
Save kstrauss/cb0118d28c227981fdcec6645ff9541d to your computer and use it in GitHub Desktop.
Conversion of word docs to PDF and then combine into one large pdf for a set of directories

scenario

Have a set of directories that have a combination of PDFs and word documents. This is to help me remember what I did for helping Heather with a small work project she needed.

Objectives

  1. Each directories' documents (word & pdf) need to be combined in order into a larger PDF

Prequisites

  1. Have the location of word documents as a trusted location, according to Word's Trust Center.
  2. have ghostscript setup and installed in c:\temp\gs\gs8.63
$d2 = dir -recurse -filter *.doc
$word = new-object -comobject Word.Application
$NotFound = @()
$errors = @()
$d2 | ForEach-Object {
$doc = $null
try{
$f = $_;
$doc = $word.Documents.open($f.fullname)
$found = $false
$dates = (1988..2019 | sort -Descending)
$dates | %{
if (-not $found) {
$pdf_filename = "$($f.DirectoryName)\$_ - $($f.BaseName).pdf"
$range = $doc.content
$p = $range.movestart()
$found = $range.Find.Execute($_)
if ($found){
write-host $pdf_filename
$doc.SaveAs([ref] $pdf_filename, [ref] 17)
}
}
}
if (-not $found){
$NotFound += $f
}
}
catch {
$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
$errors += $_
write-error $ErrorMessage
# stop the loop
break;
}
finally{
if ($doc){
$doc.close()
}
}
}
$word.quit()
# convert into
./FindAndConvert.ps1
# step to combine PDFs that exist into a "combined.pdf" file
$errors=[System.Collections.ArrayList]@()
dir -Directory | % {
pushd $_
$inputFiles = $(dir *.pdf -exclude combined.pdf |? Name -Match "20((16)|(17)|(18)|(19)|(20))\s?-.*" | sort name -Descending | % {$_.name} )
if ($inputFiles.count -gt 1){
#if you get an error try a different version of ghostscript
c:\temp\gs\gs8.63\bin\gswin32c.exe -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress -sOutputFile="combined.pdf" $inputFiles
}
elseif ($inputFiles.count -eq 1){
copy-item $inputFiles combined.pdf
}
if (!$?){
write-host "Failed on $_"
$errors.add($_) | Out-Null
Write-host $inputFiles
}
popd
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment