Erick's Blog
  • Home
  • Blog
  • Apps
    • Mortgage Calculator
  • PowerShell
    • Blog & Examples
    • Excel Reference
  • Windchill
    • Document Exporter
    • Document Worker
    • E.P.L.E.S.
    • Export Released
    • Property Checker
    • Windchill Business Analytics >
      • Part 01 - Program Logic
      • Part 02 - Getting Data with SQL Queries
      • Part 03 - Automating SQL Queries
      • Part 04 - Converting SQL Results to XML
      • Part 05 - Data Processing and Manipulation
      • Part 06 - Displaying XML in HTML
      • Part 07 - Auto Updating Displayed Data
      • Part 08 - Hosting Webpage with an Existing Apache Installation
      • Part 09 - Running Multiple Queries In Sequence
      • Part 10 - Calculating Data Trends
      • Part 11 - Making It Modular
    • Windchill Quick View
  • Music
  • Contact

How to Convert Any Office File to PDF with PowerShell

6/16/2016

5 Comments

 
In this case, "Any" = Word, Excel, PowerPoint, Publisher, and Visio.

Now that that is out of the way, my Doc2PDF script will:
  • Detect whether you have given it a file or directory
  • Convert that single file or all files in a directory and all subdirectories to PDFs

It is very easy to use.  Please leave me feedback if you found it useful.  Download from below link.

https://github.com/escottj/Doc2PDF
5 Comments
Joseph
4/15/2019 07:07:21 pm

Sorry I'm not to great with coding, how do I define the folder that it looks in?

I have a fold with over 800 .pub files that I need to convert to PDF.

Regards,

Joseph C

Reply
Joseph
4/15/2019 11:17:02 pm

Sorry, I got myself confused. I have got the script working for everything but Publisher Files. Any idea why they aren't working? Im using Windows 7 / Office 2010 64Bit / Powershell v4

Regards,

Joseph C.

Joseph C.

Reply
Christian
10/22/2020 05:03:11 am

Hi, nice script. It helped me big time!

Reply
Chuck M
9/15/2022 12:34:12 pm

Handy script for my conversion project. I tweaked it a little for logic and flow but functionally accurate. Thanks! I've included my mods below:

<#
https://github.com/escottj/Doc2PDF/blob/master/Doc2PDF.ps1

Doc2PDF
Created: April 30, 2016
Last Modified: June 16, 2016
Version: 1.0
Supported Office: 2010*, 2013, 2016
Supported PowerShell: 4, 5
Copyright © 2016 Erick Scott Johnson
All rights reserved.
#>
#Input
$Input = $args[0]

#Define Office Formats
$Wrd_Array = '*.docx', '*.doc', '*.odt', '*.rtf', '*.txt', '*.wpd'
$Exl_Array = '*.xlsx', '*.xls', '*.ods', '*.csv'
$Pow_Array = '*.pptx', '*.ppt', '*.odp'
$Pub_Array = '*.pub'
$Vis_Array = '*.vsdx', '*.vsd', '*.vssx', '*.vss'
$Off_Array = $Wrd_Array + $Exl_Array + $Pow_Array + $Pub_Array + $Vis_Array
$ExtChk = [System.IO.Path]::GetExtension($Input)

#Convert Word to PDF
Function Wrd-PDF($f, $p)
{
$Wrd = New-Object -ComObject Word.Application
$Version = $Wrd.Version
$Doc = $Wrd.Documents.Open($f)

#Check Version of Office Installed
If ($Version -eq '16.0' -Or $Version -eq '15.0') {
$Doc.SaveAs($p, 17)
$Doc.Close($False)
}
ElseIf ($Version -eq '14.0') {
$Doc.SaveAs([ref] $p,[ref] 17)
$Doc.Close([ref]$False)
}
[gc]::Collect()
[gc]::WaitForPendingFinalizers()
$Wrd.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Wrd)
Remove-Variable Wrd
}

#Convert Excel to PDF
Function Exl-PDF($f, $p)
{
$Exl = New-Object -ComObject Excel.Application
$Doc = $Exl.Workbooks.Open($f)
$Doc.ExportAsFixedFormat([Microsoft.Office.Interop.Excel.XlFixedFormatType]::xlTypePDF, $p)
$Doc.Close($False)
[gc]::Collect()
[gc]::WaitForPendingFinalizers()
$Exl.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Exl)
Remove-Variable Exl
}

