flutter_pdfview 1.5.0-beta.4
flutter_pdfview: ^1.5.0-beta.4 copied to clipboard
A Flutter plugin that provides a PDFView widget on Android and iOS.
flutter_pdfview #
Native PDF View for iOS and Android
Use this package as a library #
1. Depend on it #
Add this to your package's pubspec.yaml file:
dependencies:
flutter_pdfview: ^1.4.5
Trying the 1.5.0 beta
1.5.0-beta.4 continues the Kotlin/Swift line with AcroForm form-field docs
(#303) and onTap
(#133). Pre-releases are not picked up
by a ^ constraint, so pin it explicitly:
dependencies:
flutter_pdfview: 1.5.0-beta.4
Feedback is welcome in #351.
2. Install it #
You can install packages from the command line:
with Flutter:
$ flutter packages get
Alternatively, your editor might support pub get or flutter packages get. Check the docs for your editor to learn more.
3. Import it #
Now in your Dart code, you can use:
import 'package:flutter_pdfview/flutter_pdfview.dart';
Options #
| Name | Android | iOS | Default |
|---|---|---|---|
| defaultPage | ✅ | ✅ | 0 |
| onViewCreated | ✅ | ✅ | null |
| onRender | ✅ | ✅ | null |
| onPageChanged | ✅ | ✅ | null |
| onLoadComplete | ✅ | ✅ | null |
| onDraw | ✅ | ✅ | null |
| onTap | ✅ | ✅ | null |
| onError | ✅ | ✅ | null |
| onPageError | ✅ | ❌ | null |
| onLinkHandle | ✅ | ✅ | null |
| gestureRecognizers | ✅ | ✅ | null |
| filePath | ✅ | ✅ | |
| pdfData | ✅ | ✅ | |
| fitPolicy | ✅ | ✅ | FitPolicy.WIDTH |
| enableSwipe | ✅ | ✅ | true |
| swipeHorizontal | ✅ | ✅ | false |
| password | ✅ | ✅ | null |
| nightMode | ✅ | ❌ | false |
| autoSpacing* | ✅ | ✅ | true |
| pageFling | ✅ | ✅ | true |
| pageSnap | ✅ | ❌ | true |
| preventLinkNavigation | ✅ | ✅ | false |
| backgroundColor | ✅ | ✅ | null |
| minZoom | ✅ | ✅ | 1.0 |
| maxZoom | ✅ | ✅ | 4.0 |
| showScrollIndicators* | ✅ | ✅ | false |
Notes:
showScrollIndicatorsis ignored on iOS while horizontal page-flipping is active (pageFling: truetogether withswipeHorizontal: true).autoSpacingonly adds gaps between pages. It does not change initial zoom orfitPolicy(fixed in #150).
Detecting taps (onTap) #
For a reliable single-tap callback, use the first-class onTap parameter. It
is delivered from the native PDF control on both platforms:
PDFView(
filePath: path,
onTap: () {
// e.g. toggle chrome / app bar
},
)
Do not rely on gestureRecognizers with a TapGestureRecognizer for taps.
Flutter’s platform-view gesture arena often never delivers onTap for embedded
native views (#133).
Keep gestureRecognizers for parent-scroll conflicts (below).
AcroForm / fillable form fields #
This plugin is a viewer, not a form editor. Platform behavior:
| Platform | Engine | Annotation / form field painting |
|---|---|---|
| Android | Pdfium (via AndroidPdfViewer) | Widget appearance streams (/AP) are painted when enableAnnotationRendering is on (always enabled by this plugin). Pdfium does not regenerate appearances from /V + /DA the way Adobe Reader does. |
| iOS | PDFKit | Uses the system renderer; typically more tolerant of incomplete appearances. |
Implication: if a PDF’s form field has a missing, empty, or broken /AP entry, Android can omit that field even when Adobe shows it. A common producer-side failure mode (seen with PDFBox + incremental saves / digital signing) is an object-number collision where the field’s /AP indirect reference is later reused for an XRef stream — the field value is still in the file, but the appearance object is no longer valid. See #303.
Producer workarounds (fix generators such as Apache PDFBox):
- After setting values, generate valid appearance streams (PDFBox:
setValue/ appearance generation; avoid leaving fields without a usable/AP). - Prefer a full rewrite save when possible; after incremental updates (especially signing), re-check that each field’s
/APstill resolves to a Form XObject, not an XRef or other object. - Flatten fields when interactivity is not required (
PDAcroForm.flatten()), so values become normal page content. - Optionally set
/NeedAppearances trueon the AcroForm dictionary — some viewers honor it; Pdfium generally still requires a real appearance stream for reliable display.
There is no plugin API that can repair malformed form appearances without embedding a full PDF rewrite stack (out of scope for this viewer).
Using PDFView inside a scrollable widget #
When a PDFView is embedded in a scrollable parent (SingleChildScrollView,
ListView, PageView, ...), the parent can claim drag gestures before they
reach the native view, so swiping inside the PDF does not work — most notably
on iOS (#265). Pass an
EagerGestureRecognizer to let the PDF view consume gestures within its bounds:
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
PDFView(
filePath: path,
gestureRecognizers: {
Factory<OneSequenceGestureRecognizer>(() => EagerGestureRecognizer()),
},
)
BackdropFilter / ColorFiltered over PDFView (iOS) #
PDFView is a platform view (UiKitView on iOS, hybrid composition on
Android). Flutter composites native views outside the normal Flutter layer
tree, so some widgets that sample or recolor the scene do not apply to the
PDF pixels — especially on iOS.
| Widget | iOS platform view | Notes |
|---|---|---|
ColorFiltered / ShaderMask |
❌ not supported | Official Flutter limitation |
BackdropFilter |
⚠️ partial | Supported with restrictions; needs a recent Flutter |
This is not a bug in flutter_pdfview. Flutter documents it under
iOS platform view composition limitations:
ShaderMask and ColorFiltered are unsupported; BackdropFilter works only
within the constraints of the
iOS Platform View Backdrop Filter design.
Workarounds
-
Color inversion / dark pages — Prefer the plugin API instead of wrapping the view in
ColorFiltered:- Android:
nightMode: true(native invert). - iOS:
nightModeis not available today; use a theme-appropriatebackgroundColor, or invert offline content before load if you control the PDF bytes.
- Android:
-
Blur / glass under a sheet — Place the
BackdropFilterso it samples Flutter-drawn content (not only the hole where the native PDF sits), use a semi-transparentbarrierColor/ scrim, or capture a static preview withPDFViewController.getScreenshotand blur thatImagewith Flutter widgets. -
Static filtered preview — For a one-shot recolor (e.g. thumbnail), take a screenshot and wrap the resulting image:
final path = await controller.getScreenshot('preview.png');
// Then:
ColorFiltered(
colorFilter: const ColorFilter.mode(Colors.grey, BlendMode.saturation),
child: Image.file(File(path)),
);
Wrapping PDFView itself in ColorFiltered will keep painting a solid tint
under the native view on iOS while leaving the PDF contents unchanged
(#213).
Controller Options #
| Name | Description | Parameters | Return |
|---|---|---|---|
| getPageCount | Get total page count | - | Future<int> |
| getCurrentPage | Get current page | - | Future<int> |
| setPage | Go to/Set page | int page |
Future<bool> |
| getCurrentPageSize | Return the width and height of the loaded page | - | Future<Size> |
| getScreenshot | Create a PNG of the contents of the PDFView and save to fileName | String fileName |
Future<String> |
| getPosition | Get the position of the top left of the PDF with respect to the origin (top left of PDFView) | - | Future<Offset> |
| getScale | Get the PDF zoom value, for zooming | - | Future<double> |
| setPosition | Set the position of the top left of the PDF with respect to the origin (top left of PDFView) | Offset position |
Future<bool> |
| setScale | Set the PDF zoom value, for zooming | double scale |
Future<bool> |
| setZoomLimits | Set the minimum, maximum and mid bounds of the zoom limits | double minZoom, double midZoom, double maxZoom |
- |
| reload | Reload the PDF document in the PDFView | - | Future<bool> |
Example #
PDFView(
filePath: path,
enableSwipe: true,
swipeHorizontal: true,
autoSpacing: false,
pageFling: false,
showScrollIndicators: true,
backgroundColor: Colors.grey,
onRender: (_pages) {
setState(() {
pages = _pages;
isReady = true;
});
},
onError: (error) {
print(error.toString());
},
onPageError: (page, error) {
print('$page: ${error.toString()}');
},
onViewCreated: (PDFViewController pdfViewController) {
_controller.complete(pdfViewController);
},
onPageChanged: (int page, int total) {
print('page change: $page/$total');
},
onLoadComplete: (int? pages) {
print('# of pages: $pages');
},
onDraw: (double xOffset, double yOffset, double scale) {
print('onDraw');
},
),
Dependencies #
Android #
iOS (only support> 12.0) #
Future plans #
- Replace barteksc/AndroidPdfViewer with MuPDF or Android Native PDF Renderer.
- Improve documentation
- Support other platforms such as MacOS, Windows, Linux and Web
- Add search functionality
- Improve performance on zooming, page changing
- Improve image quality
- Write more test
