Coder Social home page Coder Social logo

nilesharv / cordova-plugin-document-viewer Goto Github PK

View Code? Open in Web Editor NEW

This project forked from sitewaerts/cordova-plugin-document-viewer

0.0 2.0 0.0 3.38 MB

A Document Viewer cordova/phonegap plugin for iOS and Android

License: MIT License

Java 0.42% Objective-C 6.95% CSS 9.99% JavaScript 82.55% HTML 0.09% Batchfile 0.01%

cordova-plugin-document-viewer's Introduction

Cordova Document Viewer Plugin

This project is currently in beta stage and may only be carefully used in productive environments.

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

  1. Supported Platforms
  2. Installation
  3. Using the plugin
  4. Known issues

Supported Platforms

  • Cordova/Phonegap >=3.7.0

    • iOS 7+
    • Android 4.1+
  • 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.

Local development environment

From master:

# ~~ from master branch ~~
cordova plugin add https://github.com/sitewaerts/cordova-plugin-document-viewer.git

from a local folder:

# ~~ local folder ~~
cordova plugin add de.sitewaerts.cordova.documentviewer --searchpath path/to/plugin

or to use the last stable version:

# ~~ stable version ~~
cordova plugin add de.sitewaerts.cordova.documentviewer

or to use a specific version:

# ~~ stable version ~~
cordova plugin add de.sitewaerts.cordova.documentviewer@[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="de.sitewaerts.cordova.documentviewer" source="plugins.cordova.io" />

or a specific version:

<gap:plugin name="de.sitewaerts.cordova.documentviewer" source="plugins.cordova.io" version="[VERSION]"/>

For available versions and additional information visit the cordova 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 () {
    // cordova.plugins.SitewaertsDocumentViewer is now available
}, 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

options: {
	title: STRING,
	documentView : {
		closeLabel : STRING
	},
	navigationView : {
		closeLabel : STRING
	},
	email : {
		enabled : BOOLEAN
	},
	print : {
		enabled : BOOLEAN
	},
	openWith : {
		enabled : BOOLEAN
	},
	bookmarks : {
		enabled : BOOLEAN
	},
	search : {
		enabled : BOOLEAN
	},
}

Check if a Document File could be shown###

SitewaertsDocumentViewer.canViewDocument(
    url, contentType, options, onPossible, onMissingApp, onImpossible, onError);

onPossible

function(){
  window.console.log('document can be shown');
  //e.g. track document usage
}

onMissingApp

function(appId, installer)
{
    if(confirm("Do you want to install the free PDF Viewer App "
            + appId + " for Android?"))
    {
        installer();
    }
}

onImpossible

function(){
  window.console.log('document cannot be shown');
  //e.g. track document usage
}

onError

function(error){
  window.console.log(error);
  alert("Sorry! Cannot show document.");
}

Open a Document File

SitewaertsDocumentViewer.viewDocument(
    url, mimeType, options, onShow, onClose, onMissingApp, onError);

onShow

function(){
  window.console.log('document shown');
  //e.g. track document usage
}

onClose

function(){
  window.console.log('document closed');
  //e.g. remove temp files
}

onMissingApp

function(id, installer)
{
    if(confirm("Do you want to install the free PDF Viewer App "
            + appId + " for Android?"))
    {
        installer();
    }
} 

onError

function(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.

Screenshots

screenshot    screenshot

screenshot    screenshot

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    screenshot

Windows

The plugin uses Windows.Data.Pdf.PdfDocument to display PDF files.

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 'steel' 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 https://mozilla.github.io/pdf.js/

inspired by https://github.com/pebois/phonegap-plugin-PDFViewer

inspired by https://msdn.microsoft.com/en-us/library/windows/apps/dn263105.aspx

cordova-plugin-document-viewer's People

Contributors

phibo23 avatar regnete avatar

Watchers

James Cloos avatar Krishna Kumar avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.