I'm trying to use this to print a PDF that is online, displayed in a webview. Here is what I have unsuccessfully tried.
Any suggestions? The PDF is something that changes regularly, so I can't just load it into the app.
Code:
-(IBAction)print {
NSString *urlString = @"http://www.lagrangecoc.com/meditation.pdf";
NSURL *url = [NSURL URLWithString:urlString];
NSData *file = [NSData dataWithContentsOfURL:url];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:@"meditation.pdf"];
[file writeToFile:pdfPath atomically:YES];
NSString *path = [[NSBundle mainBundle] pathForResource:pdfPath ofType:@"pdf"];
NSData *myData = [NSData dataWithContentsOfFile:path];
UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
if(pic && [UIPrintInteractionController canPrintData: myData] ) {
pic.delegate = self;
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.jobName = [path lastPathComponent];
printInfo.duplex = UIPrintInfoDuplexLongEdge;
pic.printInfo = printInfo;
pic.showsPageRange = YES;
pic.printingItem = myData;
void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *pic, BOOL completed, NSError *error) {
//self.content = nil;
if (!completed && error) {
NSLog(@"FAILED! due to error in domain %@ with error code %u", error.domain, error.code);
}
};
[pic presentAnimated:YES completionHandler:completionHandler];
}
}