Search code examples
iosiphoneios8cordova-pluginscordova-3.8.0

A Plugin to view PDF document in iOS cordova based application


Can please suggest a working plugin to view PDF in iOS Cordova based application.

  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
  • Can any one tell me what is the issue in above method.

  • And how can we provide the path for sample.pdf (Hope I am giving any wrong path)

  • Or if any PDF plugins for iOS Cordova


Solution

  • I always use a "homemade" plugin made by me; it get the PDF on a specific URL by a single GET.

    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
    

    Remember to add in your config.xml

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

    You can call the plugin method on your Javascript file just like this:

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