How to AirPrint from your iOS application

What is AirPrint?
“AirPrint is an Apple technology that helps you create full-quality printed output without the need to download or install drivers.” – Apple (Source)

Why do I need it?
Yeah well, when you have content that the user might need to print. 🙂
Besides it is simple and straight forward for most cases. Apple has created various APIs for doing all the hard work for you and most modern printers have AirPrint support which you can take advantage of.

How do I implement AirPrint in my iOS application?
Follow through this blog for details on the implementation.

Before we implement printing in the code, Apple provides a tool for testing how printing works on paper without you having to get a printer or waste paper on it. This is provided as a printer simulator for Xcode. So as the first step, lets enable the iOS printer simulator.

For Xcode 5:

  1. Open iOS Simulator (form Xcode: menu: Xcode -> Developer Tools -> iOS Simulator)
  2. From iOS Simulator: Menu: File -> Open Printer Simulator.

Xcode 5

For Xcode 6:

  1. Download and install Hardware IO Tools for Xcode from Apple developer website. (Requires developer login) Here and search for: hardware IO.
  2. Select and download the appropriate package.
  3. Install the downloaded package. (Drag and drop the “Printer Simulator.app” file to /Applications folder.)
  4. Run Printer Simulator app.

PrinterSimulatorForXcode6

Running the Printer Simulator looks like this.PrinerSimulator

NOTE: The printer simulator need to be running for viewing and selecting printers from your iOS application.

Implementing code in your iOS application

To begin with, we need to know what is the type of content that you want to print. For instance you might want to print a PDF file an Image a HTML page or even a custom drawn view. Apple makes it really simple for optimising and printing PDFs/Images.

Below is the code snippet for printing a PDF file.

- (void)printDocumentFile:(NSURL *)iFilePath {
    // Checking if printing is permitted.
    if ([UIPrintInteractionController canPrintURL:iFilePath]) {
        // Configuring print options
        UIPrintInfo *aPrintInfo = [UIPrintInfo printInfo];
        // Using UIPrintInfoOutputGeneral for:
        // B&W or color, normal quality output for mixed text, graphics, and images
        aPrintInfo.outputType = UIPrintInfoOutputGeneral;
        aPrintInfo.jobName = [iFilePath lastPathComponent]; // Just a name
        // You can also set the orientation and duplex on print info.
        
        // UIPrintInteractionController is what allows you to select the printer, set number of copies and press print.
        UIPrintInteractionController *aPrintController = [UIPrintInteractionController sharedPrintController];
        // Specify the file path
        aPrintController.printingItem = iFilePath;
        aPrintController.printInfo = aPrintInfo;
        aPrintController.showsPageRange = YES;
        [aPrintController presentAnimated:YES completionHandler:NULL];
    }
}

Call the above method passing the path of the PDF file as below.

    // Getting the path of the PDF document.
    NSURL *aFilePath = [[NSBundle mainBundle] URLForResource:@"SamplePDF" withExtension:@"pdf"];
    [self printDocumentFile:aFilePath];

Text Formatting: Formatters

Cocoa also has API’s to allow formatting text for printing.
– For plain text: UISimpleTextFormatter
– For HTML markup text: UIMarkupTextFormatter
Refer to the API reference for more details on this.

Code snippet for printing a HTML file:

#define POINTS_PER_INCH 72
- (void)printHTMLText:(NSString *)text {
    
    UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController];
    UIMarkupTextPrintFormatter *formatter = [[UIMarkupTextPrintFormatter alloc] initWithMarkupText:text];
    formatter.perPageContentInsets = UIEdgeInsetsMake(POINTS_PER_INCH * 0.75f,  // 3/4 inch top margin
                                                      POINTS_PER_INCH * 0.75f,  // 3/4 inch left margin
                                                      POINTS_PER_INCH * 0.75f,  // 3/4 inch bottom margin
                                                      POINTS_PER_INCH * 0.75f); // 3/4 inch right margin
    controller.printFormatter = formatter;
    UIPrintInfo *printInfo = [UIPrintInfo printInfo];
    printInfo.outputType = UIPrintInfoOutputGeneral;
    printInfo.jobName    = @"Job Name";
    controller.printInfo = printInfo;
    [controller presentAnimated:YES completionHandler:NULL];
    
}

iOSPrinterOptions iOSPrinterSelection

NOTE: If you notice a yellow border it is nothing but indicator of printable area on the paper.
You can disable this from Printer Simulator -> Preferences -> Show printable area.
You can also set a location for saving the printed pages. (say desktop)
(By default the printed jobs can be viewed by clicking on the “Show in Finder” button on the top bar of Printer simulator app)

printer simulator preferences

References:

  1. AirPrint for Apple Developers: Contains links to all resources: Guide/API Reference/Videos/Sample code
  2. Apple iOS HIG(Human Interface Guidelines) for Printing
  3. Apple Drawing and Printing Guide
  4. Apple WWDC 2014 Video
  5. Apple Support: Includes all Supported Printers:
  6. Apple Support: How to print from iPhone/iPad:
  7. Detailed blog: NSHipster
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s