3

I've really stuck on how to do this, I thought it would be easy, but maybe not!

I want to create a small thumbnail image from the first page of a pdf in automator so I can batch convert a load of them at once. Is that possible? If so, what actions do I need to set up?

2 Answers 2

4

Since there is no way to extract a single page from a PDF in Automator, you can try with this Python script. It will extract the first page of each passed pdf in a temporary pdf file:

#! /usr/bin/python
#
import sys
import os
import tempfile
from Quartz.CoreGraphics import *
from os.path import splitext
from os.path import basename
from os.path import join


def createPDFDocumentWithPath(path):
    return CGPDFDocumentCreateWithURL(CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, path, len(path), False))

def main(argv):

    for input_pdf_filename in argv:
        doc = createPDFDocumentWithPath(input_pdf_filename)
        page_one = CGPDFDocumentGetPage(doc, 1)
        mediaBox = CGPDFPageGetBoxRect(page_one, kCGPDFMediaBox)
        if CGRectIsEmpty(mediaBox):
            mediaBox = None

        file_name, extension = splitext(basename(input_pdf_filename))
        output_path = join(tempfile.gettempdir(), file_name + "_page1" + extension)
        writeContext = CGPDFContextCreateWithURL(CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, output_path, len(output_path), False), None, None)
        CGContextBeginPage(writeContext, mediaBox)
        CGContextDrawPDFPage(writeContext, page_one)
        CGContextEndPage(writeContext)
        CGPDFContextClose(writeContext)
        del writeContext

        print output_path


if __name__ == "__main__":
    main(sys.argv[1:])

You can put it as a Run Shell Script step in an Automator workflow, similar to what @Graham Miln suggested:

enter image description here

Be sure to set Pass Input to as arguments, not to to stdin in the Run Shell Script step.

1
  • That is so incredibly inspired - thanks for taking the time to do that, works absolutely perfectly!
    – Osu
    Commented Oct 27, 2012 at 7:35
3

The following Automator workflow creates thumbnails for every page:

  1. Get Specified Finder Items
  2. Render PDF Pages as Images
  3. Scale Images
  4. Move Finder Items

With the thumbnails created, you can then delete all but the first page images. You may be able to automate this step as well; consider adding a Filter Finder Items Action before the Move Finder Items.

enter image description here

1
  • The script saved my life way back. Unfortunately, after all these years, I had to update my computer to Monterey, and now it doesn't work anymore. :( I assume it's probably an easy fix for someone who knows what they are doing, but unfortunately, I am not that someone. Commented Jan 19, 2023 at 8:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .