Since this post was published, we have released Foxit PDF SDK, the only true multi-platform SDK in the market, available for Windows, Mac, Linux, iOS, Android, UWP and Web. It features a single, consistent core API, as well as a built-in viewer and UI for faster development. Learn more.

Apache Cordova (also known as Adobe PhoneGap) is a mobile application development framework which enables developers to build apps using CSS3, HTML5, and JavaScript.

In today’s tutorial I am going to show you how to create a Cordova plug-in that uses Foxit MobilePDF SDK to display PDF files. Here’s a quick preview of what the end result will look like:

MobilePDF SDK Cordova Plugin Demo 2

Lets get started

  1. Begin by installing npm if it is not already installed.
  2. Install the Cordova framework:

     

    install
    -g Cordova

     

  3. Install Plugman which is used to manage plugman Cordova plugins:

     

    npm
    install
    -g plugman

     

  4. Download the free Foxit MobilePDF SDK trial.

Now that this preparatory work is complete, we can start running some commands to build our plugin:

 

<br />
cd ~<br />
mkdir cordovaPlugin<br />
cd cordovaPlugin</p>
<p># Create widget<br />
plugman create --name FoxitPreview --plugin_id FoxitPreview --plugin_version 1.0.0</p>
<p># Into the plugin directory<br />
cd FoxitPreview</p>
<p># Plugin.xml increase iOS platform<br />
plugman platform add --platform_name ios<br />

 

The below directory structure is created after the commands for the plug-in to be run successfully:

Structure of Cordova plugin

Now lets start to modify the code.

  1. Modify plugin.xml to include a reference to the Foxit MobilePDF SDK framework.
    Contents of plugin.xml
  2. Open and modify the FoxitPdfPreview.js file to define the method we will use to preview PDFs:

     

    var exec = require('cordova/exec');</p>
    <p>var pdf = function(){};</p>
    <p>pdf.prototype.preview = function(arg0, success, error) {<br />
    exec(success, error, &quot;FoxitPdfPreview&quot;, &quot;Preview&quot;, [arg0]);<br />
    };</p>
    <p>var pdf = new pdf();<br />
    module.exports = pdf;<br />
    

     

This is the current flow of our Cordova plug-in:

  1. JavaScript call

     

    cordova.plugins.FoxitPdfPreview.preview(arg0, success, error)

     

  2. Cordova interface execution

     

    exec(success, error, &amp;quot;FoxitPdfPreview&amp;quot;, &amp;quot;Preview&amp;quot;, arg0);

     

  3. Call the underlying implementation class FoxitPdfPreview for respective platforms (iOS or Android).

Now we need to modify the plugin to introduce code to call Foxit MobilePDF SDK and to render the PDF.

Start with the default generated code like this:

FoxitPdfPreview.m Cordova Plugin Implementation

To support rendering a PDF, we need to introduce Foxit’s MobilePDF SDK:

  1. First, add the mobile PDF SDK header file into the project:

     

    &lt;br /&gt;
    #import &amp;lt;FoxitRDK/FSPDFObjC.h&amp;gt;&lt;br /&gt;
    #import &amp;lt;FoxitRDK/FSPDFViewControl.h&amp;gt;&lt;br /&gt;

     

  2. Initialize the mobile PDF SDK:

     

    NSString* sn = @&amp;quot;***&amp;quot;;&lt;br /&gt;
    NSString* unlock = @&amp;quot;***&amp;quot;;&lt;br /&gt;
    [FSLibrary init:sn key:unlock];&lt;br /&gt;

     

  3. Load a document:

     

    //load doc<br />
    NSString* docPath= [[NSBundle mainBundle] pathForResource:@&quot;getting_started_ios&quot; ofType:@&quot;pdf&quot;];<br />
    FSPDFDoc* doc = [FSPDFDoc createFromFilePath:docPath];<br />
    [doc load:nil];<br />
    

     

  4. Display Foxit MobilePDF SDK’s View Control:

     

    FSPDFViewCtrl* myTestViewCtrl;&lt;br /&gt;
    myTestViewCtrl = [[FSPDFViewCtrl alloc] initWithFrame:[[UIScreen mainScreen] bounds]];&lt;br /&gt;
    [myTestViewCtrl setDoc:doc];&lt;br /&gt;
    UIViewController *testctrl = [[UIViewController alloc] init];&lt;br /&gt;
    [testctrl.view addSubview:myTestViewCtrl];&lt;br /&gt;
    testctrl.view.backgroundColor = [UIColor whiteColor];&lt;/p&gt;
    &lt;p&gt;
    //create a navigation bar&lt;br /&gt;
    UINavigationBar *navBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, self.viewController.view.frame.size.width, 64)];&lt;br /&gt;
    [testctrl.view addSubview:navBar];&lt;/p&gt;
    &lt;p&gt;
    // Create an item&lt;br /&gt;
    UINavigationItem *item = [[UINavigationItem alloc]initWithTitle:@&amp;quot;PDF Preview&amp;quot;];&lt;br /&gt;
    navBar.items = @[item];&lt;br /&gt;
    UIBarButtonItem *button = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(close)];&lt;br /&gt;
    item.rightBarButtonItem = button;&lt;/p&gt;
    &lt;p&gt;[self.viewController presentViewController:testctrl animated:YES completion:^{&lt;br /&gt;
        
    NSLog(@&amp;quot;A modal window pops up&amp;quot;);&lt;br /&gt;
    }];&lt;br /&gt;

     

The final code:

7-72c7bc44-774d-4a02-8f45-77daacdad1d5

This is all of the code required for our Cordova plugin which uses Foxit MobilePDF SDK to display PDF files.

Let’s create a test project and try the plugin

Choose a location to save the test project or just create a test project in Cordova Plugin directory:

 

&lt;br /&gt;
cd
~ / cordovaPlugin&lt;
/p
&gt;
&lt;p&gt;
# Create Project&lt;br /&gt;
cordova create pdftest&lt;br /&gt;

 

Create folder for test project

 

 

9-image2016-12-9-16-18-4

At this point the plugin has been added to the project but there’s a few settings we need to double-check.

Although the plug-in mechanism helps us automatically copy

FoxitRDK.framework

to the XCode project, we need to double-check the settings to make sure they are correct otherwise there might be some errors.

9-a-image2016-12-9-16-24-31

Next, we will modify the HTML and JavaScript code in the widget to add a button and some associated functions for calling the plugin to view the PDF:

#index.html

10-image2016-12-9-16-26-44

# index.js

11-image2016-12-9-16-27-39

At this point, the code has been completed.

Below is the test code to review. We’ll use the

getting_started_ios.pdf

for testing and we’ll presume it is in the root directory (copy the file from the Foxit MobilePDF SDK trial version to this location if it is not there already). Of course you can add the a path to a different file in your own code if you wish.

12-image2016-12-9-16-38-7

Next, build and run the app. Here’s how it should look:

12-cordova-plugin-foxitpreview2

Install the plugin or get the source code

I have added the Cordova plugin to npm for you to try yourself and also uploaded the source code to GitHub. Try it out and let me know if you have any feedback.

NPM Cordova Plugin (cordova-plugin-foxitpdf)

https://www.npmjs.com/package/cordova-plugin-foxitpdf

GitHub Source Code (cordova-plugin-foxitpdf)

https://github.com/foxitsoftware/cordova-plugin-foxitpdf

Thanks for checking out this tutorial!