-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert2pdf.ps1
147 lines (119 loc) · 4.15 KB
/
convert2pdf.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
# convert2pdf.ps1
# Powershell script to convert word/ppt to pdf
# Author: Learner Chen ([email protected])
# last updated: 2024-10-31
# License: MIT
Function WordConvertToPDF {
param(
$wordfile,
$pdf_folder
)
# single file
$file = Get-Item -path $wordfile
Write-Output "processing: $($file.Name)"
$word_app = New-Object -ComObject Word.Application
$word_app.visible = $false
$document = $word_app.Documents.Open($file.FullName)
# $pdf_file = GetPdffile $file $pdf_folder
$pdf_file = "$($file.DirectoryName)\\$pdf_folder\\$($file.BaseName).pdf"
# write-output $pdf_file
$document.SaveAs([ref] $pdf_file, [ref] 17)
$document.Close()
$word_app.Quit()
[gc]::Collect();
[gc]::WaitForPendingFinalizers();
Write-Output "done"
}
Function PptConvertToPDF {
param(
$pptfile,
$pdf_folder
)
# single file
$ppt_app = New-Object -ComObject PowerPoint.Application
# $ppt_app.visible = [Microsoft.Office.Core.MsoTriState]::msoFalse
$file = Get-Item -path $pptfile
Write-Output "processing: $($file.Name)"
$ppt = $ppt_app.Presentations.Open($file.FullName)
# $pdf_file = GetPdffile $file $pdf_folder
$pdf_file = "$($file.DirectoryName)\\$pdf_folder\\$($file.BaseName).pdf"
# write-output $pdf_file
$ppt.SaveAs([ref] $pdf_file, [ref] 32)
$ppt_app.Quit()
$ppt_app = $null
[gc]::Collect();
[gc]::WaitForPendingFinalizers();
Write-Output "done"
}
Function ScanWord2PDF {
param(
$folder,
$pdf_folder
)
$word_app = New-Object -ComObject Word.Application
$word_app.visible = $false
# This filter will find .doc as well as .docx documents
Get-ChildItem -Path $folder -Filter *.doc* | ForEach-Object {
Write-Output "processing: $_"
$document = $word_app.Documents.Open($_.FullName)
$pdf_filename = "$($_.DirectoryName)\\$pdf_folder\\$($_.BaseName).pdf"
$document.SaveAs([ref] $pdf_filename, [ref] 17)
$document.Close()
}
$word_app.Quit()
$word_app = $null
[gc]::Collect();
[gc]::WaitForPendingFinalizers();
}
Function ScanPPT2PDF {
param(
$folder,
$pdf_folder
)
$ppt_app = New-Object -ComObject PowerPoint.Application
# $ppt_app.visible = [Microsoft.Office.Core.MsoTriState]::msoFalse
# This filter will find .ppt as well as .pptx documents
Get-ChildItem -Path $folder -Filter *.ppt* | ForEach-Object {
Write-Output "processing: $_"
$presentation = $ppt_app.Presentations.Open($_.FullName)
$pdf_filename = "$($_.DirectoryName)\\$pdf_folder\\$($_.BaseName).pdf"
$presentation.SaveAs([ref] $pdf_filename, [ref] 32)
$presentation.Close()
}
$ppt_app.Quit()
$ppt_app = $null
[gc]::Collect();
[gc]::WaitForPendingFinalizers();
}
# Add the PowerPoint/Word assemblies that we'll need
Add-type -AssemblyName office -ErrorAction SilentlyContinue
Add-Type -AssemblyName microsoft.office.interop.powerpoint -ErrorAction SilentlyContinue
Add-Type -AssemblyName microsoft.office.interop.word -ErrorAction SilentlyContinue
$pdf_sub_folder_name = "converted_pdfs"
# main function entry
function Main {
param($file = $pwd)
# Write-Host "The selected file is: $file"
$path = Get-Item -path $file
if ($path.PSIsContainer) {
# Write-Host "Directory:" $path
New-Item -Path $path\$pdf_sub_folder_name -ItemType directory -Force
ScanPPT2PDF $path $pdf_sub_folder_name
ScanWord2PDF $path $pdf_sub_folder_name
}
else {
# Write-Host "File:" $path.DirectoryName $path.Name
New-Item -Path $(Join-Path -path $path.DirectoryName -childpath $pdf_sub_folder_name) -ItemType directory -Force
if ($path.Extension -eq ".doc" -or $path.Extension -eq ".docx") {
WordConvertToPDF $path $pdf_sub_folder_name
}
elseif ($path.Extension -eq ".ppt" -or $path.Extension -eq ".pptx") {
PptConvertToPDF $path $pdf_sub_folder_name
}
else {
Write-Host "Not a word or ppt file"
}
}
Read-Host -Prompt "Press any key to continue..." | Out-Null
}
main $args[0]