Cordova Document Viewer Plugin
The plugin is now published at npm with a new plugin id: cordova-plugin-document-viewer
A common requirement when developing a cordova based app is to embed a
performant and secure inline viewer for pdf files which doesn't allow the user
to extract a copy of the pdf file out of the apps sandbox.
Simple delegation to commonly available viewer apps like Acrobat Reader or
MuPDF is no proper solution, as the app looses control over the pdf file in this case.
The external viewer app may or may not provide features to send the document
via email or save it to the devices disk, which is not acceptable.
This plugin offers a slim API to view PDF files which are either stored in the
apps assets folder (/www/*) or in any other file system directory
available via the cordova file plugin
(e.g. cordova.file.applicationDirectory, cordova.file.dataDirectory).
Online files reachable via http(s) are not supported. Download them to a temp
folder before starting the viewer. You may use the onClose
callback
to cleanup the temp dir when the viewer is closed.
Viewer features like "Save as" or "Send via EMail" are configurable at runtime.
Labels for buttons (i18n) are configurable at runtime.
Plugin's Purpose
The purpose of the plugin is to create an platform independent javascript
interface for Cordova based mobile applications to view different
document types by using native viewer components.
Overview
- Supported Platforms
- Installation
- Using the plugin
- Known issues
Supported Platforms
- Cordova/Phonegap >=4.4.0
- iOS 7+
- Android 4.1+
- Windows 8.1
- Windows 10
Installation
The plugin can either be installed from git repository, from local file system
through the Command-line Interface,
or cloud based through PhoneGap Build.
The plugin is published at the npm plugin registry.
Local development environment
From master:
cordova plugin add https://github.com/sitewaerts/cordova-plugin-document-viewer.git
from a local folder:
cordova plugin add cordova-plugin-document-viewer --searchpath path/to/plugin
or to use the last stable version:
cordova plugin add cordova-plugin-document-viewer
or to use a specific version:
cordova plugin add cordova-plugin-document-viewer@[VERSION]
You may replace cordova with phonegap regarding to your needs.
PhoneGap Build
Add the following xml to your config.xml to always use the latest version of this plugin:
<gap:plugin name="cordova-plugin-document-viewer" />
or a specific version:
<gap:plugin name="cordova-plugin-document-viewer" version="[VERSION]"/>
For available versions and additional information visit the npm plugin registry.
Using the plugin
See https://github.com/sitewaerts/cordova-plugin-document-viewer-sample-app for a working example.
The plugin creates the object cordova.plugins.SitewaertsDocumentViewer
.
Plugin initialization
The plugin and its methods are not available before the deviceready event has been fired.
document.addEventListener('deviceready', function () {
}, false);
Common Arguments
url
String pointing to a device local file (e.g. 'file:///...')
mimeType
String representing the mime type of the file. Currently only 'application/pdf' is supported.
options
Some optional features like search or bookmarks are not (yet) implemented on every platform!
var options = {
title: STRING,
documentView : {
closeLabel : STRING
},
navigationView : {
closeLabel : STRING
},
email : {
enabled : BOOLEAN
},
print : {
enabled : BOOLEAN
},
openWith : {
enabled : BOOLEAN
},
bookmarks : {
enabled : BOOLEAN
},
search : {
enabled : BOOLEAN
},
autoClose: {
onPause : BOOLEAN
}
}
Check if a Document File could be shown
cordova.plugins.SitewaertsDocumentViewer.canViewDocument(
url, contentType, options, onPossible, onMissingApp, onImpossible, onError);
onPossible
function onPossible(){
window.console.log('document can be shown');
}
onMissingApp
function onMissingApp(appId, installer)
{
if(confirm("Do you want to install the free PDF Viewer App "
+ appId + " for Android?"))
{
installer();
}
}
onImpossible
function onImpossible(){
window.console.log('document cannot be shown');
}
onError
function onError(error){
window.console.log(error);
alert("Sorry! Cannot show document.");
}
Open a Document File
cordova.plugins.SitewaertsDocumentViewer.viewDocument(
url, mimeType, options, onShow, onClose, onMissingApp, onError);
onShow
function onShow(){
window.console.log('document shown');
}
onClose
function onClose(){
window.console.log('document closed');
}
onMissingApp
function onMissingApp(id, installer)
{
if(confirm("Do you want to install the free PDF Viewer App "
+ appId + " for Android?"))
{
installer();
}
}
onError
function onError(error){
window.console.log(error);
alert("Sorry! Cannot view document.");
}
iOS
The plugin uses the awesome VFRReader (https://github.com/vfr/Reader) to embed pdf viewer functionality in the app.
To open a pdf at a given page number add { page: pageIndex } to the options when calling viewDocument.
Screenshots
![screenshot](https://github.com/sitewaerts/cordova-plugin-document-viewer/raw/HEAD/doc/ios/screenshot02.png)
![screenshot](https://github.com/sitewaerts/cordova-plugin-document-viewer/raw/HEAD/doc/ios/screenshot04.png)
Android
Due to license restrictions in muPDF, the plugin dispatches to a separate
(free) viewer app based on muPDF. If the viewer app is not yet installed, the user may be
redirected to the google play app store.
https://play.google.com/store/apps/details?id=de.sitewaerts.cleverdox.viewer
https://github.com/sitewaerts/android-document-viewer
Screenshots
![screenshot](https://github.com/sitewaerts/cordova-plugin-document-viewer/raw/HEAD/doc/android/screenshot02.png)
Windows
The plugin uses
Screenshots
TODO
Known issues
- Add transparent support for online files.
- The external Viewer App (Android) cannot access files stored in app private directories. Due to this fact, the plugin copies a file to a shared temp folder before starting the viewer. When the viewer is closed, the temp file is immediately deleted. While the viewer is running, a sophisticated user may 'steal' the file from the shared temp directory. We are still searching for a better solution, any good idea is welcome.
- Add support for pdf forms.
- Add fulltext search features.
- Add user bookmark support.
- Add support for additional mime types like dwg, docx, etc.
- Optimize user experience for small screens. Currently the viewer components are tested and optimized on tablets only.
- Let developers provide graphics for buttons at runtime.
Credits
based on https://github.com/vfr/Reader
based on https://github.com/mindstorm/CDVPDFViewer
based on pdf.js
based on winjs
inspired by https://github.com/pebois/phonegap-plugin-PDFViewer
inspired by https://msdn.microsoft.com/en-us/library/windows/apps/dn263105.aspx