Soru / cevap düzenleme için özür dilerim.
İşte çalışan senaryo. Oldukça az değişiklik var ama ihtiyaç duyduğu şeyi yapıyor. Orta gelişme .pdf'den .tiff'e .txt'den .jpg'ye geçmem istendi. İşlem hala oldukça iyi çalışıyor, sadece istenen çıktı türünü elde etmek için Ghost script bölümünü değiştirmeniz gerekiyor.
Diğer konulardan pek çok yardım almak, bu yazının oldukça bir kısmı farklı insanlardan oluşan bir bolluktan Frankensteined oldu.
Gerekli bileşenler:
- Güç kalkanı
- CutePDF Yazar
- GhostScript
Bu betiğin ne yaptığını çalıştırın:
- Klasörü tarar ve dosyaları günlüğe kaydeder
- .Txt dosyalarını işlenmek üzere kök klasörüne taşır
- CutePDF Writer kullanarak .txt dosyalarını .pdf dosyasına yazdırır
- Ghostscript'i kullanarak .pdf dosyalarını .jpg dosyasına dönüştürür
- .Pdf, .jpg ve .txt dosyalarını ayrı klasörlere taşır
- Tümünü siler .pdf
- "İşleme" klasöründe değişiklik taramasını başlatır, değişiklik bulunursa değişiklik komut dosyasını tekrar çalıştırır.
Bunu bir .exe aracına dönüştürdüm ve NSSM'yi her zaman çalışacak bir hizmet olarak ayarlamak için kullandım.
$freshStart = 0
$PrintPageHandler ={
param([object]$sender, [System.Drawing.Printing.PrintPageEventArgs]$ev)
$linesPerPage = 0
$yPos = 0
$count = 0
$leftMargin = $ev.MarginBounds.Left
$topMargin = $ev.MarginBounds.Top
$line = $null
$printFont = New-Object System.Drawing.Font "Arial", 10
# Calculate the number of lines per page.
$linesPerPage = $ev.MarginBounds.Height / $printFont.GetHeight($ev.Graphics)
# Print each line of the file.
while ($count -lt $linesPerPage -and (($line = $streamToPrint.ReadLine()) -ne $null))
{
$yPos = $topMargin + ($count * $printFont.GetHeight($ev.Graphics))
$ev.Graphics.DrawString($line, $printFont, [System.Drawing.Brushes]::Black, $leftMargin, $yPos, (New-Object System.Drawing.StringFormat))
$count++
}
# If more lines exist, print another page.
if ($line -ne $null)
{
$ev.HasMorePages = $true
}
else
{
$ev.HasMorePages = $false
}
}
While ($freshStart -eq 0)
{
$prossDir = 'C:\FINAL\PROCESSING\'
$files = get-childitem -Path $prossDir | where {$_.Extension -match "txt"}
foreach($file in $files)
{
$path = "C:\FINAL\PROCESSING\$file"
$logline = "$(Get-Date), BackLog, FINAL, $path"
Add-content "C:\LOG\log.txt" -value $logline
}
#Path to your Ghostscript EXE
$tool = 'C:\Program Files\gs\gs9.26\bin\gswin64c.exe'
#Directory containing the PDF files that will be converted
$inputDir = 'C:\FINAL\'
#.pdf Files
$pdfDir = 'C:\FINAL\*.pdf'
#.jpg Files
$jpgDir = 'C:\FINAL\*.jpg'
#.txt Files
$txtDir = 'C:\FINAL\*.txt'
#Directory that deletes all old pdfs
$deleteME = 'C:\FINAL\DELETE\*.pdf'
#Directory catchall for all incoming files.
$dumpDir = 'C:\FINAL\PROCESSING\*.*'
#Output path where converted PDF files will be stored
$pdfOutputDir = 'C:\FINAL\DELETE'
#Output path where the JPG files will be saved
$jpgOutputDir = 'C:\FINAL\Folder2'
#Output path where the TXT files will be saved
$txtOutputDir = 'C:\FINAL\Folder1'
Get-ChildItem -Path $dumpDir -File | Move-Item -Destination $inputDir
function Out-Pdf
{
param($InputDocument, $OutputFolder)
Add-Type -AssemblyName System.Drawing
$doc = New-Object System.Drawing.Printing.PrintDocument
$doc.DocumentName = $InputDocument.FullName
$doc.PrinterSettings = New-Object System.Drawing.Printing.PrinterSettings
$doc.PrinterSettings.PrinterName = 'CutePDF Writer'
$doc.PrinterSettings.PrintToFile = $true
$streamToPrint = New-Object System.IO.StreamReader $InputDocument.FullName
$doc.add_PrintPage($PrintPageHandler)
$doc.PrinterSettings.PrintFileName = "$($InputDocument.DirectoryName)\$($InputDocument.BaseName).pdf"
$doc.Print()
$streamToPrint.Close()
}
Get-Childitem -Path "C:\FINAL" -File -Filter "*.txt" |
ForEach-Object { Out-Pdf $_ $_.Directory }
$pdfs = get-childitem $inputDir | where {$_.Extension -match "pdf"}
foreach($pdf in $pdfs)
{
$jpg = $inputDir + $pdf.BaseName + ".jpg"
$cJpg = $inputDir + $pdf.BaseName + "_" + "%03d" + ".jpg"
if(test-path $jpg)
{
"jpg file already exists " + $jpg
}
else
{
'Processing ' + $pdf.Name
$param = "-sOutputFile=$cJpg"
& $tool -q -dNOPAUSE -sDEVICE=jpeg $param -r300 $pdf.FullName -c quit
}
}
Get-ChildItem -Path $pdfDir -Recurse -File | Move-Item -Destination $pdfOutputDir
Get-ChildItem -Path $jpgDir -Recurse -File | Move-Item -Destination $jpgOutputDir
Get-ChildItem -Path $txtDir -Recurse -File | Move-Item -Destination $txtOutputDir
Remove-Item -Path $deleteME
$freshStart = 1
}
### SET Folder TO WATCH + FILES TO WATCH + SubFolder YES/NO
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "C:\FINAL\PROCESSING"
$watcher.Filter = "*.*"
$watcher.IncludeSubdirectories = $false
$watcher.EnableRaisingEvents = $true
### DEFINE ACTIONS AFTER AN EVENT IS DETECTED
$action =
{
$path = $Event.SourceEventArgs.FullPath
$changeType = $Event.SourceEventArgs.ChangeType
$logline = "$(Get-Date), $changeType, FINAL, $path"
Add-content "C:\LOG\log.txt" -value $logline
#Path to your Ghostscript EXE
$tool = 'C:\Program Files\gs\gs9.26\bin\gswin64c.exe'
#Directory containing the PDF files that will be converted
$inputDir = 'C:\FINAL\'
#.pdf Files
$pdfDir = 'C:\FINAL\*.pdf'
#.jpg Files
$jpgDir = 'C:\FINAL\*.jpg'
#.txt Files
$txtDir = 'C:\FINAL\*.txt'
#Directory that deletes all old pdfs
$deleteME = 'C:\FINAL\DELETE\*.pdf'
#Directory catchall for all incoming files.
$dumpDir = 'C:\FINAL\PROCESSING\*.*'
#Output path where converted PDF files will be stored
$pdfOutputDir = 'C:\FINAL\DELETE'
#Output path where the JPG files will be saved
$jpgOutputDir = 'C:\FINAL\Folder2'
#Output path where the TXT files will be saved
$txtOutputDir = 'C:\FINAL\Folder1'
Get-ChildItem -Path $dumpDir -File | Move-Item -Destination $inputDir
$PrintPageHandler ={
param([object]$sender, [System.Drawing.Printing.PrintPageEventArgs]$ev)
$linesPerPage = 0
$yPos = 0
$count = 0
$leftMargin = $ev.MarginBounds.Left
$topMargin = $ev.MarginBounds.Top
$line = $null
$printFont = New-Object System.Drawing.Font "Arial", 10
# Calculate the number of lines per page.
$linesPerPage = $ev.MarginBounds.Height / $printFont.GetHeight($ev.Graphics)
# Print each line of the file.
while ($count -lt $linesPerPage -and (($line = $streamToPrint.ReadLine()) -ne $null))
{
$yPos = $topMargin + ($count * $printFont.GetHeight($ev.Graphics))
$ev.Graphics.DrawString($line, $printFont, [System.Drawing.Brushes]::Black, $leftMargin, $yPos, (New-Object System.Drawing.StringFormat))
$count++
}
# If more lines exist, print another page.
if ($line -ne $null)
{
$ev.HasMorePages = $true
}
else
{
$ev.HasMorePages = $false
}
}
function Out-Pdf
{
param($InputDocument, $OutputFolder)
Add-Type -AssemblyName System.Drawing
$doc = New-Object System.Drawing.Printing.PrintDocument
$doc.DocumentName = $InputDocument.FullName
$doc.PrinterSettings = New-Object System.Drawing.Printing.PrinterSettings
$doc.PrinterSettings.PrinterName = 'CutePDF Writer'
$doc.PrinterSettings.PrintToFile = $true
$streamToPrint = New-Object System.IO.StreamReader $InputDocument.FullName
$doc.add_PrintPage($PrintPageHandler)
$doc.PrinterSettings.PrintFileName = "$($InputDocument.DirectoryName)\$($InputDocument.BaseName).pdf"
$doc.Print()
$streamToPrint.Close()
}
Get-Childitem -Path "C:\FINAL" -File -Filter "*.txt" |
ForEach-Object { Out-Pdf $_ $_.Directory }
$pdfs = get-childitem $inputDir | where {$_.Extension -match "pdf"}
foreach($pdf in $pdfs)
{
$jpg = $inputDir + $pdf.BaseName + ".jpg"
$cJpg = $inputDir + $pdf.BaseName + "_" + "%03d" + ".jpg"
if(test-path $jpg)
{
"jpg file already exists " + $jpg
}
else
{
'Processing ' + $pdf.Name
$param = "-sOutputFile=$cJpg"
& $tool -q -dNOPAUSE -sDEVICE=jpeg $param -r300 $pdf.FullName -c quit
}
}
Get-ChildItem -Path $pdfDir -Recurse -File | Move-Item -Destination $pdfOutputDir
Get-ChildItem -Path $jpgDir -Recurse -File | Move-Item -Destination $jpgOutputDir
Get-ChildItem -Path $txtDir -Recurse -File | Move-Item -Destination $txtOutputDir
Remove-Item -Path $deleteME
}
### DECIDE WHICH EVENTS SHOULD BE WATCHED
Register-ObjectEvent $watcher "Created" -Action $action
while ($true) {sleep 5}