user3433278
user3433278

Reputation: 33

Powershell script to convert PDF to TIFF with Ghostscript

I have been asked to write a script that automatically converts PDF files to TIFF files so they can be processed furter. With a lot of help from Google and this site. (I never studied any programming language) I created the code below. Even though it's working now, it is not quite what I was hoping for since it creates 13 files every time it runs where it should create only 1.

Could someone be kind enough to take a look at the script and tell me where I went wrong?

Thank you in advance!

EDIT: In this (test) case there's only one PDF in the folder and it's named test.pdf, however the idea is that the script looks through all the PDF in the given folder since it's unsure how many PDF's are in the folder at any given time. Let it run as a service in the background(?)

I'll edit the post with the error code/description once I find out how to get them, I can't keep up with the command line.

#Path to your Ghostscript EXE
$tool = 'C:\\Program Files\\gs\\gs9.10\\bin\\gswin64c.exe'

#Directory containing the PDF files that will be converted
$inputDir = 'C:\\test\\'

#Output path where converted PDF files will be stored
$outputDirPDF = 'C:\\test\\oud\\'

#Output path where the TIF files will be saved
$outputDir = 'C:\\test\\TIFF'

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

foreach($pdf in $pdfs)
{

    $tif = $outputDir + $pdf.BaseName + ".tif"
    if(test-path $tif)
    {
        "tif file already exists " + $tif
    }
    else        
    {   
        'Processing ' + $pdf.Name        
        $param = "-sOutputFile=$tif"
        & $tool -q -dNOPAUSE -sDEVICE=tiffg4 $param -r300 $pdf.FullName -c quit
    }
    Move-Item $pdf $outputDirPDF
}

Upvotes: 0

Views: 9245

Answers (2)

user3433278
user3433278

Reputation: 33

It's working now, apparently I was missing an "exit" at the end of the code. It might not be the most beautiful piece of code, but it seems to do the job so I'm happy with it. Below the piece of code that actually works;

#Path to your Ghostscript EXE
$tool = 'C:\\Program Files\\gs\\gs9.10\\bin\\gswin64c.exe'

#Directory containing the PDF files that will be converted
$inputDir = 'C:\\test\\'

#Output path where converted PDF files will be stored
$outputDirPDF = 'C:\\test\\oud\\'

#Output path where the TIF files will be saved
$outputDir = 'C:\\test\\TIFF\\'

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

foreach($pdf in $pdfs)
{
    $tif = $outputDir + $pdf.BaseName + ".tif"
    if(test-path $tif)
    {
        "tif file already exists " + $tif
    }
    else        
    {   
        'Processing ' + $pdf.Name        
        $param = "-sOutputFile=$tif"
        & $tool -q -dNOPAUSE -sDEVICE=tiffg4 $param -r300 $pdf.FullName -c quit
    }
    Move-Item $pdf $outputDirPDF
}
EXIT

Upvotes: 3

KenS
KenS

Reputation: 31141

It appears to be creating one TIFF file for each PDF file in the source directory. How many PDF files are in the directory (and any sub-directories) ? How many pages in the input PDF file ?

I note that you move the original PDF from 'InputDir' to 'OutputDirPDF' when completed, but 'OutputDirPDF' is a child of 'InputDir', so if you recurse child directories when looking for input files you may find files you have already processed. NB I know nothing about Powershell so this may be just fine.

I'd suggest making 'InputDir' and 'OutputDirPDF' at the same level, eg "c:\temp\input" and "c:\temp\outputPDF".

That's about all I can say on the information here, you could state what the input PDF filename(s) and output Filename(s) are, and what the processing messages say.

Upvotes: 0

Related Questions