【问题标题】:Print PDF file on iphone or ipad在 iPhone 或 iPad 上打印 PDF 文件
【发布时间】:2011-07-23 05:00:34
【问题描述】:

我在使用此代码的邮件中附加了一个文件。

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

我怎样才能打印一个文件,我需要打印这个 [myView PDFData]。

我发现只有这个用于打印:

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

NSData *dataFromPath = [NSData dataWithContentsOfFile:PDFFileWithName];

谢谢

【问题讨论】:

    标签: iphone xcode ipad pdf printing


    【解决方案1】:

    您应该通读Drawing and Printing Guide for iOSUIPrintInteractionControllerprintingItem 属性可以设置为 PDF 的 NSData

    添加代码的更新

    dataFromPath 的值应该等于 [myView PDFData],不过我建议在变量名正常工作后更改它。

    NSData *dataFromPath = [myView PDFData];
    

    【讨论】:

    • 这些是我用于电子邮件的代码(效果很好)和需要更改的打印代码
    • 我已经更新了我的答案,如果你愿意,你可以将你的代码示例重新发布到你的帖子的编辑中。
    • 我用这段代码做了,实际上很简单。感谢大家:printController.printingItem = [myView PDFData];
    【解决方案2】:

    之前发布了错误的链接 - 这个应该有帮助!

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

    【讨论】:

    • 我知道那个教程,它很棒,但我找不到我需要的东西。
    【解决方案3】:

    编写下面的代码并检查它

    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];
    

    【讨论】:

    • 您能否通过描述为什么此代码可以解决问题来扩展您的答案?没有描述的代码转储很少能帮助未来的读者。
    【解决方案4】:

    打印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];
    

    【讨论】:

      猜你喜欢
      • 2019-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-19
      • 2014-03-20
      • 2011-04-19
      • 1970-01-01
      相关资源
      最近更新 更多