#Convert PowerPoint to PDF
Function Pow-PDF($f, $p)
{
$Pow = New-Object -ComObject PowerPoint.Application
$Doc = $Pow.Presentations.Open($f, $True, $True, $False)
$Doc.SaveAs($p, 32)
$Doc.Close()
[gc]::Collect()
[gc]::WaitForPendingFinalizers()
$Pow.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Pow)
Remove-Variable Pow
}

#Convert Publisher to PDF
Function Pub-PDF($f, $p)
{
$Pub = New-Object -ComObject Publisher.Application
$Doc = $Pub.Open($f)
$Doc.ExportAsFixedFormat([Microsoft.Office.Interop.Publisher.PbFixedFormatType]::pbFixedFormatTypePDF, $p)
$Doc.Close()
[gc]::Collect()
[gc]::WaitForPendingFinalizers()
$Pub.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Pub)
Remove-Variable Pub
}

#Convert Visio to PDF
Function Vis-PDF($f, $p)
{
$Vis = New-Object -ComObject Visio.Application
$Doc = $Vis.Documents.Open($f)
$Doc.ExportAsFixedFormat([Microsoft.Office.Interop.Visio.VisFixedFormatType]::xlTypePDF, $p)
$Doc.Close()
[gc]::Collect()
[gc]::WaitForPendingFinalizers()
$Vis.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Vis)
Remove-Variable Vis
}

#Check for Formats
Function Fmt-Chk($f, $e, $p, $fmt)
{
$f = [string]$f
For ($i = 0; $i -le $Wrd_Array.Length; $i++)
{
$Temp = [string]$Wrd_Array[$i]
$Temp = $Temp.TrimStart('*')
If ($e -eq $Temp)
{
Switch ($Fmt)
{
'Wrd' { Wrd-PDF $f $p }
'Exl' { Exl-PDF $f $p }
'Pow' { Pow-PDF $f $p }
'Pub' { Pub-PDF $f $p }
'Vis' { Vis-PDF $f $p }
}
}
}
}

#Check if input is file or directory
If ($ExtChk -eq '')
{
$Files = Get-ChildItem -path $Input -include $Off_Array -recurse
ForEach ($File in $Files) {
$Path = [System.IO.Path]::GetDirectoryName($File)
$Filename = [System.IO.Path]::GetFileNameWithoutExtension($File)
$Ext = [System.IO.Path]::GetExtension($File)
$PDF = $Path + '\' + $Filename + '.pdf'
'Wrd','Exl','Pow','Pub','Vis' | %{ Fmt-Chk $File $Ext $PDF $_ }
}
}
Else
{
$File = $Input
$Path = [System.IO.Path]::GetDirectoryName($File)
$Filename = [System.IO.Path]::GetFileNameWithoutExtension($File)
$Ext = [System.IO.Path]::GetExtension($File)
$PDF = $Path + '\' + $Filename + '.pdf'
'Wrd','Exl','Pow','Pub','Vis' | %{ Fmt-Chk $File $Ext $PDF $_ }
}

#Cleanup
'Wrd','Exl','Pow','Pub','Vis' | %{ Remove-Item Function:$_-PDF, Function:$_-Chk }

Reply
St Louis Plumbing link
7/8/2023 10:01:14 am

Apprecciate you blogging this

Reply



Leave a Reply.

    Author

    PLM engineer while "on the clock", programmer, designer, dreamer all other times.

    Archives

    August 2016
    June 2016

    Categories

    All
    Cover Page
    Excel
    Office
    PDF
    PowerShell
    Reference
    Signature
    Watermark
    Word

    RSS Feed

Copyright © 2023 Erick Johnson
  • Home
  • Blog
  • Apps
    • Mortgage Calculator
  • PowerShell
    • Blog & Examples
    • Excel Reference
  • Windchill
    • Document Exporter
    • Document Worker
    • E.P.L.E.S.
    • Export Released
    • Property Checker
    • Windchill Business Analytics >
      • Part 01 - Program Logic
      • Part 02 - Getting Data with SQL Queries
      • Part 03 - Automating SQL Queries
      • Part 04 - Converting SQL Results to XML
      • Part 05 - Data Processing and Manipulation
      • Part 06 - Displaying XML in HTML
      • Part 07 - Auto Updating Displayed Data
      • Part 08 - Hosting Webpage with an Existing Apache Installation
      • Part 09 - Running Multiple Queries In Sequence
      • Part 10 - Calculating Data Trends
      • Part 11 - Making It Modular
    • Windchill Quick View
  • Music
  • Contact