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...
The
printingItemproperty ofUIPrintInteractionControlleris documented to support only PDF and image data:If you want to use
UIPrintInteractionControllerto print non-PDF, non-image data such as Office documents, you must instead use theprintFormatterproperty.You can print
UIWebView,UITextViewandMKMapViewcontent via their print formatters without any custom logic. This is documented in the UIViewPrintFormatter documentation:Unfortunately, the view of
QLPreviewControlleris not documented as giving back a validviewPrintFormatter. This means you won't be able to roll your own custom printing code withQLPreviewController. In its place, you could consider using aUIWebViewto render the document.