xcode 在 iPhone 或 iPad 上打印 PDF 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5437809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:39:13  来源:igfitidea点击:

Print PDF file on iphone or ipad

iphonexcodeipadpdfprinting

提问by Marco

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

谢谢

回答by Joe

You should read through the Drawing and Printing Guide for iOS. The printingItemproperty of UIPrintInteractionControllercan be set to the NSDataof a PDF.

您应该通读iOS绘图和打印指南。的printingItem属性UIPrintInteractionController可以设置为NSDataPDF的属性。

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

回答by Alok

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

回答by Chandresh

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

回答by Paul Gleeson

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 的教程