native_pdf_view 3.6.2

  • Readme
  • Changelog
  • Example
  • Installing
  • 96

native_pdf_view #

Flutter Plugin to render PDF and show a PDF file on Web, MacOs 10.11+, Android 5.0+ and iOS.

Showcase #

LiveScreenshot

Getting Started #

In your flutter project add the dependency:

pub package

dependencies:
  native_pdf_view: any

For web add lines in index.html before importing main.dart.js:

<script src="//cdnjs.cloudflare.com/ajax/libs/pdf.js/2.4.456/pdf.min.js"></script>
<script type="text/javascript">
  pdfjsLib.GlobalWorkerOptions.workerSrc = "//cdnjs.cloudflare.com/ajax/libs/pdf.js/2.4.456/pdf.worker.min.js";
</script>

Usage example #

It very simple!

import 'package:native_pdf_view/native_pdf_view.dart';

final pdfController = PdfController(
  document: PdfDocument.openAsset('assets/sample.pdf'),
);

Widget pdfView() => PdfView(
  controller: pdfController,
);

Api #

PdfController #

ParameterDescriptionDefault
documentThe document to be displayed-
initialPageThe page to show when first creating the [PdfView]1
viewportFractionThe fraction of the viewport that each page should occupy.1.0

PdfView #

ParameterDescriptionDefault
controllerPages control. See page control and additional pdf info-
onPageChangedCalled whenever the page in the center of the viewport changes. See Document callbacks-
onDocumentLoadedCalled when a document is loaded. See Document callbacks-
onDocumentErrorCalled when a document loading error. Exception is passed in the attributes-
documentLoaderWidget showing when pdf document loadingSizedBox()
pageLoaderWidget showing when pdf page loadingSizedBox()
builderCallback called to render a widget for each page. See custom page builderDefault builder
errorBuilderShow document loading error message inside PdfViewCentered error text
rendererCustom PdfRenderer library options. See custom renderer optionswidth: page.width * 2
height: page.height * 2
format: PdfPageFormat.JPEG
backgroundColor: '#ffffff'
scrollDirectionPage turning directionAxis.horizontal
physicsHow the widgets should respond to user input-
pageSnappingSet to false for mouse wheel scroll on webtrue

Additional examples #

Open another document #

pdfController.openDocument(PdfDocument.openAsset('assets/sample.pdf'));

Page control: #

// Jump to specified page
pdfController.jumpTo(3);

// Animate to specified page
_pdfController.animateToPage(3, duration: Duration(milliseconds: 250), curve: Curves.ease);

// Animate to next page 
_pdfController.nextPage(duration: Duration(milliseconds: 250), curve: Curves.easeIn);

// Animate to previous page
_pdfController.previousPage(duration: Duration(milliseconds: 250), curve: Curves.easeOut);

Additional pdf info: #

// Actual showed page
pdfController.page;

// Count of all pages in document
pdfController.pagesCount;

Document callbacks #

int _actualPageNumber = 0, _allPagesCount = 0;

PdfView(
  controller: pdfController,
  onDocumentLoaded: (document) {
    setState(() {
      _allPagesCount = document.pagesCount;
    });
  },
  onPageChanged: (page) {
    setState(() {
      _actualPageNumber = page;
    });
  },
);

/// Now you can use these values to display the reading status of the document.
Text('Read: $_actualPageNumber of $_allPagesCount');

Custom renderer options #

PdfView(
  controller: pdfController,
  renderer: (PdfPage page) => page.render(
    width: page.width * 2,
    height: page.height * 2,
    format: PdfPageFormat.JPEG,
    backgroundColor: '#FFFFFF',
  ),
);

Custom page builder: #

