-
Notifications
You must be signed in to change notification settings - Fork 0
/
tif2pdf
251 lines (210 loc) · 9.84 KB
/
tif2pdf
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
class ProgressManager {
[int]$TotalFiles
[int]$ProcessedFiles
ProgressManager([int]$totalFiles) {
$this.TotalFiles = $totalFiles
$this.ProcessedFiles = 0
}
[void]UpdateProgress() {
$this.ProcessedFiles++
$percentComplete = ($this.ProcessedFiles / $this.TotalFiles) * 100
Write-Progress -Activity "進捗状況" -Status "$($this.ProcessedFiles) / $($this.TotalFiles)" -PercentComplete $percentComplete
}
}
class FolderManager {
[string]$CommonPath
[string[]]$SourceFolders
FolderManager([string]$commonPath, [string[]]$sourceFolders) {
$this.CommonPath = $commonPath
$this.SourceFolders = $sourceFolders
}
[int]GetTotalFiles() {
$totalFiles = 0
foreach ($sourceFolder in $this.SourceFolders) {
$sourceFolderPath = Join-Path $this.CommonPath -ChildPath $sourceFolder
$tiffFolder = Join-Path -Path $sourceFolderPath -ChildPath "ExpFile"
if (Test-Path -Path $tiffFolder) {
$totalFiles += (Get-ChildItem -Path $tiffFolder -Filter *.tif).Count
}
}
return $totalFiles
}
[array]GetTiffFiles([string]$sourceFolder, [int]$maxFilesToProcess) {
$sourceFolderPath = Join-Path $this.CommonPath -ChildPath $sourceFolder
$tiffFolder = Join-Path -Path $sourceFolderPath -ChildPath "ExpFile"
if (-not (Test-Path -Path $tiffFolder)) {
Write-Host "Error: Path does not exist - $tiffFolder"
return @()
}
return Get-ChildItem -Path $tiffFolder -Filter *.tif | Select-Object -First $maxFilesToProcess
}
[void]EnsureFolderExists([string]$folderPath) {
if (-not (Test-Path -Path $folderPath)) {
New-Item -Path $folderPath -ItemType Directory
}
}
[void]MoveFile([string]$sourcePath, [string]$destinationPath) {
Move-Item -Path $sourcePath -Destination $destinationPath -Force
}
[bool]WaitForPdfGeneration([string]$pdfFilePath, [int]$timeoutMinutes, [int]$sleepSeconds) {
$waittime = [datetime]::Now.AddMinutes($timeoutMinutes)
while ((-not (Test-Path -Path $pdfFilePath)) -and ([datetime]::Now -lt $waittime)) {
Start-Sleep -Seconds $sleepSeconds
}
return Test-Path -Path $pdfFilePath
}
}
class TiffToPdfConverter {
[string]$NetworkPath
[string]$LocalDrive
[string]$CommonPath
[string[]]$SourceFolders
[string]$PscanInFolder
[string]$PscanOutFolder
[int]$MaxFilesToProcess
[int]$TimeoutMinutes
[int]$SleepSeconds
[bool]$SkipConverted
[bool]$Reset
[ProgressManager]$ProgressManager
[FolderManager]$FolderManager
TiffToPdfConverter([string]$networkPath, [string]$localDrive, [string]$commonPath, [string[]]$sourceFolders, [string]$pscanInFolder, [string]$pscanOutFolder, [int]$maxFilesToProcess, [int]$timeoutMinutes, [int]$sleepSeconds, [bool]$skipConverted, [bool]$reset) {
$this.NetworkPath = $networkPath
$this.LocalDrive = $localDrive
$this.CommonPath = $commonPath
$this.SourceFolders = $sourceFolders
$this.PscanInFolder = $pscanInFolder
$this.PscanOutFolder = $pscanOutFolder
$this.MaxFilesToProcess = $maxFilesToProcess
$this.TimeoutMinutes = $timeoutMinutes
$this.SleepSeconds = $sleepSeconds
$this.SkipConverted = $skipConverted
$this.Reset = $reset
$this.FolderManager = [FolderManager]::new($commonPath, $sourceFolders)
}
[void]Initialize() {
# ネットワークドライブを一時的にマッピング
if (-not (Test-Path -Path $this.LocalDrive)) {
Write-Host "Mapping network drive..."
New-PSDrive -Name "Z" -PSProvider FileSystem -Root $this.NetworkPath -Persist
}
# ネットワークドライブの確認
if (-not (Test-Path -Path $this.CommonPath)) {
Write-Host "Error: Network drive not mapped correctly or path does not exist."
exit
}
# 変換リストの生成
$tiffListPath = Join-Path -Path $this.CommonPath -ChildPath "TIFF_LIST.txt"
if ($this.Reset -and (Test-Path -Path $tiffListPath)) {
Remove-Item -Path $tiffListPath -Force
}
if (-not (Test-Path -Path $tiffListPath)) {
New-Item -Path $tiffListPath -ItemType File
}
# リセットモードの場合、PDFフォルダとERRフォルダを削除
if ($this.Reset) {
foreach ($sourceFolder in $this.SourceFolders) {
$sourceFolderPath = Join-Path $this.CommonPath -ChildPath $sourceFolder
$pdfFolderBase = Join-Path -Path $sourceFolderPath -ChildPath "PDF"
$pdfErrFolderBase = Join-Path -Path $sourceFolderPath -ChildPath "ERR"
if (Test-Path -Path $pdfFolderBase) {
Remove-Item -Path $pdfFolderBase -Recurse -Force
}
if (Test-Path -Path $pdfErrFolderBase) {
Remove-Item -Path $pdfErrFolderBase -Recurse -Force
}
}
}
# TIFFファイルの総数をカウント
$totalFiles = $this.FolderManager.GetTotalFiles()
$this.ProgressManager = [ProgressManager]::new($totalFiles)
}
[void]ProcessFiles() {
foreach ($sourceFolder in $this.SourceFolders) {
$tiffFiles = $this.FolderManager.GetTiffFiles($sourceFolder, $this.MaxFilesToProcess)
foreach ($tiffFile in $tiffFiles) {
$tiffFileName = $tiffFile.Name
$tiffFilePath = $tiffFile.FullName
$pdfFileName = [System.IO.Path]::ChangeExtension($tiffFileName, ".pdf")
$pdfFolderBase = Join-Path -Path (Join-Path $this.CommonPath -ChildPath $sourceFolder) -ChildPath "PDF"
$pdfFilePath = Join-Path -Path $pdfFolderBase -ChildPath $pdfFileName
# 既に変換済みのTIFFファイルをスキップ
if ($this.SkipConverted -and (Test-Path -Path $pdfFilePath)) {
Write-Host "Skipping already converted file: $tiffFileName"
continue
}
# TIFFファイルをPSCAN_INにコピー
Copy-Item -Path $tiffFilePath -Destination $this.PscanInFolder
# TIFF_LISTに追記
$tiffListPath = Join-Path -Path $this.CommonPath -ChildPath "TIFF_LIST.txt"
Add-Content -Path $tiffListPath -Value "$sourceFolder`t$tiffFileName`t" -NoNewline
# PDFファイルが生成されるかを待機
$pdfFilePath = Join-Path -Path $this.PscanOutFolder -ChildPath $pdfFileName
$pdfGenerated = $this.FolderManager.WaitForPdfGeneration($pdfFilePath, $this.TimeoutMinutes, $this.SleepSeconds)
if ($pdfGenerated) {
# PDFフォルダの作成
$this.FolderManager.EnsureFolderExists($pdfFolderBase)
# PDFファイルを移動(既存のファイルがある場合は上書き)
$this.FolderManager.MoveFile($pdfFilePath, $pdfFolderBase)
# TIFF_LISTにOKを追記
$timestamp = Get-Date -Format "yyyy/MM/dd HH:mm:ss"
Add-Content -Path $tiffListPath -Value "OK`t$timestamp`n"
# 処理状況を更新
$this.ProgressManager.UpdateProgress()
} else {
# タイムアウト処理
$pdfErrFolderBase = Join-Path -Path (Join-Path $this.CommonPath -ChildPath $sourceFolder) -ChildPath "ERR"
$this.FolderManager.EnsureFolderExists($pdfErrFolderBase)
# TIFFファイルをERRフォルダに移動(既存のファイルがある場合は上書き)
$this.FolderManager.MoveFile($tiffFilePath, $pdfErrFolderBase)
# TIFF_LISTにNGを追記
$timestamp = Get-Date -Format "yyyy/MM/dd HH:mm:ss"
Add-Content -Path $tiffListPath -Value "NG`t$timestamp`n"
# 処理状況を更新
$this.ProgressManager.UpdateProgress()
}
}
}
}
[void]Cleanup() {
# ネットワークドライブを解除
if (Get-PSDrive -Name "Z" -ErrorAction SilentlyContinue) {
Remove-PSDrive -Name "Z" -Force
Write-Host "Network drive Z: has been removed."
}
}
}
param (
[switch]$debug,
[switch]$SkipConverted = $true,
[switch]$Reset,
[switch]$ShowDialog
)
# ネットワークドライブの設定
$networkPath = if ($debug) { "\\DELLD033\技術部" } else { "\\ycsvm112\技術部" }
$localDrive = "Z:"
$commonPath = "Z:\管理課\管理課共有資料\ArcSuite\eValue図面検索データ_240310\"
# 変換元フォルダのリスト(全角の「¥」をそのまま使用)
$sourceFolders = @(
"図面検索【最新版】¥図面",
"図面検索【最新版】¥通知書",
"図面検索【最新版】¥個装",
"図面検索【旧版】¥図面(旧)",
"図面検索【旧版】¥個装"
)
# デバッグモードの設定
$maxFilesToProcess = if ($debug) { 2 } else { [int]::MaxValue }
# フォルダのパス設定
$pscanInFolder = "\\10.23.2.28\HGPscanServPlus5\Job02_OCR\OCR_IN"
$pscanOutFolder = "\\10.23.2.28\HGPscanServPlus5\Job02_OCR\OCR_OUT"
# 待機時間の設定
$timeoutMinutes = 1
$sleepSeconds = 10
# TiffToPdfConverterクラスのインスタンスを作成
$converter = [TiffToPdfConverter]::new($networkPath, $localDrive, $commonPath, $sourceFolders, $pscanInFolder, $pscanOutFolder, $maxFilesToProcess, $timeoutMinutes, $sleepSeconds, $SkipConverted, $Reset)
# 初期化
$converter.Initialize()
# ファイルの処理
$converter.ProcessFiles()
# クリーンアップ
$converter.Cleanup()