1from CoreFoundation import * 2from Quartz import * 3 4import sys 5import objc 6 7DEBUG=0 8 9class MyConverterData (object): 10 def __init__(self): 11 self.doProgress = False 12 self.abortConverter = False 13 self.outStatusFile = sys.stdout 14 self.converter = None 15 16# Converter callbacks 17 18def begin_document_callback(info): 19 print >>info.outStatusFile, "\nBegin Document\n" 20 21def end_document_callback(info, success): 22 if success: 23 success = "success" 24 else: 25 success = "failed" 26 27 print >>info.outStatusFile, "\nEnd Document: %s\n"%(success,) 28 29def begin_page_callback(info, pageno, page_info): 30 print >>info.outStatusFile, "\nBeginning page %d\n"%(pageno,) 31 if DEBUG: 32 if page_info is not None: 33 CFShow(page_info) 34 35 36def end_page_callback(info, pageno, page_info): 37 print >>info.outStatusFile, "\nEnd page %d\n"%(pageno,) 38 if DEBUG: 39 if page_info is not None: 40 CFShow(page_info) 41 42def progress_callback(info): 43 if info.doProgress: 44 print >>info.outStatusFile.write(".") 45 info.outStatusFile.flush() 46 47 # Here would be a callout to code that 48 # would conceivably return whether to abort 49 # the conversion process. 50 51 #UpdateStatus(converterDataP); 52 53 if info.abortConverter: 54 CGPSConverterAbort(info.converter); 55 print >>info.outStatusFile, "ABORTED!" 56 57 58def message_callback(info, cfmessage): 59 # Extract an ASCII version of the message. Typically 60 # these messages are similar to those obtained from 61 # any PostScript interpreter. Messages of the form 62 # "%%[ Error: undefined; OffendingCommand: bummer ]%%" 63 # are PostScript error messages and are the typical 64 # reason a conversion job fails. 65 66 print >>info.outStatusFile, "\nMessage: %s"%( 67 message) 68 69# Given an input URL and a destination output URL, convert 70# an input PS or EPS file to an output PDF file. This conversion 71# can be time intensive and perhaps should be performed on 72# a secondary thread or by another process. */ 73def convertPStoPDF(inputPSURL, outPDFURL): 74 provider = CGDataProviderCreateWithURL(inputPSURL); 75 consumer = CGDataConsumerCreateWithURL(outPDFURL); 76 77 if provider is None or consumer is None: 78 if provider is None: 79 print >>sys.stderr, "Couldn't create provider" 80 81 if consumer is None: 82 print >>sys.stderr, "Couldn't create consumer" 83 84 return False 85 86 # Setup the info data for the callbacks to 87 # do progress reporting, set the initial state 88 # of the abort flag to false and use stdout 89 # as the file to write status and other information. 90 myConverterData = MyConverterData() 91 myConverterData.doProgress = True 92 myConverterData.abortConverter = False 93 myConverterData.outStatusFile = sys.stdout 94 95 # Create a converter object with myConverterData as the 96 # info parameter and our callbacks as the set of callbacks 97 # to use for the conversion. There are no converter options 98 # defined as of Tiger so the options dictionary passed is None. 99 myConverterData.converter = CGPSConverterCreate(myConverterData, ( 100 begin_document_callback, 101 end_document_callback, 102 begin_page_callback, 103 end_page_callback, 104 progress_callback, 105 message_callback, 106 None, # no release_info_callback 107 ), None) 108 109 if myConverterData.converter is None: 110 print >>sys.stderr, "Couldn't create converter object!" 111 return False 112 113 # There are no conversion options so the options 114 # dictionary for the conversion is None. 115 success = CGPSConverterConvert(myConverterData.converter, 116 provider, consumer, None) 117 if not success: 118 print >>sys.stderr, "Conversion failed!" 119 120 # There is no CGPSConverterRelease function. Since 121 # a CGPSConverter object is a CF object, use CFRelease 122 # instead. 123 return success; 124 125def main(args = None): 126 if args is None: 127 args = sys.argv 128 129 if len(args) != 3: 130 print >>sys.stderr, "Usage: %s inputfile outputfile."%(args[0],) 131 return 0 132 133 # Create the data provider and data consumer. 134 inputURL = CFURLCreateFromFileSystemRepresentation(None, 135 args[1], len(args[1]), False) 136 137 outputURL = CFURLCreateFromFileSystemRepresentation(None, 138 args[2], len(args[2]), False) 139 140 if inputURL is not None and outputURL is not None: 141 convertPStoPDF(inputURL, outputURL) 142 143 return 0 144 145 146if __name__ == "__main__": 147 sys.exit(main()) 148