Some test text!

Search
Hamburger Icon

Android / Guides

Add an API for Cordova / Ionic

This project is no longer in development. If you are looking for cross-platform solution, check out React Native or Flutter. If you are looking to add to your current Cordova/Ionic project, check out the WebViewer sample.

Introduction

The Apryse Android Cordova API includes all of the most used functions and methods for viewing, annotating and saving PDF documents. However, it is possible your app may need access to APIs that are available as part of the native API, but are not directly available to Cordova.

This guide provides an example of how to add a "save" API to the Cordova interface, which triggers a save of the file and returns the file path of the save file. You can follow the same pattern to add new APIs that your Cordova app may need. The new APIs could be simple ones, which expose one piece of functionality, or custom ones, that expose a series of native commands under the hood.

Example: Adding the "save" API

1. Fork and clone Apryse's Cordova Plugin Repo

The plugin is hosted on GitHub here: https://github.com/PDFTron/pdftron-cordova

Fork the project and clone a copy of the repository to your disk.

2. Update the .js interface file

The .js interface file lists all of the Cordova APIs. It is located, starting at the forked plugin project, at www/PDFTron.js.

Add the following new API:

NativeViewer.prototype.save = function (success, error) {
exec(success, error, 'Apryse', 'save', []);
};

In the code above, success and error are output parameters, Apryse is the name of the module, save is the name of the method, and [] is an empty input parameter list.

3. Define the method string that matches the JS declaration

Open file src/android/com/pdftron/cordova/PDFTron.java.

Add the method key that matches the JS declaration:

public static final String Key_save = "save";

4. Implement the new method

In the same file, src/android/com/pdftron/cordova/PDFTron.java, find entry point execute method:

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    if (Key_save.equals(action)) {
        save(callbackContext);
    }
}

private void save(CallbackContext callbackContext) {
    cordova.getActivity().runOnUiThread(() -> {
        if (mDocumentView != null) {
            try {
                if (mDocumentView.mPdfViewCtrlTabHostFragment != null && mDocumentView.mPdfViewCtrlTabHostFragment.getCurrentPdfViewCtrlFragment() != null) {
                    mDocumentView.mPdfViewCtrlTabHostFragment.getCurrentPdfViewCtrlFragment().save(false, true, true);
                    callbackContext.success(mDocumentView.mPdfViewCtrlTabHostFragment.getCurrentPdfViewCtrlFragment().getFilePath());
                } else {
                    callbackContext.error("Saving failed.");
                }
            } catch (Exception ex) {
                callbackContext.error(ex.getMessage());
            }
        }
    });
}

The actual implementation will depend on the actual functionality.

5. Push the code and integrate the updated plugin

Push your changes back to your forked copy of the repo.

Next, from the command line, navigate to the root of your Cordova app project and remove the Apryse Cordova plugin as follows:

cordova plugin remove pdftron-cordova

Then integrate your forked copy of the repo with the changes, using the appropriate names for your Git(Hub) name/repo:

cordova plugin add https://github.com:MyGithubOrg/pdftron-cordova-fork.git

The new method is now ready to use.

Note that if you later on need to update any code in the plugin (for example to add another method, or to fix a bug), you will need to remove and re-add the Cordova plugin every time in order to make the changes visible to your app.

6. Access the new functionality

The app can now access the new API as follows:

this.viewer.save(function (s) {
    console.log(s);
    fileUrl = "file://" + encodeURI(s);
    console.log(fileUrl);
}, function (e) {
    console.log(e);
});

6. All done!

If you're only developing for Android, then you're all done!

If you're also deploying on iOS, you'll need to repeat steps 3 and 4 for iOS.

If you're developing for both iOS and Android, please consider submitting a PR, as upstreaming the change will simplify your developing and make the API available for other Apryse customers.

Get the answers you need: Chat with us