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:
Lets get started
- Begin by installing npm if it is not already installed.
- Install the Cordova framework:
install-g Cordova
- Install Plugman which is used to manage plugman Cordova plugins:
npminstall-g plugman
- 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:
Now lets start to modify the code.
- Modify plugin.xml to include a reference to the Foxit MobilePDF SDK framework.
- 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, "FoxitPdfPreview", "Preview", [arg0]);<br /> };</p> <p>var pdf = new pdf();<br /> module.exports = pdf;<br />
This is the current flow of our Cordova plug-in:
- JavaScript call
cordova.plugins.FoxitPdfPreview.preview(arg0, success, error)
- Cordova interface execution
exec(success, error, &quot;FoxitPdfPreview&quot;, &quot;Preview&quot;, arg0);
- 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:
To support rendering a PDF, we need to introduce Foxit’s MobilePDF SDK:
- First, add the mobile PDF SDK header file into the project:
<br />#import &lt;FoxitRDK/FSPDFObjC.h&gt;<br />#import &lt;FoxitRDK/FSPDFViewControl.h&gt;<br />
- Initialize the mobile PDF SDK:
NSString* sn = @&quot;***&quot;;<br />NSString* unlock = @&quot;***&quot;;<br />[FSLibrary init:sn key:unlock];<br />
- Load a document:
//load doc<br /> NSString* docPath= [[NSBundle mainBundle] pathForResource:@"getting_started_ios" ofType:@"pdf"];<br /> FSPDFDoc* doc = [FSPDFDoc createFromFilePath:docPath];<br /> [doc load:nil];<br />
- Display Foxit MobilePDF SDK’s View Control:
FSPDFViewCtrl* myTestViewCtrl;<br />myTestViewCtrl = [[FSPDFViewCtrl alloc] initWithFrame:[[UIScreen mainScreen] bounds]];<br />[myTestViewCtrl setDoc:doc];<br />UIViewController *testctrl = [[UIViewController alloc] init];<br />[testctrl.view addSubview:myTestViewCtrl];<br />testctrl.view.backgroundColor = [UIColor whiteColor];</p><p>//create a navigation bar<br />UINavigationBar *navBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, self.viewController.view.frame.size.width, 64)];<br />[testctrl.view addSubview:navBar];</p><p>// Create an item<br />UINavigationItem *item = [[UINavigationItem alloc]initWithTitle:@&quot;PDF Preview&quot;];<br />navBar.items = @[item];<br />UIBarButtonItem *button = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(close)];<br />item.rightBarButtonItem = button;</p><p>[self.viewController presentViewController:testctrl animated:YES completion:^{<br />NSLog(@&quot;A modal window pops up&quot;);<br />}];<br />
The final code:
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:
<br /> cd ~ / cordovaPlugin< /p > <p> # Create Project<br /> cordova create pdftest<br /> |
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
to the XCode project, we need to double-check the settings to make sure they are correct otherwise there might be some errors.
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
# index.js
At this point, the code has been completed.
Below is the test code to review. We’ll use the
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.
Next, build and run the app. Here’s how it should look:
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!
Is possible to use annotations, bookmarks and full text search with this plugin?
Hi David,
We will be publishing a second article shortly that explains how to integrate the UI Extensions Component as well which contains the annotation, bookmark and full-text search functionality.
Have you had a chance to download the trial version of Foxit MobilePDF SDK yet? If you want to be connected with one of our Solution Engineers please let me know.
Best,
Rowan
Hi Rowan,
I am developing an application for ios and android using Cordova. At the moment I have a plugin to read the pdf files but only has the functionality of bookmarks active, so I would like to know if you could have those features mentioned above..
Hi David,
Shoot me an email at nicolas_derico at foxitsoftware.com and I’ll put you in touch with an engineer to help you with your questions!
Hi David, my apologies for missing your response. Got caught up in end of year madness. I see Nic has responded if you aren’t already in touch with Nic please do so and we can connect to get you up and running with the functionality you need.
Hi David,
If you head on over to https://github.com/foxitsoftware you’ll see that we now have a version of this plugin which supports all of the features that you require.
Cheers,
Rowan