Zikko's Resources


pdf2jpg - Converting PDF to JPG with Photoshop Scripting

There are several tools out there for converting pages in a PDF into individual JPG files. They cost. Maybe they're good. One of the overall best image convertion tools I know of is ImageMagick. It's free, it's commandline, and it converts between JPG and PDF and lots and lots of other formats too. My hat off to those guys, really! It's just that when it comes to rendering PDF files, I've never been completely happy with the results. By default the antialiasing is kinda jagged, and the only advice I've received on how to get a crisp result is to convert with a higher resolution (producing a HUGE image in memory) and then resize it back to size. This is slightly better, but not good, and it takes a lot of time and processor time. And if ImageMagick can't do it, who can?

Photoshop can!

Adobe Photoshop is the best image manipulation software, no one can argue. It's just that you have to do a whole lot of clicking around. Or do you?... No, you don't! Photoshop can be scripted to do all the things you can have it do manually. In fact, Photoshop is trilingual. It understands JavaScript, VBScript and AppleScript. Which one is best? AppleScript is great if you use Mac I suppose. I first went with JavaScript, feeling more familiar with the syntax. But it has the downside of not being executable from commandline, meaning that you can't pass parameters into the script. You need VBScript to do that, so that's what I went with. I won't go into any great depth on exactly how PS scripting works, but check out Adobe's own documentation, it's very comprehensive.

How do I use it?

Copy and paste the code below into a new document and save it as pdf2jpg.vbs. Then invoke it from commandline. You'll need to have Photoshop installed, of course. This version requires the input file to be in the same path as the script, which is kinda lame. I'll fix it in the next version, which will never come.

Input

The only required parameter is the input filename. Other parameters allow you to specify the prefix of the output filenames (the page number will be appended to it), the maximum width and height of the output files, the output quality, and to limit the range of pages to convert.

Output

The script outputs one JPG file for every page in the PDF (or in the specified range).

Example

pdf2jpg.vbs input.pdf output=out width=400 height=300 quality=9 from=5 to=10

The code

Path = Left( Wscript.ScriptFullName, Len( Wscript.ScriptFullName ) - Len( Wscript.ScriptName ) )

Set Arguments = CreateObject("Scripting.Dictionary")

MainArgument = ""

For Each Argument in WScript.Arguments

    Parts = Split( Argument, "=" )
    
    Name = Parts(0)
    
    If UBound(Parts) > 0 Then
        Value = Parts(1)
    Else
        Value = True
        
        If MainArgument = "" Then
            MainArgument = Name
        End If
        
    End if
    
    Arguments.Add Name, Value

Next

If MainArgument <> "" Then

    InputFileName = MainArgument

    If Arguments.Exists( "output" ) Then
        OutputFileName = Arguments.Item("output")
    Else
        Parts = Split( InputFileName, "." )
        OutputFileName = Parts( 0 )
    End If
    
    InputFilePath = Path & InputFileName
    OutputFilePath = Path & OutputFileName
    
    Set OpenOptions = CreateObject( "Photoshop.PDFOpenOptions" )
    
    OpenOptions.ConstrainProportions = True
    
    If Arguments.Exists( "width" ) Then
        OpenOptions.Width = CInt( Arguments.Item( "width" ) )
    End if
    
    If Arguments.Exists( "height" ) Then
        OpenOptions.Height = CInt( Arguments.Item( "height" ) )
    End if
    
    OpenOptions.Mode = 2
    OpenOptions.AntiAlias = True
    
    Set SaveOptions = CreateObject( "Photoshop.JPEGSaveOptions" )
    
    If Arguments.Exists( "quality" ) Then
        SaveOptions.Quality = CInt( Arguments.Item( "quality" ) )
    Else
        SaveOptions.Quality = 9
    End if
    
    If Arguments.Exists( "from" ) Then
        FromPage = CInt( Arguments.Item( "from" ) )
    Else
        FromPage = 1
    End if
    
    If Arguments.Exists( "to" ) Then
        ToPage = CInt( Arguments.Item( "to" ) )
    Else
        ToPage = 10000
    End if
        
    Set PS = CreateObject( "Photoshop.Application" )
    
    On Error Resume Next
    
    For Page = FromPage To ToPage
    
        OpenOptions.Page = Page
        
        Set Jpg = PS.Open( InputFilePath, OpenOptions )
        
        If Err.Number <> 0 Then Exit For
        
        Jpg.SaveAs OutputFilePath & Page & ".jpg", SaveOptions, True, 2
        
        Jpg.Close( 2 )
        
    Next
    
Else

    WScript.Echo "No input specified!"&vbCRLF&"Example usage:"&vbCRLF&"pdf2jpg.vbs input.pdf output=out width=400 height=300 quality=9 from=5 to=10"&vbCRLF&"All arguments but the input file are optional."
    
End if


All content on these pages may be used, copied and modifed freely. Questions or comments may be sent to . Also visit zikko.se.