Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print in iOS 4.2?

Tags:

I want to integrate the print functionality in my app.

The document I want to print will be in .doc or .txt format. I am not very experienced in iPhone development yet, so finding it difficult to implement it by following the Apple documentation.

If someone could help me by posting some sample code, will be a great help.

like image 933
iPhoneDev Avatar asked Dec 07 '10 04:12

iPhoneDev


People also ask

Why can't I find the print option on my iPhone?

Since the iOS 13 and iPadOS update, you no longer find the print icon in the top two rows of your apps' share sheet. There is no PRINT ICON in iOS 13+ and iPadOS. That's because Apple moved the print function to the actions list that sits below the rows of icons.

Can you print on iOS?

Use AirPrint to print wirelessly to an AirPrint-enabled printer from apps such as Mail, Photos, and Safari. Many apps available on the App Store also support AirPrint. iPhone and the printer must be on the same Wi-Fi network.


2 Answers

Check out the Drawing and Printing Guide for iOS -- I linked to the printing section. There's sample code and good links to more sample code there.

Edit: I see now that you indicate you find the documentation difficult to follow.

Word documents are complicated -- you'll need to parse through the data, which is quite difficult.

Text and HTML are easier. I took Apple's example for HTML and changed it for plain text:

- (IBAction)printContent:(id)sender {     UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];     pic.delegate = self;      UIPrintInfo *printInfo = [UIPrintInfo printInfo];     printInfo.outputType = UIPrintInfoOutputGeneral;     printInfo.jobName = self.documentName;     pic.printInfo = printInfo;      UISimpleTextPrintFormatter *textFormatter = [[UISimpleTextPrintFormatter alloc]                                                  initWithText:yourNSStringWithContextOfTextFileHere];     textFormatter.startPage = 0;     textFormatter.contentInsets = UIEdgeInsetsMake(72.0, 72.0, 72.0, 72.0); // 1 inch margins     textFormatter.maximumContentWidth = 6 * 72.0;     pic.printFormatter = textFormatter;     [textFormatter release];     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:sender animated:YES completionHandler:completionHandler];     } else {         [pic presentAnimated:YES completionHandler:completionHandler];     } } 
like image 102
Matthew Frederick Avatar answered Oct 19 '22 07:10

Matthew Frederick


First of all add UIPrintInteractionControllerDelegate and create variable

    UIPrintInteractionController *printController; 

Below code to print all images, documents, excel, powerpoint , pdf files works for me:

[self printItem:SomeData withFilePath:YourFilePath]; 

In above code you provide your NSData of your document/image and URL (filePath) and below further code of printItem:withFilePath: method

-(void)printItem :(NSData*)data withFilePath:(NSString*)filePath{ printController = [UIPrintInteractionController sharedPrintController]; printController.delegate = self;  UIPrintInfo *printInfo = [UIPrintInfo printInfo]; printInfo.outputType = UIPrintInfoOutputGeneral; printInfo.jobName = [NSString stringWithFormat:@""]; printInfo.duplex = UIPrintInfoDuplexLongEdge; printController.printInfo = printInfo; printController.showsPageRange = YES;   //If NSData contains data of image/PDF if(printController && [UIPrintInteractionController canPrintData:data]) {     printController.printingItem = data;  }else{     UIWebView* webView = [UIWebView new];     printInfo.jobName = webView.request.URL.absoluteString;     [webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]]];      printController.printFormatter = webView.viewPrintFormatter;  }      void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {         if (!completed && error) {             //NSLog(@"FAILED! due to error in domain %@ with error code %u", error.domain, error.code);         }     };      // Check wether device is iPad/iPhone , because UIPrintInteractionControllerDelegate has different methods for both devices     if([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {         [printController presentFromRect:self.view.frame inView:self.view animated:YES completionHandler:completionHandler];     }     else {         [printController presentAnimated:YES completionHandler:completionHandler];     } } 

I hope it will help. Thanks

// For Swift and if you want to just print image First create IBoutlet for image

@IBOutlet var qrImage : UIImageView? 

and then on click of print button just add below code

//In your view controller @IBAction func printButton(sender: AnyObject) {      let printInfo = UIPrintInfo(dictionary:nil)     printInfo.outputType = UIPrintInfoOutputType.general     printInfo.jobName = "My Print Job"      // Set up print controller     let printController = UIPrintInteractionController.shared     printController.printInfo = printInfo      // Assign a UIImage version of my UIView as a printing iten     printController.printingItem = self.qrImage?.image      // Do it     printController.present(from: self.view.frame, in: self.view, animated: true, completionHandler: nil) } 
like image 29
Maishi Wadhwani Avatar answered Oct 19 '22 07:10

Maishi Wadhwani