Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion PDFView.android.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
import React,{ Component, PropTypes } from 'react';
import React,{ Component } from 'react';
import { requireNativeComponent, View } from 'react-native';
import PropTypes from 'prop-types';

class PDFView extends Component {
constructor(props) {
Expand All @@ -23,6 +24,7 @@ class PDFView extends Component {

PDFView.propTypes = {
...View.propTypes,
rawData: PropTypes.string,
asset: PropTypes.string,
src: PropTypes.string,
pageNumber: PropTypes.number,
Expand Down
4 changes: 3 additions & 1 deletion PDFView.ios.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
import React,{ Component, PropTypes } from 'react';
import React,{ Component } from 'react';
import { requireNativeComponent, View } from 'react-native';
import PropTypes from 'prop-types';

class PDFView extends Component {
constructor(props) {
Expand All @@ -23,6 +24,7 @@ class PDFView extends Component {

PDFView.propTypes = {
...View.propTypes,
rawData: PropTypes.string,
src: PropTypes.string,
path: PropTypes.string,
pageNumber: PropTypes.number,
Expand Down
Binary file added RNPDFView.xcodeproj.zip
Binary file not shown.
1 change: 1 addition & 0 deletions RNPDFView/RNPDFView.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

@property(nonatomic, strong) NSString *src;
@property(nonatomic, strong) NSString *path;
@property(nonatomic, strong) NSString *rawData;
@property(nonatomic, strong) NSNumber *pageNumber;
@property(nonatomic, strong) NSNumber *zoom;

Expand Down
47 changes: 28 additions & 19 deletions RNPDFView/RNPDFView.m
Original file line number Diff line number Diff line change
Expand Up @@ -53,28 +53,37 @@ - (void)removeReactSubview:(UIView *)subview

- (void)reloadPdf
{
if (self.path == (id)[NSNull null] || self.path.length == 0) {
NSLog(@"null path");
} else {
NSLog(@"not null: %@", self.path);
if (self.path != (id)[NSNull null] && self.path.length > 0) {
NSLog(@"not null (path): %@", self.path);

NSURL *pdfURL = [NSURL fileURLWithPath:self.path];
_pdf = CGPDFDocumentCreateWithURL( (__bridge CFURLRef) pdfURL );
_numberOfPages = (int)CGPDFDocumentGetNumberOfPages( _pdf );
pdf = CGPDFDocumentCreateWithURL( (_bridge CFURLRef) pdfURL );
} else if (self.rawData != (id)[NSNull null] && self.rawData.length > 0) {
NSLog(@"not null (data): %lu", self.rawData.length);
NSData *rawPDF = [[NSData alloc] initWithBase64EncodedString:self.rawData options:0];
CFDataRef myPDFData = (__bridge CFDataRef)rawPDF;
CGDataProviderRef provider = CGDataProviderCreateWithCFData(myPDFData);
_pdf = CGPDFDocumentCreateWithProvider(provider);
// CFRelease((__bridge CFTypeRef)rawPDF);
} else {
NSLog(@"null path & rawData");
return;
}

if (self.pageNumber != nil && [self.pageNumber intValue] <= _numberOfPages) {
_page = CGPDFDocumentGetPage( _pdf, [self.pageNumber unsignedIntValue] );
} else {
_page = CGPDFDocumentGetPage( _pdf, 1 );
}

NSLog(@"self.page==NULL? %@",_page==NULL?@"yes":@"no");

_pdfScrollView = [[PDFScrollView alloc] initWithFrame:self.bounds];
_pdfScrollView.PDFScale = 1;
[_pdfScrollView setPDFPage:_page];
[self addSubview:_pdfScrollView];
_numberOfPages = (int)CGPDFDocumentGetNumberOfPages( _pdf );

if (self.pageNumber != nil && [self.pageNumber intValue] <= _numberOfPages) {
_page = CGPDFDocumentGetPage( _pdf, [self.pageNumber unsignedIntValue] );
} else {
_page = CGPDFDocumentGetPage( _pdf, 1 );
}

NSLog(@"self.page==NULL? %@",_page==NULL?@"yes":@"no");

_pdfScrollView = [[PDFScrollView alloc] initWithFrame:self.bounds];
_pdfScrollView.PDFScale = 1;
[_pdfScrollView setPDFPage:_page];
[self addSubview:_pdfScrollView];
}

- (void)setPageNumber:(NSNumber *)pageNumber
Expand Down Expand Up @@ -133,4 +142,4 @@ - (void)layoutSubviews
}
}

@end
@end
1 change: 1 addition & 0 deletions RNPDFView/RNPDFViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ - (UIView *)view

RCT_EXPORT_VIEW_PROPERTY(src, NSString);
RCT_EXPORT_VIEW_PROPERTY(path, NSString);
RCT_EXPORT_VIEW_PROPERTY(rawData, NSString);
RCT_EXPORT_VIEW_PROPERTY(pageNumber, NSNumber);
RCT_EXPORT_VIEW_PROPERTY(zoom, NSNumber);
RCT_EXPORT_VIEW_PROPERTY(onChange, RCTBubblingEventBlock);
Expand Down
38 changes: 31 additions & 7 deletions android/src/main/java/com/keyee/pdfview/PDFViewManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.content.Context;
import android.view.ViewGroup;
import android.util.Log;
import android.util.Base64;
import android.graphics.PointF;

import com.github.barteksc.pdfviewer.PDFView;
Expand All @@ -28,6 +29,7 @@
import com.facebook.react.uimanager.events.RCTEventEmitter;
import com.facebook.react.common.MapBuilder;


import static java.lang.String.format;
import java.lang.ClassCastException;

Expand All @@ -38,6 +40,7 @@ public class PDFViewManager extends SimpleViewManager<PDFView> implements OnPage
Integer pageNumber = 0;
String assetName;
String filePath;
byte[] rawPDFData;


public PDFViewManager(ReactApplicationContext reactContext){
Expand All @@ -64,8 +67,8 @@ public PDFView createViewInstance(ThemedReactContext context) {
showLog("does not has a parent");
}
}
return pdfView;
//return new PDFView(context, null);
// return pdfView;
return new PDFView(context, null);
}

@Override
Expand All @@ -86,7 +89,9 @@ public void loadComplete(int nbPages) {
);
}

private void display(boolean jumpToFirstPage) {
private void display(PDFView pdfView, boolean jumpToFirstPage) {
this.pdfView = pdfView;

if (jumpToFirstPage)
pageNumber = 1;
showLog(format("display %s %s", filePath, pageNumber));
Expand All @@ -109,35 +114,54 @@ private void display(boolean jumpToFirstPage) {
.onPageChange(this)
.onLoad(this)
.load();
} else if (rawPDFData != null) {
pdfView.fromBytes(rawPDFData)
.defaultPage(pageNumber)
//.showMinimap(false)
//.enableSwipe(true)
//.swipeVertical(true)
.enableSwipe(true)
.swipeHorizontal(false)
.onPageChange(this)
.onLoad(this)
.load();
}
}

@ReactProp(name = "asset")
public void setAsset(PDFView view, String ast) {
assetName = ast;
display(false);
display(view, false);
}

@ReactProp(name = "rawData")
public void setRawData(PDFView view, String rawData) {
assetName = null;
filePath = null;
rawPDFData = rawData == null ? null : Base64.decode(rawData, Base64.DEFAULT);
display(view, false);
}

@ReactProp(name = "pageNumber")
public void setPageNumber(PDFView view, Integer pageNum) {
//view.setPageNumber(pageNum);
if (pageNum >= 0){
pageNumber = pageNum;
display(false);
display(view, false);
}
}

@ReactProp(name = "path")
public void setPath(PDFView view, String pth) {
filePath = pth;
display(false);
display(view, false);
}

@ReactProp(name = "src")
public void setSrc(PDFView view, String src) {
//view.setSource(src);
filePath = src;
display(false);
display(view, false);
}

@ReactProp(name = "zoom")
Expand Down
95 changes: 79 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,72 @@
{
"name": "react-native-pdf-view",
"version": "0.4.0",
"description": "A pdf file view component for react native.",
"main": "index.js",
"typings": "./index.d.ts",
"repository": {
"type": "git",
"url": "git+https://github.com/cnjon/react-native-pdf-view.git"
"_args": [
[
{
"raw": "react-native-pdf-view@git+https://git@github.com/mataron/react-native-pdf-view.git",
"scope": null,
"escapedName": "react-native-pdf-view",
"name": "react-native-pdf-view",
"rawSpec": "git+https://git@github.com/mataron/react-native-pdf-view.git",
"spec": "git+https://git@github.com/mataron/react-native-pdf-view.git",
"type": "hosted",
"hosted": {
"type": "github",
"ssh": "git@github.com:mataron/react-native-pdf-view.git",
"sshUrl": "git+ssh://git@github.com/mataron/react-native-pdf-view.git",
"httpsUrl": "git+https://git@github.com/mataron/react-native-pdf-view.git",
"gitUrl": "git://git@github.com/mataron/react-native-pdf-view.git",
"shortcut": "github:mataron/react-native-pdf-view",
"directUrl": "https://git@raw.githubusercontent.com/mataron/react-native-pdf-view/master/package.json"
}
},
"/Users/Home/Projects/mynet-mobile/mynet"
]
],
"_from": "git+https://git@github.com/mataron/react-native-pdf-view.git",
"_id": "react-native-pdf-view@0.4.0",
"_inCache": true,
"_location": "/react-native-pdf-view",
"_phantomChildren": {},
"_requested": {
"raw": "react-native-pdf-view@git+https://git@github.com/mataron/react-native-pdf-view.git",
"scope": null,
"escapedName": "react-native-pdf-view",
"name": "react-native-pdf-view",
"rawSpec": "git+https://git@github.com/mataron/react-native-pdf-view.git",
"spec": "git+https://git@github.com/mataron/react-native-pdf-view.git",
"type": "hosted",
"hosted": {
"type": "github",
"ssh": "git@github.com:mataron/react-native-pdf-view.git",
"sshUrl": "git+ssh://git@github.com/mataron/react-native-pdf-view.git",
"httpsUrl": "git+https://git@github.com/mataron/react-native-pdf-view.git",
"gitUrl": "git://git@github.com/mataron/react-native-pdf-view.git",
"shortcut": "github:mataron/react-native-pdf-view",
"directUrl": "https://git@raw.githubusercontent.com/mataron/react-native-pdf-view/master/package.json"
}
},
"_requiredBy": [
"/"
],
"_resolved": "git+https://git@github.com/mataron/react-native-pdf-view.git#0b786d78617005890c8f1b1c7fb43fdd7d8230f7",
"_shasum": "ad69492bd132a89d9a68fbea1e125d5259436a0e",
"_shrinkwrap": null,
"_spec": "react-native-pdf-view@git+https://git@github.com/mataron/react-native-pdf-view.git",
"_where": "/Users/Home/Projects/mynet-mobile/mynet",
"author": {
"name": "cnjon",
"url": "https://github.com/cnjon"
},
"bugs": {
"url": "https://github.com/cnjon/react-native-pdf-view/issues"
},
"dependencies": {
"prop-types": "15.5.10"
},
"description": "A pdf file view component for react native.",
"devDependencies": {},
"gitHead": "0b786d78617005890c8f1b1c7fb43fdd7d8230f7",
"homepage": "https://github.com/cnjon/react-native-pdf-view#readme",
"keywords": [
"react-component",
"react-native",
Expand All @@ -16,16 +75,20 @@
"pdf",
"view"
],
"author": {
"name": "cnjon",
"url": "https://github.com/cnjon"
},
"license": "MIT",
"bugs": {
"url": "https://github.com/cnjon/react-native-pdf-view/issues"
},
"main": "index.js",
"name": "react-native-pdf-view",
"optionalDependencies": {},
"peerDependencies": {
"@types/react": "*",
"@types/react-native": "*"
}
},
"readme": "# react-native-pdf-view\nReact Native PDF View (cross-platform support)\n\n### Breaking changes\n\n* React native 0.40 moved iOS headers, thus all iOS react import statements has been changed. Use version 0.4.* for react native >=0.40. For earlier version see below breaking change.\n\n* React native 0.19 changed the ReactProps class which led to problems with updating native view properties (see https://github.com/facebook/react-native/issues/5649). These errors are corrected in react-native-pdf-view version 0.2.0. Use version 0.2.* for react native >=0.19 and for earlier react native versions use version 0.1.3.\n\n### Installation\n```bash\nnpm i react-native-pdf-view --save\n\nreact-native link react-native-pdf-view\n```\n* In `android/setting.gradle`\n\n```gradle\n...\ninclude ':PDFView'\nproject(':PDFView').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-pdf-view/android')\n```\n\n* In `android/app/build.gradle`\n\n```gradle\n...\ndependencies {\n ...\n compile project(':PDFView')\n}\n```\n\n* register module (in MainActivity.java)\n\nOn newer versions of React Native (0.18+):\n```java\nimport com.keyee.pdfview.PDFView; // <--- import\n\npublic class MainActivity extends ReactActivity {\n ......\n @Override\n protected List<ReactPackage> getPackages() {\n return Arrays.<ReactPackage>asList(\n new PDFView(), // <------ add here\n new MainReactPackage());\n }\n}\n```\n\nOn older versions of React Native:\n```java\nimport com.keyee.pdfview.PDFView; // <--- import\n\npublic class MainActivity extends Activity implements DefaultHardwareBackBtnHandler {\n ......\n\n @Override\n protected void onCreate(Bundle savedInstanceState) {\n super.onCreate(savedInstanceState);\n mReactRootView = new ReactRootView(this);\n\n mReactInstanceManager = ReactInstanceManager.builder()\n .setApplication(getApplication())\n .setBundleAssetName(\"index.android.bundle\")\n .setJSMainModuleName(\"index.android\")\n .addPackage(new MainReactPackage())\n .addPackage(new PDFView()) // <------ add here\n .setUseDeveloperSupport(BuildConfig.DEBUG)\n .setInitialLifecycleState(LifecycleState.RESUMED)\n .build();\n\n mReactRootView.startReactApplication(mReactInstanceManager, \"ExampleRN\", null);\n\n setContentView(mReactRootView);\n }\n\n ......\n\n}\n```\n\n### Usage\n\nFirst, require it from your app's JavaScript files with:\n```bash\nimport PDFView from 'react-native-pdf-view';\n```\n\n\n### Example\n\n```js\n'use strict';\n\nimport React,{\n Component\n} from 'react';\n\nimport {\n StyleSheet,\n View\n} from 'react-native';\n\nimport PDFView from 'react-native-pdf-view';\n\nexport default class PDF extends Component {\n constructor(props) {\n super(props);\n }\n\n render(){\n <PDFView ref={(pdf)=>{this.pdfView = pdf;}}\n src={\"sdcard/pdffile.pdf\"}\n onLoadComplete = {(pageCount)=>{\n this.pdfView.setNativeProps({\n zoom: 1.5\n });\n }}\n style={styles.pdf}/>\n }\n}\nvar styles = StyleSheet.create({\n pdf: {\n flex:1\n }\n});\n```\n\n\n### Configuration\n\n| Property | Type \t| Default \t\t \t\t\t\t| Description | iOS | Android |\n| ------------- |:-------------:|:------------:\t\t\t\t| ----------- | --- | ------- |\n| path | string \t\t\t| null \t\t\t \t\t\t\t| pdf absolute path| ✔ | ✔ |\n| src | string \t\t\t| null \t\t\t \t\t\t\t| pdf absolute path(`Deprecated`) | ✔ | ✔ |\n| asset | string \t\t\t| null \t\t\t \t\t\t\t| the name of a PDF file in the asset folder | | ✔ |\n| pageNumber \t\t | number \t |\t1 \t\t \t\t\t\t| page index | ✔ | ✔ |\n| zoom \t\t | number \t |\t1.0 \t| zoom scale | ✔ | ✔ |\n| onLoadComplete \t\t\t| function \t | null\t \t\t\t| page load complete,return page count | ✔ | ✔ |\n",
"readmeFilename": "README.md",
"repository": {
"type": "git",
"url": "git+https://github.com/cnjon/react-native-pdf-view.git"
},
"typings": "./index.d.ts",
"version": "0.4.0"
}