PdfView(
  controller: pdfController,
  document: snapshot.data,
  pageBuilder: (
    PdfPageImage pageImage, 
    bool isCurrentIndex, 
    AnimationController animationController,
  ) {
    // Double tap scales
    final List<double> _doubleTapScales = <double>[1.0, 2.0, 3.0]
    // Double tap animation
    Animation<double> _doubleTapAnimation;
    void Function() _animationListener;

    Widget image = ExtendedImage.memory(
      pageImage.bytes,
      key: Key(pageImage.hashCode.toString()),
      fit: BoxFit.contain,
      mode: ExtendedImageMode.gesture,
      initGestureConfigHandler: (_) => GestureConfig(
        minScale: 1,
        maxScale: 3.0,
        animationMinScale: .75,
        animationMaxScale: 3.0,
        speed: 1,
        inertialSpeed: 100,
        inPageView: true,
        initialScale: 1.0,
        cacheGesture: false,
      ),
      onDoubleTap: (ExtendedImageGestureState state) {
        final pointerDownPosition = state.pointerDownPosition;
        final begin = state.gestureDetails.totalScale;
        double end;

        _doubleTapAnimation?.removeListener(_animationListener);

        animationController
          ..stop()
          ..reset();

        if (begin == _doubleTapScales[0]) {
          end = _doubleTapScales[1];
        } else {
          if (begin == _doubleTapScales[1]) {
            end = _doubleTapScales[2];
          } else {
            end = _doubleTapScales[0];
          }
        }

        _animationListener = () {
          //print(_animation.value);
          state.handleDoubleTap(
              scale: _doubleTapAnimation.value,
              doubleTapPosition: pointerDownPosition);
        };
        _doubleTapAnimation = animationController
            .drive(Tween<double>(begin: begin, end: end))
              ..addListener(_animationListener);

        animationController.forward();
      },
    );
    if (isCurrentIndex) {
      image = Hero(
        tag: 'pdf_view' + pageImage.pageNumber.toString(),
        child: image,
      );
    }
    return image;
  },
);

Rendering additional info #

On Web #

This plugin uses the PDF.js

On Android #

This plugin uses the Android native PdfRenderer

On Ios & MacOs #

This plugin uses the IOS native CGPDFPage

3.6.2 #

  • Fixed issue#78, loadDocument in PdfController not working

3.6.1 #

  • Fixed issue#74, min ios version now 2 instead 11
  • Fixed issue#76, page width & height exception on web
  • Update dependency extended_image

3.6.0 #

  • Fixed errorBuilder (usage object as type instead Exception)
  • Added option loaderSwitchDuration
  • Fixed readme

3.5.2 #

3.5.1 #

  • Update dependency extended_image

3.5.0+1 #

  • Docs improvement

3.5.0 #

  • Scroll with mouse wheel for web issue#69

3.4.0 #

  • MacOs supported!

3.3.0 #

  • Web supported!

3.2.1 #

  • Fixed issue#65 PdfController.jumpToPage

3.2.0 #

  • Set minimal flutter version to 1.17.0

3.2.0-dev.1 #

  • Adapted 3.1.0 for flutter sdk > 1.16.0

3.1.0 #

  • Added pageSnapping, physics issue#58
  • Fixed errorBuilder

3.1.0-dev.2 #

  • Adapted 3.0.1 for flutter sdk > 1.16.0

3.0.1 #

3.1.0-dev.1 #

  • Fix build error on high flutter sdk (> 1.16.0)

3.0.0 #

  • Added more docs & examples in readme
  • Fixed issue#54, issue#39
  • render property work fixed
  • Renamed PDFView to PdfView
  • Removed constructor PDFView.builder, builder property now available in PdfView constructor
  • Added double tap animation, a
  • Added third step for double tap
  • Added PdfController, document loading now happens through it
  • Property controller now requires PdfController instead PageController (methods and properties from PageController saved in PdfController)
  • loader property replaced to documentLoader and pageLoader
  • Added properties:
    1. onDocumentLoaded(PdfDocument document) - calls on document loaded
    2. onDocumentError(Exception error) - calls on loading document error
    3. Widget errorBuilder(Exception error) - show document loading error in PdfView
  • In PDFViewPageBuilder (builder property) added animationController argument for animate double tap
  • Updated [extended_image] package

