forked from escottj/Doc2PDF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Doc2PDF.ps1
197 lines (183 loc) · 5.79 KB
/
Doc2PDF.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#############################################
# 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 Word Formats
Function Wrd-Chk($f, $e, $p){
$f = [string]$f
For ($i = 0; $i -le $Wrd_Array.Length; $i++) {
$Temp = [string]$Wrd_Array[$i]
$Temp = $Temp.TrimStart('*')
If ($e -eq $Temp) {
Wrd-PDF $f $p
}
}
}
#Check for Excel Formats
Function Exl-Chk($f, $e, $p){
$f = [string]$f
For ($i = 0; $i -le $Exl_Array.Length; $i++) {
$Temp = [string]$Exl_Array[$i]
$Temp = $Temp.TrimStart('*')
If ($e -eq $Temp) {
Exl-PDF $f $p
}
}
}
#Check for PowerPoint Formats
Function Pow-Chk($f, $e, $p){
$f = [string]$f
For ($i = 0; $i -le $Pow_Array.Length; $i++) {
$Temp = [string]$Pow_Array[$i]
$Temp = $Temp.TrimStart('*')
If ($e -eq $Temp) {
Pow-PDF $f $p
}
}
}
#Check for Publisher Formats
Function Pub-Chk($f, $e, $p){
$f = [string]$f
For ($i = 0; $i -le $Pub_Array.Length; $i++) {
$Temp = [string]$Pub_Array[$i]
$Temp = $Temp.TrimStart('*')
If ($e -eq $Temp) {
Pub-PDF $f $p
}
}
}
#Check for Visio Formats
Function Vis-Chk($f, $e, $p){
$f = [string]$f
For ($i = 0; $i -le $Vis_Array.Length; $i++) {
$Temp = [string]$Vis_Array[$i]
$Temp = $Temp.TrimStart('*')
If ($e -eq $Temp) {
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-Chk $File $Ext $PDF
Exl-Chk $File $Ext $PDF
Pow-Chk $File $Ext $PDF
Pub-Chk $File $Ext $PDF
Vis-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-Chk $File $Ext $PDF
Exl-Chk $File $Ext $PDF
Pow-Chk $File $Ext $PDF
Pub-Chk $File $Ext $PDF
Vis-Chk $File $Ext $PDF
}
#Cleanup
Remove-Item Function:Wrd-PDF, Function:Wrd-Chk
Remove-Item Function:Exl-PDF, Function:Exl-Chk
Remove-Item Function:Pow-PDF, Function:Pow-Chk
Remove-Item Function:Pub-PDF, Function:Pub-Chk
Remove-Item Function:Vis-PDF, Function:Vis-Chk