[英]Print PDF file on iphone or ipad
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]. 如何打印文件我需要打印这个[myView PDFData]。
I found only this for printing: 我发现只有这个用于打印:
NSString *PDFFileWithName = [[NSBundle mainBundle] pathForResource:@"name" ofType:@"pdf"];
NSData *dataFromPath = [NSData dataWithContentsOfFile:PDFFileWithName];
Thanks 谢谢
You should read through the Drawing and Printing Guide for iOS . 您应该阅读适用于iOS的绘图和打印指南 。 The
printingItem
property of UIPrintInteractionController
can be set to the NSData
of a PDF. UIPrintInteractionController
的printingItem
属性可以设置为PDF的NSData
。
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. dataFromPath的值应该等于[myView PDFData],尽管我建议在变量名称工作后更改变量名称。
NSData *dataFromPath = [myView PDFData];
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];
Full code to print pdf 打印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];
Posted the wrong link earlier - this one should help! 之前发布了错误的链接 - 这个应该有帮助!
Blog - Printing in iOS - Goes into great detail and includes a tutorial on Printing PDFs 博客 - 在iOS中打印 - 详细介绍并包含有关打印PDF的教程
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.