PostScript Conversion

The Preview application automatically converts PostScript files to PDF. The Quartz 2D API provides functions you can use to perform PostScript conversion in your application. The Quartz 2D PostScript conversions functions are not available in iOS.

Follow these steps to convert a PostScript document to a PDF document:

  1. Write callbacks. Quartz communicates the status of per page processes through callbacks.

  2. Fill a callbacks structure.

  3. Create a PostScript converter object.

  4. Create a data provider object for the PostScript file you want to convert.

  5. Create a data consumer object for the PDF that results from the conversion.

  6. Perform the conversion.

Each of these steps is discussed in the sections that follow.

Writing Callbacks

Callbacks provide a way for Quartz to inform your application of the status of the conversion. If your application has a user interface, you can use the status information to provide feedback to the user, as shown in Figure 15-1.

Figure 15-1  A status message for a PostScript conversion application
A status message for a PostScript conversion application

You can provide callbacks to inform your application that Quartz 2D is:

See the CGPSConverter Reference for the prototype each callback follows.

Filling In a Callbacks Structure

You need to assign a version number and the callbacks you created to the appropriate fields of the CGPSConverterCallbacks data structure (shown in Listing 15-1). The version is 0. Assign NULL to those fields for which you do not supply a callback.

Listing 15-1  The PostScript converter callbacks data structure

struct CGPSConverterCallbacks {
   unsigned int version;
   CGPSConverterBeginDocumentCallback beginDocument;
   CGPSConverterEndDocumentCallback endDocument;
   CGPSConverterBeginPageCallback beginPage;
   CGPSConverterEndPageCallback endPage;
   CGPSConverterProgressCallback noteProgress;
   CGPSConverterMessageCallback noteMessage;
   CGPSConverterReleaseInfoCallback releaseInfo;
};

Creating a PostScript Converter Object

You call the function CGPSConverterCreate to create a PostScript converter object. This function takes three parameters:

Creating Data Provider and Data Consumer Objects

You create a data provider object by calling the function CGDataProviderCreateWithURL, supplying a CFURL object that specifies the address of the PostScript file you want to convert.

Similarly, you create a data consumer object by calling the function CGDataConsumerCreateWithURL, supplying a CFURL object that specifies the address of the PDF document that results from the conversion.

Performing the Conversion

You call the function CGPSConverterConvert to perform the actual conversion from PostScript to PDF. This function takes as parameters:

The function returns true if the conversion is successful.

You can call the function CGPSConverterIsConverting to check whether the conversion is still progressing.