2011-03-25 224 views
7

我附加了一个文件到我使用此代码的邮件中。在iPhone或iPad上打印PDF文件

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

感谢

回答

6

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

更新添加的代码

dataFromPath的价值应该等于[MyView的PDFData]虽然我将建议改变变量名,一旦你得到它的工作。

NSData *dataFromPath = [myView PDFData]; 
+0

这些都是我使用电子邮件的代码(它的工作原理很大)和代码用于需要更改的打印 – Marco 2011-03-26 13:23:42

+0

我已经更新了我的答案,如果您愿意,可以在您的帖子的编辑中重新发布您的代码示例。 – Joe 2011-03-28 18:22:38

+1

我用这段代码做了,实际上很简单..感谢大家:printController.printingItem = [myView PDFData]; – Marco 2011-03-29 00:00:32

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 [email protected]“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]; 
+0

你可以用说明扩展你的答案吗?为什么这段代码能解决问题?没有描述的代码转储很少有助于未来的读者。 – 2016-02-24 11:54:33

2

的完整代码打印PDF

UIPrintInteractionController *pc = [UIPrintInteractionController 
             sharedPrintController]; 
    UIPrintInfo *printInfo = [UIPrintInfo printInfo]; 
    printInfo.outputType = UIPrintInfoOutputGeneral; 
    printInfo.orientation = UIPrintInfoOrientationPortrait; 
    printInfo.jobName [email protected]"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];