If you want create a PDF file from web view and print the same , then do the followings
- (void) webViewDidFinishLoad:(UIWebView *)aWebView
{
[self createPDFfromUIView:aWebView saveToDocumentsWithFileName:@"Test"];
}
- (void) createPDFfromUIView:(UIWebView *)aView saveToDocumentsWithFileName:(NSString *)aFilename
{
NSMutableData *pdfData = [NSMutableData data];
UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil);
UIGraphicsBeginPDFPage();
CGContextRef pdfContext = UIGraphicsGetCurrentContext();
[aView.layer renderInContext:pdfContext];
UIGraphicsEndPDFContext();
NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString *documentDirectory = [documentDirectories objectAtIndex:0];
NSString *documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename];
//[pdfData writeToFile:documentDirectoryFilename atomically:YES];// if you want to store the file in document directory
[self printPdf:documentDirectoryFilename]; //for printing PDF
}
- (void) printPdf:(NSString *)path
{
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) {
if (!completed && error)
{
NSLog (@”FAILED! due to error in domain %@ with error code %u”, error.domain, error.code);
}
};
[pic presentAnimated:YES completionHandler:completionHandler];
}
}