I'm trying to implement a routine that converts a PDF document to
image files using ctypes and the Apple CoreGraphics library as per the
'Splitting a PDF File' example on Apple's web site [0]. Unfortunately
I cannot use the CoreGraphics module used in that example because I'm
using Python 2.5 (AFAIK that module is only available in the system
default Python 2.3.5). There are three questions in the code snippet
below. Each problem area has been commented out in the example so it
runs through to the end. The code is obviously not complete, but it's
enough do demonstrate my problems so far.
BTW, this is the only way I have found to convert a PDF (generated by
ReportLab) to an image that can be fed into a PyQt (v3) QPrinter
object. If anyone knows another way to gain access to the system print
services on Mac OS X using Python please do tell. Oh yes, I'd rather
not include PyObjC because I'm already packaging PyQt, and that would
make the resulting app a lot bigger.
# BEGIN CODE
from ctypes import cdll, c_void_p
from ctypes.util import find_library
cglib = cdll.LoadLibrar y(find_library( 'ApplicationSer vices'))
# the next line causes a segfault - what's the right way to do this?
#GCS_RGB = cglib.kCGColorS paceGenericRGB( )
#cs = cglib.CGColorSp aceCreateWithNa me(GCS_RGB)
# the next line causes the following error:
#CGPDFDocumentR ef = cglib.CGPDFDocu mentRef
# AttributeError: dlsym(0x1018c0, CGPDFDocumentRe f): symbol not found
CGPDFDocumentCr eateWithProvide r =
cglib.CGPDFDocu mentCreateWithP rovider
CGPDFDocumentCr eateWithProvide r.restype = c_void_p #CGPDFDocumentR ef
provider = cglib.CGDataPro viderCreateWith Filename("sampl e.pdf")
pdf = CGPDFDocumentCr eateWithProvide r(provider)
#for page in xrange(1, pdf.getNumberOf Pages() + 1):
# print page
# pdf.getNumberOf Pages caues the following error:
# AttributeError: 'int' object has no attribute 'getNumberOfPag es'
# I presume the 'pdf' object is a pointer. How do I get a real
# CGPDFDocuemnt instance that has a getNumberOfPage s method?
cglib.CGPDFDocu mentRelease(pdf )
# END CODE
[0] Splitting a PDF File <http://developer.apple.com/graphicsimaging/
pythonandquartz .html>
Thanks in advance for any hints you can provide.
~ Daniel