Sunday, April 20, 2014

PowerShell function to convert pdf to jpg using GhostScript

#Here is an example on how to call this function to convert pdf to jpg.
#convert-PdfToImage -OriginalFile "$home\Pictures\PayStubSinglePage.pdf" -NewFile "$home\Pictures\PayStubSinglePage.jpg"
 
Function convert-PdfToImage {
    [CmdletBinding()]
    Param(  [Parameter(Mandatory=$True)] [string]$OriginalFile,
            [Parameter(Mandatory=$True)] [string]$NewFile)

    #Path to your Ghostscript EXE
    $tool = 'C:\Users\Greg\Programming\GhostScript\bin\gswin64c.exe'
   
    #Directory containing the PDF files that will be converted
    $inputDir = $OriginalFile
 
    #Output path where the TIF files will be saved
    $outputDir = $NewFile
 
    $pdf = get-childitem $OriginalFile
    $jpg = $outputDir
    Remove-Item $jpg -Force -ErrorAction SilentlyContinue
    'Processing ' + $pdf.Name       

    $param = "-sOutputFile=$jpg"
    & $tool -q -dNOPAUSE -sDEVICE=jpeg $param -r1200 $pdf.FullName -c quit

    #& "$tool" "-q -dNOPAUSE -dNOPROMT -sDEVICE=jpeg $param -r1200 $pdf.FullName -dMaxBitmap=500000000 -dAlignToPixels=0 dGridFitTT=2 -sDEVICE=pngalpha -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -c quit"
    #& "$tool" "-q -dNOPAUSE -dQuiet -dSAFER -dNOPROMT -sDEVICE=jpeg $param -r1200 $pdf.FullName -c quit"
    #& "$tool -dNOPAUSE -sDEVICE=jpeg -sOutputFile=image%d.jpg -dJPEGQ=100 -r300x300 -q C:\Users\Greg\Pictures\PayStubSinglePage.pdf -c quit"
    #& "$tool" "-q -dBATCH -dNOPAUSE -sDEVICE=jpeg -r350 dJPEGQ=100 $pdf.FullName -sOutputFile=C:\Users\Greg\Pictures\PayStubSinglePage.jpg -c quit"
    #& "$tool" "- -q -dQuiet -dSAFER -dBATCH -dNOPAUSE -dNOPROMT -dMaxBitmap=500000000 -dAlignToPixels=0 dGridFitTT=2 -sDEVICE=pngalpha -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -sOutputFile=C:\Users\Greg\Pictures\PayStubSinglePage.jpg -f $pdf.FullName" 
}

3 comments:

  1. Convert pdf to jpg easily.You can use it in C#, VB, .netASP environment, very convenient.

    ReplyDelete
    Replies
    1. Hi Jim, looks neat but it comes with quite the price tag. If you are looking for free solutions to converting PDF to Jpg just Google it, there are lots of free options and tutorials other than mine.

      Delete
    2. Thank you, can you send me the source code of pdf to image? If you can, please send it to my email address: pauljimstudio@gmail.com

      Delete