打印不适用于ipad

时间:2010-12-28 06:02:37

标签: iphone objective-c printing ipad

我的应用程序通过进纸操作被调用,但没有打印出来,纸张出现空白..我使用以下代码进行打印。

  -(IBAction)printButtonAction:(id)sender{
 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

 NSString *documentsDirectory = [paths objectAtIndex:0];

 NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"Preview.pdf"];

 NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:writableDBPath]];

 UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController];
 UIPrintInfo *printInfo = [UIPrintInfo printInfo];
 printInfo.outputType = UIPrintInfoOutputGeneral;
 printInfo.jobName = [writableDBPath lastPathComponent];
 printInfo.duplex = UIPrintInfoDuplexLongEdge;
 controller.printInfo = printInfo;
 controller.showsPageRange = YES;
 controller.printingItem = data;

 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);
 };


 UIViewPrintFormatter *viewFormatter = [documentView viewPrintFormatter];
    viewFormatter.startPage = 0;
    controller.printFormatter = viewFormatter;

 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  [controller presentFromBarButtonItem:printButton animated:YES
         completionHandler:completionHandler];
 } else {
  [controller presentAnimated:YES completionHandler:completionHandler];
 }



}

谢谢

Deepika jain

1 个答案:

答案 0 :(得分:0)

我认为以下代码可行,

 -(IBAction)printButtonAction:(id)sender{
    float systemVersion = [[[UIDevice currentDevice] systemVersion] floatValue];

if (systemVersion>4.1) {

    NSData *myPdfData = [NSData dataWithContentsOfFile:pdfPath]; //check the value inside |myPdfData| and |pdfPath| is the path of your pdf. 
    UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController];
    if (controller && [UIPrintInteractionController canPrintData:myPdfData]){
        controller.delegate = delegate; //if necessary else nil
        UIPrintInfo *printInfo = [UIPrintInfo printInfo]; 
        printInfo.outputType = UIPrintInfoOutputGeneral; 
        printInfo.jobName = [pdfPath lastPathComponent];    
        //printInfo.duplex = UIPrintInfoDuplexLongEdge; 
        controller.printInfo = printInfo;
        controller.showsPageRange = YES;
        controller.printingItem = myPdfData;    

        // We need a completion handler block for printing.
        UIPrintInteractionCompletionHandler completionHandler = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
            if(completed && error){
                NSLog(@"FAILED! due to error in domain %@ with error code %u", error.domain, error.code);
            }
        };

        [controller presentFromRect:rect inView:senderView animated:YES completionHandler:completionHandler];
    }else {
        NSLog(@"Couldn't get shared UIPrintInteractionController!");
    }
}   

检查你提取的pdf的路径,我想你错过了Documents目录后的'/'。您尝试设置断点并检查路径,然后检查| data |来自您指定路径的内容的变量。试试吧,回复你的评论。 :)