Skip to content

Commit 95a1085

Browse files
authored
Merge pull request #122 from factisresearch/raw-pdf
Add support for reading PDF from byte data (merged)
2 parents 921d907 + 94a1e53 commit 95a1085

File tree

4 files changed

+61
-42
lines changed

4 files changed

+61
-42
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ import 'package:flutter_pdfview/flutter_pdfview.dart';
6363
| onLinkHandle ||| `null` |
6464
| gestureRecognizers ||| `null` |
6565
| filePath ||| |
66+
| pdfData ||| |
6667
| fitPolicy ||| `FitPolicy.WIDTH` |
6768
| enableSwipe ||| `true` |
6869
| swipeHorizontal ||| `false` |

android/src/main/java/io/endigo/plugins/pdfviewflutter/FlutterPDFView.java

+12-5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.util.Map;
1717

1818
import com.github.barteksc.pdfviewer.PDFView;
19+
import com.github.barteksc.pdfviewer.PDFView.Configurator;
1920
import com.github.barteksc.pdfviewer.listener.*;
2021
import com.github.barteksc.pdfviewer.util.Constants;
2122
import com.github.barteksc.pdfviewer.util.FitPolicy;
@@ -37,12 +38,18 @@ public class FlutterPDFView implements PlatformView, MethodCallHandler {
3738

3839
linkHandler = new PDFLinkHandler(context, pdfView, methodChannel, preventLinkNavigation);
3940

40-
if (params.containsKey("filePath")) {
41-
String filePath = (String) params.get("filePath");
42-
43-
Constants.PRELOAD_OFFSET = 3;
41+
Configurator config = null;
42+
if (params.get("filePath") != null) {
43+
String filePath = (String) params.get("filePath");
44+
config = pdfView.fromUri(getURI(filePath));
45+
}
46+
else if (params.get("pdfData") != null) {
47+
byte[] data = (byte[]) params.get("pdfData");
48+
config = pdfView.fromBytes(data);
49+
}
4450

45-
pdfView.fromUri(getURI(filePath))
51+
if (config != null) {
52+
config
4653
.enableSwipe(getBoolean(params, "enableSwipe"))
4754
.swipeHorizontal(getBoolean(params, "swipeHorizontal"))
4855
.password(getString(params, "password"))

ios/Classes/FlutterPDFView.m

+38-35
Original file line numberDiff line numberDiff line change
@@ -67,51 +67,54 @@ - (instancetype)initWithFrame:(CGRect)frame
6767
NSInteger defaultPage = [args[@"defaultPage"] integerValue];
6868

6969
NSString* filePath = args[@"filePath"];
70+
FlutterStandardTypedData* pdfData = args[@"pdfData"];
7071

72+
PDFDocument* document;
7173
if ([filePath isKindOfClass:[NSString class]]) {
72-
NSURL * sourcePDFUrl = [NSURL fileURLWithPath:filePath];
73-
PDFDocument * document = [[PDFDocument alloc] initWithURL: sourcePDFUrl];
74+
NSURL* sourcePDFUrl = [NSURL fileURLWithPath:filePath];
75+
document = [[PDFDocument alloc] initWithURL: sourcePDFUrl];
76+
} else if ([pdfData isKindOfClass:[FlutterStandardTypedData class]]) {
77+
NSData* sourcePDFdata = [pdfData data];
78+
document = [[PDFDocument alloc] initWithData: sourcePDFdata];
79+
}
7480

75-
if (document == nil) {
76-
[_channel invokeMethod:@"onError" arguments:@{@"error" : @"cannot create document: File not in PDF format or corrupted."}];
81+
if (document == nil) {
82+
[_channel invokeMethod:@"onError" arguments:@{@"error" : @"cannot create document: File not in PDF format or corrupted."}];
83+
} else {
84+
_pdfView.autoresizesSubviews = YES;
85+
_pdfView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
86+
_pdfView.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1.0];
87+
BOOL swipeHorizontal = [args[@"swipeHorizontal"] boolValue];
88+
if (swipeHorizontal) {
89+
_pdfView.displayDirection = kPDFDisplayDirectionHorizontal;
7790
} else {
78-
_pdfView.autoresizesSubviews = YES;
79-
_pdfView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
80-
81-
_pdfView.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1.0];
82-
BOOL swipeHorizontal = [args[@"swipeHorizontal"] boolValue];
83-
if (swipeHorizontal) {
84-
_pdfView.displayDirection = kPDFDisplayDirectionHorizontal;
85-
} else {
86-
_pdfView.displayDirection = kPDFDisplayDirectionVertical;
87-
}
91+
_pdfView.displayDirection = kPDFDisplayDirectionVertical;
92+
}
8893

89-
[_pdfView usePageViewController:pageFling withViewOptions:nil];
90-
_pdfView.displayMode = enableSwipe ? kPDFDisplaySinglePageContinuous : kPDFDisplaySinglePage;
91-
_pdfView.document = document;
92-
_pdfView.autoScales = autoSpacing;
93-
94-
NSString* password = args[@"password"];
95-
if ([password isKindOfClass:[NSString class]] && [_pdfView.document isEncrypted]) {
96-
[_pdfView.document unlockWithPassword:password];
97-
}
94+
[_pdfView usePageViewController:pageFling withViewOptions:nil];
95+
_pdfView.displayMode = enableSwipe ? kPDFDisplaySinglePageContinuous : kPDFDisplaySinglePage;
96+
_pdfView.document = document;
97+
_pdfView.autoScales = autoSpacing;
98+
NSString* password = args[@"password"];
99+
if ([password isKindOfClass:[NSString class]] && [_pdfView.document isEncrypted]) {
100+
[_pdfView.document unlockWithPassword:password];
101+
}
98102

99-
NSUInteger pageCount = [document pageCount];
103+
NSUInteger pageCount = [document pageCount];
100104

101-
if (pageCount <= defaultPage) {
102-
defaultPage = pageCount - 1;
103-
}
105+
if (pageCount <= defaultPage) {
106+
defaultPage = pageCount - 1;
107+
}
104108

105-
PDFPage* page = [document pageAtIndex: defaultPage];
106-
[_pdfView goToPage: page];
109+
PDFPage* page = [document pageAtIndex: defaultPage];
110+
[_pdfView goToPage: page];
107111

108-
_pdfView.minScaleFactor = _pdfView.scaleFactorForSizeToFit;
109-
_pdfView.maxScaleFactor = 4.0;
112+
_pdfView.minScaleFactor = _pdfView.scaleFactorForSizeToFit;
113+
_pdfView.maxScaleFactor = 4.0;
110114

111-
dispatch_async(dispatch_get_main_queue(), ^{
112-
[weakSelf handleRenderCompleted:[NSNumber numberWithUnsignedLong: [document pageCount]]];
113-
});
114-
}
115+
dispatch_async(dispatch_get_main_queue(), ^{
116+
[weakSelf handleRenderCompleted:[NSNumber numberWithUnsignedLong: [document pageCount]]];
117+
});
115118
}
116119

117120
if (@available(iOS 11.0, *)) {

lib/flutter_pdfview.dart

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'dart:async';
2+
import 'dart:typed_data';
23

34
import 'package:flutter/foundation.dart';
45
import 'package:flutter/gestures.dart';
@@ -17,7 +18,8 @@ enum FitPolicy { WIDTH, HEIGHT, BOTH }
1718
class PDFView extends StatefulWidget {
1819
const PDFView({
1920
Key key,
20-
@required this.filePath,
21+
this.filePath,
22+
this.pdfData,
2123
this.onViewCreated,
2224
this.onRender,
2325
this.onPageChanged,
@@ -36,7 +38,8 @@ class PDFView extends StatefulWidget {
3638
this.defaultPage = 0,
3739
this.fitPolicy = FitPolicy.WIDTH,
3840
this.preventLinkNavigation = false,
39-
}) : super(key: key);
41+
}) : assert(filePath != null || pdfData != null),
42+
super(key: key);
4043

4144
@override
4245
_PDFViewState createState() => _PDFViewState();
@@ -62,6 +65,7 @@ class PDFView extends StatefulWidget {
6265

6366
/// The initial URL to load.
6467
final String filePath;
68+
final Uint8List pdfData;
6569

6670
final bool enableSwipe;
6771
final bool swipeHorizontal;
@@ -121,23 +125,27 @@ class _PDFViewState extends State<PDFView> {
121125
class _CreationParams {
122126
_CreationParams({
123127
this.filePath,
128+
this.pdfData,
124129
this.settings,
125130
});
126131

127132
static _CreationParams fromWidget(PDFView widget) {
128133
return _CreationParams(
129134
filePath: widget.filePath,
135+
pdfData: widget.pdfData,
130136
settings: _PDFViewSettings.fromWidget(widget),
131137
);
132138
}
133139

134140
final String filePath;
141+
final Uint8List pdfData;
135142

136143
final _PDFViewSettings settings;
137144

138145
Map<String, dynamic> toMap() {
139146
Map<String, dynamic> params = {
140147
'filePath': filePath,
148+
'pdfData': pdfData,
141149
};
142150

143151
params.addAll(settings.toMap());

0 commit comments

Comments
 (0)