Some test text!

menu
Add an APIkeyboard_arrow_down

Add an API for Cordova / Ionic

linkIntroduction

The PDFTron 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 a series of native commands under the hood.

linkExample: Adding the "save" API

link1. Fork and clone PDFTron'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.

link2. 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, 'PDFTron', 'save', []);
};

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

link3. 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";

link4. 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.

link5. 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 PDFTron 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.

link6. 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);
});

link6. 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 PDFTron customers.

Get the answers you need: Support

close

Free Trial

Get unlimited trial usage of PDFTron SDK to bring accurate, reliable, and fast document processing capabilities to any application or workflow.

Select a platform to get started with your free trial.

Unlimited usage. No email address required.

Join our live demo to learn about use cases & capabilities for WebViewer

Learn more
close