ios - 一个在基于 iOS cordova 的应用程序中查看 PDF 文档的插件

标签 ios iphone ios8 cordova-plugins cordova-3.8.0

请推荐一个可以在基于 iOS Cordova 的应用程序中查看 PDF 的工作插件。

  I have tried many of PDF related plugins:

  https://github.com/siegfriedbolz/cordova-plugin-file-opener2.git 
  https://github.com/RandyMcMillan/PDFViewer
   but of no use..My application stopped at loading.

 Lastly with I have tried with pebois/phonegap-plugin-PDFViewer:


  I have installed using : 
   "cordova plugin add com.lesfrancschatons.cordova.plugins.pdfreader" 


  In index.html:

  function onDeviceReady() 
        {

            PDFViewer.open("file://sample.pdf","sample.pdf", function (msg) {
                           console.log(msg);//
                           });
      }


//error on pdfviewer.open method
  • 谁能告诉我上述方法有什么问题吗?

  • 我们如何提供sample.pdf的路径(希望我给出了错误的路径)

  • 或者是否有适用于 iOS Cordova 的 PDF 插件

最佳答案

我总是使用我制作的“自制”插件;它通过单个 GET 获取特定 URL 上的 PDF。

PDFUtilities.m

#import "PDFUtilities.h"


@interface PDFUtilities()

@property (strong, nonatomic) UIDocumentInteractionController *documentInteractionController;

@end


@implementation PDFUtilities

- (void)viewPdf:(CDVInvokedUrlCommand*)command
{   
    if (command.arguments[0] == (id)[NSNull null])
    {
        [self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR] callbackId:command.callbackId];
        return;
    }

    NSString *url = command.arguments[0];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND,(unsigned long)NULL), ^(void)
    {
        NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        NSString *file = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"PDF.pdf"]];

        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

        [request setURL:[NSURL URLWithString:url]];
        [request setHTTPMethod:@"GET"];

        [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
        {
           NSLog(@"pdf downloaded, opening...");

           NSData *pdf = data;

           CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)pdf);
           CGPDFDocumentRef document = CGPDFDocumentCreateWithProvider(provider);

           if (document == nil) {
               // error
           }
           else 
           {
               NSURL *localURL = [NSURL fileURLWithPath:file];

               [pdf writeToFile:file options:NSDataWritingAtomic error:&error];

                self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:localURL];
               [self.documentInteractionController setDelegate:self];
               [self.documentInteractionController presentPreviewAnimated:NO];

           }

           CGDataProviderRelease(provider);
           CGPDFDocumentRelease(document);
        }]; 
    });

    [self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK] callbackId:command.callbackId];
}

- (UIViewController*) documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller
{
    return  [[[[UIApplication sharedApplication] delegate] window] rootViewController];
}

PDFUtilities.h

#import <Cordova/CDVPlugin.h>

@interface PDFUtilities : CDVPlugin <UIDocumentInteractionControllerDelegate>

- (void)viewPdf:(CDVInvokedUrlCommand*)command;

@end

记住添加到您的config.xml

<feature name="PDFUtilities">
        <param name="ios-package" value="PDFUtilities" />
</feature>

您可以在 Javascript 文件上调用插件方法,如下所示:

cordova.exec(
            function(){},
            function(){},
            'PDFUtilities',
            'viewPdf',
            [urlpdf]
);

关于ios - 一个在基于 iOS cordova 的应用程序中查看 PDF 文档的插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30780282/

相关文章:

ios - 我们可以在谷歌地图中的标记周围添加自定义 View 或脉冲环动画吗

ios - 'unsigned long ' UIUserNotificationSettings *' 的隐式转换不允许使用 arc

ios - [UIScreen mainScreen]iOS 7和iOS 8.bounds.size差异

ios - 如何在自定义 tableViewCell 中初始化 UiPickerView?

ios - Swift - 每个 View 中的响应 View ?

iphone - 如何(以编程方式)判断是否有/没有任何支持打开特定文档类型的注册应用程序?

ios - 如何以编程方式将页面添加到我的自定义键盘和类别(类似于 Apple 表情符号键盘)?

iphone - 如何在电子邮件中添加图像而不是作为附件?

ios - 使用 faSTLane 截取我的 React-Native 应用程序的屏幕截图

ios - xcode - 从另一个类调用方法后刷新 TableView