Print PDF file on iphone or ipad

11,562

Solution 1

You should read through the Drawing and Printing Guide for iOS. The printingItem property of UIPrintInteractionController can be set to the NSData of a PDF.

Update for added code

The value of dataFromPath should be equal to [myView PDFData] although I would recommend changing the variable name once you get it working.

NSData *dataFromPath = [myView PDFData];

Solution 2

Full code to print pdf

UIPrintInteractionController *pc = [UIPrintInteractionController
                                        sharedPrintController];
    UIPrintInfo *printInfo = [UIPrintInfo printInfo];
    printInfo.outputType = UIPrintInfoOutputGeneral;
    printInfo.orientation = UIPrintInfoOrientationPortrait;
    printInfo.jobName =@"Report";

    pc.printInfo = printInfo;
    pc.showsPageRange = YES;
    pc.printingItem = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://test.com/Print_for_Client_Name.pdf"]];
    // You can use here image or any data type to print.


UIPrintInteractionCompletionHandler completionHandler =
^(UIPrintInteractionController *printController, BOOL completed,
  NSError *error) {
    if(!completed && error){
        NSLog(@"Print failed - domain: %@ error code %ld", error.domain,
              (long)error.code);
    }
};


[pc presentFromRect:CGRectMake(0, 0, 300, 300) inView:self.view animated:YES completionHandler:completionHandler];

Solution 3

write below code and check it

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *pathFolder = [NSString stringWithFormat:@"%@",pdfFileName];
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:pathFolder];
NSURL *targetURL = [NSURL fileURLWithPath:path];

UIPrintInteractionController *pc = [UIPrintInteractionController sharedPrintController];
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.orientation = UIPrintInfoOrientationPortrait;
printInfo.jobName =@“Print”;
printInfo.duplex = UIPrintInfoDuplexLongEdge;

pc.printInfo = printInfo;
pc.showsPageRange = YES;
pc.printingItem = targetURL;

UIPrintInteractionCompletionHandler completionHandler =
    ^(UIPrintInteractionController *printController, BOOL completed,
      NSError *error) {
     if(!completed && error){
         NSLog(@"Print failed - domain: %@ error code %ld", error.domain, (long)error.code);
     }
};
[pc presentFromRect:shareButton.frame inView:self.view animated:YES completionHandler:completionHandler];

Solution 4

Posted the wrong link earlier - this one should help!

Blog - Printing in iOS - Goes into great detail and includes a tutorial on Printing PDFs

Share:
11,562

Related videos on Youtube

Marco
Author by

Marco

Updated on June 04, 2022

Comments

  • Marco
    Marco almost 2 years

    I attached a file to the mail I am using this code.

    [mail addAttachmentData:[myView PDFData] mimeType:@"application/pdf" fileName:@"name.pdf"];
    

    How can I do the same thing for printing a file, I need to print this [myView PDFData].

    I found only this for printing:

    NSString *PDFFileWithName = [[NSBundle mainBundle] pathForResource:@"name" ofType:@"pdf"];
    
    NSData *dataFromPath = [NSData dataWithContentsOfFile:PDFFileWithName];
    

    Thanks

  • Marco
    Marco about 13 years
    I knew that tutorial, it is great, but I cannot find what I need.
  • Marco
    Marco about 13 years
    these are the codes I am using for email (it works greatly) and the code for printing which needs to be changed
  • Joe
    Joe about 13 years
    I have updated my answer you can repost your code example in an edit to your post if you like.
  • Marco
    Marco about 13 years
    I did with this code, it was actually easy.. thanks to everybody :printController.printingItem = [myView PDFData];
  • Artjom B.
    Artjom B. about 8 years
    Can you expand your answer with a description why this code would solve the issue? A code dump without a description rarely helps future readers.

Related