Skip to content
Merged
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
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ A react native PDF view component (cross-platform support)

### Feature

* read a PDF from url/local file/asset and can cache it.
* read a PDF from url, blob, local file or asset and can cache it.
* display horizontally or vertically
* drag and zoom
* double tap for zoom
Expand Down Expand Up @@ -253,11 +253,12 @@ import Pdf from 'react-native-pdf';
export default class PDFExample extends React.Component {
render() {
const source = { uri: 'http://samples.leanpub.com/thereactnativebook-sample.pdf', cache: true };
// const source = require('./test.pdf'); // ios only
// const source = { uri:'bundle-assets://test.pdf' };

// const source = { uri: 'file:///sdcard/test.pdf' };
// const source = { uri: "data:application/pdf;base64,JVBERi0xLjcKJc..." };
//const source = require('./test.pdf'); // ios only
//const source = {uri:'bundle-assets://test.pdf' };
//const source = {uri:'file:///sdcard/test.pdf'};
//const source = {uri:"data:application/pdf;base64,JVBERi0xLjcKJc..."};
//const source = {uri:"content://com.example.blobs/xxxxxxxx-...?offset=0&size=xxx"};
//const source = {uri:"blob:xxxxxxxx-...?offset=0&size=xxx"};

return (
<View style={styles.container}>
Expand Down Expand Up @@ -351,8 +352,10 @@ const styles = StyleSheet.create({
| `{uri:"data:application/pdf;base64,JVBERi0xLjcKJc..."}` | load pdf from base64 string | ✔ | ✔ | ✔ |
| `{uri:"file:///absolute/path/to/xxx.pdf"}` | load pdf from local file system | ✔ | ✔ | ✔ |
| `{uri:"ms-appx:///xxx.pdf"}}` | load pdf bundled with UWP app | ✖ | ✖ | ✔ |
| `{uri:"content://com.example.blobs/xxxxxxxx-...?offset=0&size=xxx"}` | load pdf from content URI | ✔* | ✖ | ✖ |
| `{uri:"blob:xxxxxxxx-...?offset=0&size=xxx"}` | load pdf from blob URL | ✖ | ✔ | ✖ |


\*) requires building React Native from source with [this patch](https://github.com/facebook/react-native/pull/31789)
### Methods
* [setPage](#setPage)

Expand Down
23 changes: 21 additions & 2 deletions android/src/main/java/org/wonday/pdf/PdfView.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import java.io.File;

import android.content.ContentResolver;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
Expand Down Expand Up @@ -49,6 +50,9 @@
import com.facebook.react.common.ReactConstants;

import static java.lang.String.format;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.lang.ClassCastException;

import com.shockwave.pdfium.PdfDocument;
Expand Down Expand Up @@ -222,8 +226,23 @@ public void drawPdf() {
Constants.Pinch.MINIMUM_ZOOM = this.minScale;
Constants.Pinch.MAXIMUM_ZOOM = this.maxScale;

Configurator configurator = this.fromUri(getURI(this.path))
.defaultPage(this.page-1)
Configurator configurator;

if (this.path.startsWith("content://")) {
ContentResolver contentResolver = getContext().getContentResolver();
InputStream inputStream = null;
Uri uri = Uri.parse(this.path);
try {
inputStream = contentResolver.openInputStream(uri);
} catch (FileNotFoundException e) {
throw new RuntimeException(e.getMessage());
}
configurator = this.fromStream(inputStream);
} else {
configurator = this.fromUri(getURI(this.path));
}

configurator.defaultPage(this.page-1)
.swipeHorizontal(this.horizontal)
.onPageChange(this)
.onLoad(this)
Expand Down
10 changes: 10 additions & 0 deletions example/PDFExample.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
</intent-filter>
</activity>
<activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
<provider
android:name="com.facebook.react.modules.blob.BlobProvider"
android:authorities="@string/blob_provider_authority"
android:exported="false" />
</application>

</manifest>
1 change: 1 addition & 0 deletions example/android/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<resources>
<string name="app_name">PDFExample</string>
<string name="blob_provider_authority">com.pdfexample.blobs</string>
</resources>
2 changes: 2 additions & 0 deletions ios/RCTPdf/RCTPdfView.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

NS_CLASS_AVAILABLE_IOS(11_0) @interface RCTPdfView : UIView <UIGestureRecognizerDelegate>

- (instancetype)initWithBridge:(RCTBridge *)bridge;

@property(nonatomic, strong) NSString *path;
@property(nonatomic) int page;
@property(nonatomic) float scale;
Expand Down
21 changes: 17 additions & 4 deletions ios/RCTPdf/RCTPdfView.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
#import <React/RCTEventDispatcher.h>
#import <React/UIView+React.h>
#import <React/RCTLog.h>
#import <React/RCTBlobManager.h>
#else
#import "RCTBridgeModule.h"
#import "RCTEventDispatcher.h"
#import "UIView+React.h"
#import "RCTLog.h"
#import <RCTBlobManager.h">
#endif

#ifndef __OPTIMIZE__
Expand All @@ -39,6 +41,7 @@

@implementation RCTPdfView
{
RCTBridge *_bridge;
PDFDocument *_pdfDocument;
PDFView *_pdfView;
PDFOutline *root;
Expand All @@ -47,11 +50,12 @@ @implementation RCTPdfView
NSArray<NSString *> *_changedProps;
}

- (instancetype)init
- (instancetype)initWithBridge:(RCTBridge *)bridge
{
self = [super init];
if (self) {

_bridge = bridge;
_page = 1;
_scale = 1;
_minScale = MIN_SCALE;
Expand Down Expand Up @@ -114,14 +118,23 @@ - (void)didSetProps:(NSArray<NSString *> *)changedProps

if ([changedProps containsObject:@"path"]) {

NSURL *fileURL = [NSURL fileURLWithPath:_path];

if (_pdfDocument != Nil) {
//Release old doc
_pdfDocument = Nil;
}

_pdfDocument = [[PDFDocument alloc] initWithURL:fileURL];

if ([_path hasPrefix:@"blob:"]) {
RCTBlobManager *blobManager = [_bridge moduleForName:@"BlobModule"];
NSURL *blobURL = [NSURL URLWithString:_path];
NSData *blobData = [blobManager resolveURL:blobURL];
if (blobData != nil) {
_pdfDocument = [[PDFDocument alloc] initWithData:blobData];
}
} else {
NSURL *fileURL = [NSURL fileURLWithPath:_path];
_pdfDocument = [[PDFDocument alloc] initWithURL:fileURL];
}

if (_pdfDocument) {

Expand Down
2 changes: 1 addition & 1 deletion ios/RCTPdf/RCTPdfViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ - (UIView *)view
{
if([[[UIDevice currentDevice] systemVersion] compare:@"11.0" options:NSNumericSearch] == NSOrderedDescending
|| [[[UIDevice currentDevice] systemVersion] compare:@"11.0" options:NSNumericSearch] == NSOrderedSame) {
return [[RCTPdfView alloc] init];
return [[RCTPdfView alloc] initWithBridge:self.bridge];
} else {
return NULL;
}
Expand Down