2.3.0-dev.1 #

  • Fix build error on high flutter sdk (> 1.16.0)

2.2.0 #

  • Set minimal flutter version to 1.10
  • Upgrade packages
  • Fixed bugs with crashes issue#14, issue#16

2.1.1 #

  • Target sdk version for android upped to 28

2.1.0 #

  • Added onPageChanged callback

2.0.0 #

  • Removed default padding

2.0.0-dev.1 #

  • Rewritten again with using package [native_pdf_renderer]
  • Usage [extended_image] instead [photo_view]
  • Optimized page rendering speed

1.0.2 #

  • Update example readme

1.0.1 #

  • Fix package details

1.0.0 #

  • Initial release

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:native_pdf_view/native_pdf_view.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  int _actualPageNumber = 1, _allPagesCount = 0;
  PdfController _pdfController;

  @override
  void initState() {
    _pdfController = PdfController(
      document: PdfDocument.openAsset('assets/sample.pdf'),
    );
    super.initState();
  }

  @override
  void dispose() {
    _pdfController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) => MaterialApp(
        theme: ThemeData(primaryColor: Colors.white),
        home: Scaffold(
          appBar: AppBar(
            title: Text('PdfView example'),
            actions: <Widget>[
              IconButton(
                icon: Icon(Icons.navigate_before),
                onPressed: () {
                  _pdfController.previousPage(
                    curve: Curves.ease,
                    duration: Duration(milliseconds: 100),
                  );
                },
              ),
              Container(
                alignment: Alignment.center,
                child: Text(
                  '$_actualPageNumber/$_allPagesCount',
                  style: TextStyle(fontSize: 22),
                ),
              ),
              IconButton(
                icon: Icon(Icons.navigate_next),
                onPressed: () {
                  _pdfController.nextPage(
                    curve: Curves.ease,
                    duration: Duration(milliseconds: 100),
                  );
                },
              ),
            ],
          ),
          body: PdfView(
            documentLoader: Center(child: CircularProgressIndicator()),
            pageLoader: Center(child: CircularProgressIndicator()),
            controller: _pdfController,
            onDocumentLoaded: (document) {
              setState(() {
                _allPagesCount = document.pagesCount;
              });
            },
            onPageChanged: (page) {
              setState(() {
                _actualPageNumber = page;
              });
            },
          ),
        ),
      );
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  native_pdf_view: ^3.6.2

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:native_pdf_view/native_pdf_view.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
92
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
96
Learn more about scoring.

We analyzed this package on Jul 3, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.13
  • Flutter: 1.17.5

Analysis suggestions

Package does not support Flutter platform android

Because of import path [package:native_pdf_view/native_pdf_view.dart, package:native_pdf_view/src/native_pdf_view.dart, package:extended_image/extended_image.dart, package:extended_image/src/image/extended_render_image.dart, package:extended_image/src/extended_image_typedef.dart, package:extended_image/src/gesture/extended_image_slide_page.dart, package:extended_image/src/gesture/extended_image_slide_page_handler.dart, package:extended_image/src/gesture/extended_image_gesture_utils.dart, package:extended_image/src/extended_image_utils.dart, package:extended_image/src/extended_image.dart, package:extended_image/src/editor/extended_image_editor.dart, package:extended_image_library/extended_image_library.dart, package:extended_image_library/src/extended_network_image_utils.dart, package:extended_image_library/src/extended_network_image_provider.dart, package:extended_image_library/src/_network_image_io.dart, package:path_provider/path_provider.dart, package:path_provider_linux/path_provider_linux.dart] that declares support for platforms: linux

Package does not support Flutter platform ios

