UIPrintInteractionController print issue

4.1k Views Asked by At

I added a action button to the navigationItem of my custom QLPreviewController. when tap on the action button, I present a UIPrintInteractionController. I'm getting files from the Documents directory of my app. There is no issue when preview them. But when I'm printing the same file by tapping the action button, [UIPrintInteractionController canPrintData:data] returns false. But it works, if the file is locating inside my app root.

Below is the code, which execute when tap on the action button.

- (void)tappedPrintButton:(id) sender { 
UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];

NSURL *fileURL = (NSURL *)[self currentPreviewItem];

NSData *data = [NSData dataWithContentsOfURL:fileURL];

if  (pic && [UIPrintInteractionController canPrintData:data] ) {

    pic.delegate = self;



    UIPrintInfo *printInfo = [UIPrintInfo printInfo];

    printInfo.outputType = UIPrintInfoOutputGeneral;

    printInfo.jobName = [(NSURL *)[self.files objectAtIndex:0] lastPathComponent];

    printInfo.duplex = UIPrintInfoDuplexLongEdge;

    pic.printInfo = printInfo;

    pic.showsPageRange = YES;

    pic.printingItem = data;



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

    ^(UIPrintInteractionController *pic, BOOL completed, NSError *error) {

        if (!completed && error)

            NSLog(@"FAILED! due to error in domain %@ with error code %u",

                  error.domain, error.code);

    };

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {

        [pic presentFromBarButtonItem:self.myActionBarButton animated:YES

                    completionHandler:completionHandler];

    } else {

        [pic presentAnimated:YES completionHandler:completionHandler];

    }
}
}

I can not image the issue. Please help...

1

There are 1 best solutions below

0
On

The printingItem property of UIPrintInteractionController is documented to support only PDF and image data:

The object must be an instance of the NSURL, NSData, UIImage, or ALAsset class. An object of the first two types must reference or contain image data or PDF data.

If you want to use UIPrintInteractionController to print non-PDF, non-image data such as Office documents, you must instead use the printFormatter property.

You can print UIWebView, UITextView and MKMapView content via their print formatters without any custom logic. This is documented in the UIViewPrintFormatter documentation:

An instance of the UIViewPrintFormatter class lays out the drawn content of a view for printing. The view’s content can span multiple pages.

Instances of three system classes offer usable view print formatters to applications: UIWebView and UITextView of the UIKit framework, and MKMapView of the Map Kit framework. To obtain a view print formatter for a print job, call the UIView method viewPrintFormatter and initialize the print formatter’s inherited layout properties.

Unfortunately, the view of QLPreviewController is not documented as giving back a valid viewPrintFormatter. This means you won't be able to roll your own custom printing code with QLPreviewController. In its place, you could consider using a UIWebView to render the document.