-(void)print{
if (![UIPrintInteractionController isPrintingAvailable]) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Printer Availability Error Title", @"")
message:NSLocalizedString(@"Printer Availability Error Message", @"")
delegate:nil
cancelButtonTitle:NSLocalizedString(@"OK", @"OK")
otherButtonTitles:nil];
[alertView show];
return;
}
UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
if(!pic) {
NSLog(@"Couldn't get shared UIPrintInteractionController!");
return;
}
pic.delegate = self;
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.jobName = @"Sample";
pic.printInfo = printInfo;
NSString *htmlString = [self prepareHTMLText];
UIMarkupTextPrintFormatter *htmlFormatter = [[UIMarkupTextPrintFormatter alloc] initWithMarkupText:htmlString];
htmlFormatter.startPage = 0;
htmlFormatter.contentInsets = UIEdgeInsetsMake(72.0, 72.0, 72.0, 72.0); // 1-inch margins on all sides
htmlFormatter.maximumContentWidth = 6 * 72.0; // printed content should be 6-inches wide within those margins
pic.printFormatter = htmlFormatter;
pic.showsPageRange = YES;
void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =
^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
if (!completed && error) {
NSLog(@"Printing could not complete because of error: %@", error);
}
};
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
// [pic presentFromBarButtonItem:self.myPrintBarButton animated:YES completionHandler:completionHandler];
} else {
[pic presentAnimated:YES completionHandler:completionHandler];
}
}
Add base64.h and .m files to project and import to ur viewcontroller to print an image.
- (NSString *)prepareHTMLText {
NSMutableString *body = [NSMutableString stringWithString:@"<!DOCTYPE html><html><body>"];
NSString *str1 = [Base64 encode:UIImagePNGRepresentation(imageView.image)];
[body appendString:@"<h2>ID Card</h2>"];
[body appendFormat:@"<img src=\"data:image/png;base64,%@\" alt=\"\" height=\"100\" width=\"100\"/>",str1];
[body appendFormat:@"<p>%@ %@</p>", nameField.text, districtField.text];
[body appendString:@"</body></html>"];
return body;
}
No comments:
Post a Comment