Skip to content

Commit

Permalink
Initial implmentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Maksym Rusynyk authored and Maksym Rusynyk committed Mar 8, 2018
1 parent 3373ff8 commit 6c714fa
Show file tree
Hide file tree
Showing 17 changed files with 7,751 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .circleci/config.yml
@@ -0,0 +1,10 @@
version: 2
jobs:
build:
docker:
- image: circleci/node:latest
steps:
- checkout
- run: npm install
- run: npm run lint
- run: npm run test
13 changes: 13 additions & 0 deletions .editorconfig
@@ -0,0 +1,13 @@
# EditorConfig is awesome: http://EditorConfig.org

# top-most EditorConfig file
root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true

[**/{.js,package.json}]
indent_style = space
indent_size = 2
8 changes: 8 additions & 0 deletions .eslintrc.json
@@ -0,0 +1,8 @@
{
"parser": "babel-eslint",
"extends": "airbnb",
"rules": {
"no-mixed-operators": 0,
"react/jsx-filename-extension": 0
}
}
40 changes: 40 additions & 0 deletions .gitignore
@@ -0,0 +1,40 @@
# NPM
react-native-pdfview-*.tgz
package/

# OSX
#
.DS_Store

# node.js
#
node_modules/
npm-debug.log
yarn-error.log


# Xcode
#
build/
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata
*.xccheckout
*.moved-aside
DerivedData
*.hmap
*.ipa
*.xcuserstate
project.xcworkspace


# Android
#
build/
.gradle
7 changes: 7 additions & 0 deletions .npmignore
@@ -0,0 +1,7 @@
__tests__
.DS_Store
.editorconfig
.eslintrc.json
coverage
demo
node_modules
77 changes: 77 additions & 0 deletions README.md
@@ -0,0 +1,77 @@

# react-native-pdfview

[![CircleCI](https://circleci.com/gh/rumax/react-native-PDFView.svg?style=shield)](https://circleci.com/gh/rumax/react-native-PDFView)
[![codecov](https://codecov.io/gh/rumax/react-native-PDFView/branch/master/graph/badge.svg)](https://codecov.io/gh/rumax/react-native-PDFView)
[![npm version](https://badge.fury.io/js/react-native-PDFView.svg)](https://badge.fury.io/js/react-native-PDFView)

Library for displaying [PDF documents](https://acrobat.adobe.com/us/en/acrobat/about-adobe-pdf.html) in [react-native](http://facebook.github.io/react-native/)

- android - uses implementation of [Android PdfViewer](https://github.com/barteksc/AndroidPdfViewer)
- ios - To be implemented

## Example

```js
import PdfView from 'react-native-pdfview';

<View>
<PdfView
style={{
height: 360,
width: Dimensions.get('window').width,
}}
onError={(error) => {
console.log('onError', error);
}}
onLoad={() => {
console.log('onLoad');
}}
resource="http://www.pdf995.com/samples/pdf.pdf"
/>
</View>
```

## Getting started

`$ npm install react-native-pdfview --save`

### Mostly automatic installation

`$ react-native link react-native-pdfview`

### Manual installation


#### iOS

TBI

#### Android

1. Open up `android/app/src/main/java/[...]/MainActivity.java`
- Add `import com.reactlibrary.PdfViewPackage;` to the imports at the top of the file
- Add `new RNReactNativePdfViewPackage()` to the list returned by the `getPackages()` method
2. Append the following lines to `android/settings.gradle`:
```
include ':react-native-pdf-view'
project(':react-native-pdf-view').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-pdf-view/android')
```
3. Insert the following lines inside the dependencies block in `android/app/build.gradle`:
```
compile project(':react-native-pdf-view')
```

#### Windows
[ReactWindows](https://github.com/ReactWindows/react-native)

N/A

## License

[MIT](https://opensource.org/licenses/MIT)

### Other information

- Generated with [react-native-create-library](https://github.com/frostney/react-native-create-library)
- Alternatives: [react-native-pdf](https://www.npmjs.com/package/react-native-pdf) has full implementation of [PDFView](https://github.com/barteksc/AndroidPdfViewer) but requires other dependencies and some functionality that is superfluous if you only need to display pdf (using url or base64)
36 changes: 36 additions & 0 deletions android/build.gradle
@@ -0,0 +1,36 @@
apply plugin: 'com.android.library'
description = 'react-native-pdfview'

buildscript {
repositories {
jcenter()
}

dependencies {
classpath 'com.android.tools.build:gradle:1.3.1'
}
}

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"

defaultConfig {
minSdkVersion 19
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
lintOptions {
abortOnError false
}
}

repositories {
mavenCentral()
}

dependencies {
compile 'com.facebook.react:react-native:+'
compile 'com.github.barteksc:android-pdf-viewer:3.0.0-beta.5'
}
3 changes: 3 additions & 0 deletions android/src/main/AndroidManifest.xml
@@ -0,0 +1,3 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.reactlibrary">
</manifest>
87 changes: 87 additions & 0 deletions android/src/main/java/com/reactlibrary/AsyncDownload.java
@@ -0,0 +1,87 @@
package com.reactlibrary;

/**
* Created by Maksym Rusynyk on 06/03/2018.
*
* This source code is licensed under the MIT license
*/

import android.os.AsyncTask;
import android.util.Log;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;

class AsyncDownload extends AsyncTask<Void, Void, Void> {
private static String TAG = "AsyncDownload";
private static int BUFF_SIZE = 8192;
private AsyncTaskCompleted listener;
private File file = null;
private String resource = null;
private IOException exception;

public AsyncDownload(String resource, File file, AsyncTaskCompleted listener) {
this.listener = listener;
this.file = file;
this.resource = resource;
}

@Override
protected void onPreExecute() {
super.onPreExecute();
exception = null;
Log.i(TAG, "onPreExecute");
}

@Override
protected Void doInBackground(Void... params) {
URL url;
URLConnection connection = null;
try {
url = new URL(resource);
connection = url.openConnection();
connection.connect();
} catch (IOException e) {
exception = e;
return null;
}
try (
InputStream input = new BufferedInputStream(url.openStream(), BUFF_SIZE);
OutputStream output = new FileOutputStream(file);
) {
long total = 0;
int count;
Log.d(TAG, "Downloading");
int lengthOfFile = connection.getContentLength();
Log.d(TAG, "length of the file: " + lengthOfFile);
byte data[] = new byte[BUFF_SIZE];

while ((count = input.read(data)) != -1) {
total += count;
output.write(data, 0, count);
}

output.flush();
Log.d(TAG, "total: " + total);
} catch (IOException e) {
exception = e;
}

return null;
}

@Override
protected void onPostExecute(Void param) {
Log.d(TAG, "onPostExecute");
if (exception == null) {
listener.downloadTaskCompleted();
} else {
listener.downloadTaskFailed(exception);
}
}
}
14 changes: 14 additions & 0 deletions android/src/main/java/com/reactlibrary/AsyncTaskCompleted.java
@@ -0,0 +1,14 @@
package com.reactlibrary;

/**
* Created by Maksym Rusynyk on 06/03/2018.
*
* This source code is licensed under the MIT license
*/

import java.io.IOException;

public interface AsyncTaskCompleted {
void downloadTaskCompleted();
void downloadTaskFailed(IOException e);
}

0 comments on commit 6c714fa

Please sign in to comment.