在iphone或ipad上打印PDF文件

Mar*_*rco 10 printing pdf iphone xcode ipad

我将一个文件附加到我正在使用此代码的邮件中.

[mail addAttachmentData:[myView PDFData] mimeType:@"application/pdf" fileName:@"name.pdf"];
Run Code Online (Sandbox Code Playgroud)

如何打印文件我需要打印这个[myView PDFData].

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

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

NSData *dataFromPath = [NSData dataWithContentsOfFile:PDFFileWithName];
Run Code Online (Sandbox Code Playgroud)

谢谢

Joe*_*Joe 6

您应该阅读适用于iOS绘图和打印指南.该printingItem财产UIPrintInteractionController可以被设置为NSData一个PDF的.

添加代码的更新

dataFromPath的值应该等于[myView PDFData],尽管我建议在变量名称工作后更改变量名称.

NSData *dataFromPath = [myView PDFData];
Run Code Online (Sandbox Code Playgroud)


Alo*_*lok 5

打印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];
Run Code Online (Sandbox Code Playgroud)