The UIWebView class provides a lot of useful functions to manage complex documents, html, .doc, .pdf etc etc

In this article we will see how to print its content.

webView is the UIWebView I want to print.

  • Show the printing user interface.

UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];

 

  • Prepare a print-information object initialized with default values.

UIPrintInfo *printInfo = [UIPrintInfo printInfo];

  • Set the output type.

printInfo.outputType = UIPrintInfoOutputGeneral;

 

pic.printInfo = printInfo;

the output type could assume the following values:

        UIPrintInfoOutputGeneral     Text, Graphics and images.

        UIPrintInfoOutputPhoto       Black and white or color images

        UIPrintInfoOutputGrayscale   content is in gray scale.

  • Lay out the drawn content of the view for printing.

pic.printFormatter = [webView viewPrintFormatter];

  • Show the page range

pic.showsPageRange = YES;

  • Wait for completion

            void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =

            ^(UIPrintInteractionController *printController, BOOL completed, NSError *error)

            {

                if (!completed && error)

                {

                    NSLog(@"Printing could not complete because of error: %@", error);

                }

            };

            [pic presentAnimated:YES completionHandler:completionHandler];

That's all, it's easy and fast. You can try this code also on simulator using the Printer Simulator.

 

If you found useful this article, please share it using the social buttons below. Thank you in advance.

Gg1