Skip to main content
added 6 characters in body
Source Link

If you drag and drop a pdf onto the application, it moves the file to another folder, then creates a copy of it in another folder, then converts itthe copy to a png of the 1st page from the pdf using the exact same name except *.png, then moves the png to it's final folder.

If you drag and drop a pdf onto the application, it moves the file to another folder, then creates a copy of it in another folder, then converts it to a png of the 1st page from the pdf using the exact same name except *.png, then moves the png to it's final folder.

If you drag and drop a pdf onto the application, it moves the file to another folder, then creates a copy of it in another folder, then converts the copy to a png of the 1st page from the pdf using the exact same name except *.png, then moves the png to it's final folder.

Source Link

I ended up hacking something together using an existing Python3 script, and duct taping it together in Automator.

I'm sure I'll be back here in a few years when Apple breaks it again, but I'll take the win.

If you drag and drop a pdf onto the application, it moves the file to another folder, then creates a copy of it in another folder, then converts it to a png of the 1st page from the pdf using the exact same name except *.png, then moves the png to it's final folder.

It's not pretty, but if anyone needs it, it works.

Here's the Python3 script after I finished butchering it. (I'm sure there's plenty of stuff in there that doesn't need to be there, but it's working, so I'm gonna quit while I'm ahead)

#!/usr/bin/env python3

"""
PDF2PNG v.3.0: Creates a bitmap image from each page of each PDF supplied to it.
by Ben Byram-Wigfield
Now written for python3. You may need to install pyobjc with pip3.
"""
import os, sys
import Quartz as Quartz
# from LaunchServices import (kUTTypeJPEG, kUTTypeTIFF, kUTTypePNG, kCFAllocatorDefault) 

kUTTypeJPEG = 'public.jpeg'
kUTTypeTIFF = 'public.tiff'
kUTTypePNG = 'public.png'
kCFAllocatorDefault = None

resolution = 300.0 #dpi
scale = resolution/72.0

cs = Quartz.CGColorSpaceCreateWithName(Quartz.kCGColorSpaceSRGB)
whiteColor = Quartz.CGColorCreate(cs, (1, 1, 1, 1))
# Options: Quartz.kCGImageAlphaNoneSkipLast (no trans), Quartz.kCGImageAlphaPremultipliedLast 
transparency = Quartz.kCGImageAlphaNoneSkipLast

#Save image to file
def writeImage (image, url, type, options):
    destination = Quartz.CGImageDestinationCreateWithURL(url, type, 1, None)
    Quartz.CGImageDestinationAddImage(destination, image, options)
    Quartz.CGImageDestinationFinalize(destination)
    return

if __name__ == '__main__':

    for filename in sys.argv[1:]:
        filenameNonU = filename.encode('utf8')
        pdf = Quartz.CGPDFDocumentCreateWithProvider(Quartz.CGDataProviderCreateWithFilename(filenameNonU))
        print(filenameNonU)
        numPages = Quartz.CGPDFDocumentGetNumberOfPages(pdf)
        shortName = os.path.splitext(filename)[0]
        prefix = os.path.splitext(os.path.basename(filename))[0]
                # For each page, create a file
        for i in range (1, numPages+0):
            page = Quartz.CGPDFDocumentGetPage(pdf, i)
            if page:    
        #Get mediabox
                mediaBox = Quartz.CGPDFPageGetBoxRect(page, Quartz.kCGPDFMediaBox)
                x = Quartz.CGRectGetWidth(mediaBox)
                y = Quartz.CGRectGetHeight(mediaBox)
                x *= scale
                y *= scale
                r = Quartz.CGRectMake(0,0,x, y)
        # Create a Bitmap Context, draw a white background and add the PDF
                writeContext = Quartz.CGBitmapContextCreate(None, int(x), int(y), 8, 0, cs, transparency)
                Quartz.CGContextSaveGState (writeContext)
                Quartz.CGContextScaleCTM(writeContext, scale,scale)
                Quartz.CGContextSetFillColorWithColor(writeContext, whiteColor)
                Quartz.CGContextFillRect(writeContext, r)
                Quartz.CGContextDrawPDFPage(writeContext, page)
                Quartz.CGContextRestoreGState(writeContext)
        # Convert to an "Image"
                image = Quartz.CGBitmapContextCreateImage(writeContext) 
        # Create unique filename per page
                outFile = filename
                outFile_nonU = outFile.encode('utf8')
                url = Quartz.CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, outFile_nonU, len(outFile_nonU), False)
        # kUTTypeJPEG, kUTTypeTIFF, kUTTypePNG
                type = kUTTypePNG
        # See the full range of image properties on Apple's developer pages.
                options = {
                    Quartz.kCGImagePropertyDPIHeight: resolution,
                    Quartz.kCGImagePropertyDPIWidth: resolution
                    }
                writeImage (image, url, type, options)
            
                del page

And here's the complete Automator workflow. (The Shell dropdown under Run Shell Script is blank because I'm on a different computer which doesn't have Python3, but it should have the python3 path selected there to work)

enter image description here