【问题标题】:Creating a PDF programmatically on the iPhone?在 iPhone 上以编程方式创建 PDF?
【发布时间】:2012-09-21 13:22:08
【问题描述】:

大家好,想知道是否有人能指出我正确的方向。我很好奇如何在 iPhone 上以编程方式创建 PDF。该文档提到了 CGPDFDocumentCreateWithProvider 但我不确定如何使用它。谢谢!

【问题讨论】:

  • 这可能不正确,但我相当确定 iPhone 无法在没有外部服务生成文档的情况下创建 PDF。同样,我不相信如果没有外部服务来呈现输出,就不可能在 iPhone 上查看 PDF。

标签: iphone pdf


【解决方案1】:

同样,我不相信如果没有外部服务来呈现输出,就不可能在 iPhone 上查看 PDF

这是不正确的。您可以在 UIWebView 实例中呈现 PDF 文档,例如:

NSString *urlPathStr = [[NSBundle mainBundle] pathForResource:@"myPDFFile" ofType:@"pdf"];
NSURL *url = [NSURL fileURLWithPath:urlPathStr];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[myWebView loadRequest:urlRequest];

您还可以在 UIView 对象中呈现 PDF 以获得更好的性能。

PDFKit 库尚不适用于 iPhone。

您可以查看QuartzDemo 示例应用程序,了解一些用于呈现和操作 PDF 文档的 Quartz2D 方法。

【讨论】:

    【解决方案2】:

    CGPDFDocumentCreateWithProvider 或 CGPDFDocumentCreateWithURL 是您必须使用的方法。

    我尝试了这个方法 CGPDFDocumentCreateWithURL。它会在你提供给方法的第一个参数的 url 中创建一个 pdf。

    创建后,在 web 视图中加载 url 以查看 pdf。

    您可以使用 cgcontextRef 类在 pdf 中输入内容。

    【讨论】:

      【解决方案3】:

      以下是如何在 iphone 应用程序中本地读取和存储 pdf:

      首先创建 UIWebView 并包含 UIWebViewDelegate>>

        NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
        NSString *documentsPath = [paths objectAtIndex:0];
        NSString *filePath = [documentsPath stringByAppendingPathComponent:@"YourPDF.pdf"]; 
       if(![[NSFileManager defaultManager] fileExistsAtPath:filePath]){
                 // download file , here "https://s3.amazonaws.com/hgjgj.pdf" = pdf downloading link
               NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"https://s3.amazonaws.com/hgjgj.pdf"]];
                  //Store the downloaded file  in documents directory as a NSData format
               [pdfData writeToFile:filePath atomically:YES];
          }
      
           NSURL *url = [NSURL fileURLWithPath:filePath];
           NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
           [yourWebView setUserInteractionEnabled:YES];
           [yourWebView setDelegate:self];
           yourWebView.scalesPageToFit = YES;
           [yourWebView loadRequest:requestObj];
      

      或者,如果您只是想从您的资源文件夹中读取/加载 pdf,那么只需执行以下操作:

           NSString*  filePath= [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"pdf"]];
          /// or you can even read docs file as : pathForResource:@"sample" ofType:@"docx"]
           NSURL *url = [NSURL fileURLWithPath:filePath];
           NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
           [yourWebView setUserInteractionEnabled:YES];
           [yourWebView setDelegate:self];
           yourWebView.scalesPageToFit = YES;
           [yourWebView loadRequest:requestObj];
      

      或者,如果您想从一堆文本和字符串创建自己的 pdf,我是这样做的: http://iosameer.blogspot.com/2012/09/creating-pdf-file-from-simple.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-25
        • 1970-01-01
        • 1970-01-01
        • 2013-05-24
        • 2011-04-05
        相关资源
        最近更新 更多