top of page
  • Writer's pictureRm

Batch convert PDF to TIFF

I had a recent project at work which I was asked to find a better way to convert PDFs to TIFF (or TIF) in a batch without having to use Adobe Pro as Adobe Pro only runs on 32bit and large maps with multiple pages will error out or just take forever. I came up with a solution by using GhostScript to conver the PDFs and PowerShell to tell GhostScript what and how to convert.


Requirements:


PowerShell Script:


$tool = 'C:\Program Files\gs\gs9.22\bin\gswin64c.exe' #the path to your Ghostscript program.


#ask the user if the DPI should be 600 instead of 300

Write-host "Would you like to convert to 600 DPI? Default: 300 DPI" -ForegroundColor Yellow

$Readhost = Read-Host " ( y / n ) "

Switch ($ReadHost)

{

Y {$DPI = "-r600"}

N {$DPI = "-r300"}

Default {}

}


#ask the user if this is a multi-page pdf so it can split the pages into seperate tiff or tif files

Write-host "Is this a mult-page pdf?" -ForegroundColor Yellow

$Readhost = Read-Host " ( y / n ) "

Switch ($ReadHost)

{

Y {$multi = "_p%04d"}

N {$multi = ""}

Default {}

}

#asks the user for the input folder of the PDFs

[string]$files_folder = Read-Host -Prompt 'What is the input location of the PDF Files? Example: C:\Temp'


#asks the user for the output folder of the PDFs

[string]$files_folder_output = Read-Host -Prompt 'What is the output location of the TIF Files? Example: C:\Temp'


#tests out the folder path to make sure its there

$testfolder = Get-Item $files_folder_output


#sets the folder path based on test

$files_folder_output = $testfolder.FullName


#find all the pdfs in that folder

$pdfs = get-childitem $files_folder | where {$_.Extension -match "pdf"}


# for each pdf in the pdf folder, replace the pdf extention to tiff or tif and then run ghostscript to convert that pdf to G4 fax encoding tiff.

foreach($pdf in $pdfs)

{


$TIF = $pdf.Name.replace('.pdf','').replace('.pdf','') + $multi + '.tiff' #note: you can change '.tiff' to '.tif' if you want the .tif extension instead

#sets the tiff output folder and name

$TIFoutput = $files_folder_output+"\"+$TIF

#if the tiff or tif already exist, then don't convert it

if(test-path $TIF)

{

"TIF file already exists " + $TIF

}

else

#converts the pdf

{

'Processing ' + $pdf.Name

$param = "-sOutputFile=$TIFoutput"

& $tool -q -dNOPAUSE -sDEVICE=tiffg4 $DPI -dGraphicsAlphaBits=2 $param $pdf.FullName -c quit

#note: you can change tiffg4 (G4 fax encoding) to the encoding options listed in https://www.ghostscript.com/doc/9.21/Devices.htm

}

}




23 views0 comments
bottom of page