Because of import path [package:native_pdf_view/native_pdf_view.dart, package:native_pdf_view/src/native_pdf_view.dart, package:extended_image/extended_image.dart, package:extended_image/src/image/extended_render_image.dart, package:extended_image/src/extended_image_typedef.dart, package:extended_image/src/gesture/extended_image_slide_page.dart, package:extended_image/src/gesture/extended_image_slide_page_handler.dart, package:extended_image/src/gesture/extended_image_gesture_utils.dart, package:extended_image/src/extended_image_utils.dart, package:extended_image/src/extended_image.dart, package:extended_image/src/editor/extended_image_editor.dart, package:extended_image_library/extended_image_library.dart, package:extended_image_library/src/extended_network_image_utils.dart, package:extended_image_library/src/extended_network_image_provider.dart, package:extended_image_library/src/_network_image_io.dart, package:path_provider/path_provider.dart, package:path_provider_linux/path_provider_linux.dart] that declares support for platforms: linux

Package does not support Flutter platform linux

Because of import path [package:native_pdf_view/native_pdf_view.dart, package:native_pdf_view/src/native_pdf_view.dart, package:native_pdf_view/src/has_support/io.dart, package:device_info/device_info.dart] that declares support for platforms: android, ios

Package does not support Flutter platform macos

Because of import path [package:native_pdf_view/native_pdf_view.dart, package:native_pdf_view/src/native_pdf_view.dart, package:native_pdf_view/src/has_support/io.dart, package:device_info/device_info.dart] that declares support for platforms: android, ios

Package does not support Flutter platform web

Because of import path [package:native_pdf_view/native_pdf_view.dart, package:native_pdf_view/src/native_pdf_view.dart, package:extended_image/extended_image.dart, package:extended_image/src/image/extended_render_image.dart, package:extended_image/src/extended_image_typedef.dart, package:extended_image/src/gesture/extended_image_slide_page.dart, package:extended_image/src/gesture/extended_image_slide_page_handler.dart, package:extended_image/src/gesture/extended_image_gesture_utils.dart, package:extended_image/src/extended_image_utils.dart, package:extended_image/src/extended_image.dart, package:extended_image/src/editor/extended_image_editor.dart, package:extended_image_library/extended_image_library.dart, package:extended_image_library/src/extended_network_image_utils.dart, package:path_provider/path_provider.dart] that declares support for platforms: android, ios, linux, macos

Package does not support Flutter platform windows

Because of import path [package:native_pdf_view/native_pdf_view.dart, package:native_pdf_view/src/native_pdf_view.dart, package:native_pdf_view/src/has_support/io.dart, package:device_info/device_info.dart] that declares support for platforms: android, ios

Package not compatible with SDK dart

because of import path [native_pdf_view] that is in a package requiring null.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
device_info ^0.4.2+4 0.4.2+4
extended_image ^0.9.0 0.9.0
flutter 0.0.0
native_pdf_renderer ^2.3.2 2.3.2
synchronized ^2.2.0 2.2.0+1
Transitive dependencies
archive 2.0.13
args 1.6.0
charcode 1.1.3
collection 1.14.12 1.14.13
convert 2.1.1
crypto 2.1.5
extended_image_library 0.2.3
extension 0.1.1
file 5.2.1
flutter_web_plugins 0.0.0
http 0.12.1
http_client_helper 0.2.1
http_parser 3.1.4
image 2.1.13
intl 0.16.1
js 0.6.2
meta 1.1.8
path 1.7.0
path_provider 1.6.11
path_provider_linux 0.0.1+2
path_provider_macos 0.0.4+3
path_provider_platform_interface 1.0.2
petitparser 3.0.4
platform 2.2.1
plugin_platform_interface 1.0.2
process 3.0.13
sky_engine 0.0.99
source_span 1.7.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6 1.2.0
uuid 2.2.0
vector_math 2.0.8
xdg_directories 0.1.0
xml 3.7.0 4.2.0
Dev dependencies
flutter_test
pedantic ^1.9.0 1.9.0 1.9.1