native_pdf_renderer2 1.8.2

  • Readme
  • Changelog
  • Example
  • Installing
  • 69

PDF Renderer #

Flutter Plugin to render PDF pages as images on both Android 5.0+ and iOS 11.0+ devices.

For IOS need set swift version to 5 (instruction, issue)

Getting Started #

In your flutter project add the dependency:

pub package

dependencies:
  native_pdf_renderer2: any

For help getting started with Flutter, view the online documentation.

Usage example #

Read more

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:native_pdf_renderer2/native_pdf_renderer2.dart';

void main() async {
  try {
    final document = await PDFDocument.openAsset('assets/sample.pdf');
    final page = await document.getPage(1);
    final pageImage = await page.render(width: page.width, height: page.height);
    await page.close();
    runApp(
      MaterialApp(
        home: Scaffold(
          body: Center(
            child: Image(
              image: MemoryImage(pageImage.bytes),
            ),
          ),
        ),
        color: Colors.white,
      )
    );
  } on PlatformException catch (error) {
    print(error);
  }
}

Rendering PDF files on Android devices #

This plugin uses the Android native PdfRenderer to render the pages of PDF files and provides a widget called PdfRenderer to display the PDF page you like.

Rendering PDF files on IOS devices #

This plugin uses the IOS native PDFKit to render the pages of PDF files and provides a widget called PDFKit to display the PDF page you like.

1.8.1 #

1.8.0 #

  • Set minimal flutter version to 1.10
  • Update synchronized package
  • Fixed android build issue#34

1.7.0 #

  • Fixed PDF crop on Android (pull#25)
  • Added package synchronized for sequential access to the native api render to reduce the likelihood of a crash due to lack of memory. Additional information: issue#14 & issue#16

1.6.2 #

  • Target sdk version for android upped to 28

1.6.1 #

  • Added more tests
  • Fixed PDFPageImage.pageNumber always returns null
  • If page not in document throws PdfPageNotFoundException

1.6.0 #

  • Added more documentation for public properties and methods
  • crop property in render method marked as deprecated, usage cropRect instead
  • Added isClosed property for PDFDocument and PDFPage
  • Added tests

1.5.0 #

  • Added crop option for rendering (#11)
  • Fixed bug with render same page from issue #5

1.4.2 #

  • Fixed not correctly filling background Color on IOS

1.4.1 #

  • Resolve supports Flutter v1.7

1.4.0+1 #

  • Hotfix

1.4.0 #

  • Now format and backgroundColor options for image rendering works on IOS.

1.3.0+1 #

  • Hotfix

1.3.0 #

  • Added format and backgroundColor options for image rendering. Attention: it feature works only on Android platform!
  • Added dart linter
  • Refactoring existing code

1.2.3 #

  • Scale fix on IOS

1.2.2 #

  • Upgrade to Swift 5

1.2.1 #

  • Fix IOS build error

1.2.0 #

  • Optimized UI freezes on android platform

1.1.2 #

  • Update readme

1.1.1 #

  • Fix compilation error on pure android Java projects (#1)

1.1.0 #

  • Transferred sources to src directory
  • Removed dispose method from PDFDocument class (Replaced by close method)
  • Added more comments in code

1.0.1 #

  • Fixed support capability dart v2.0 (non 2.1) by add import dart:core in files with use Future

1.0.0 #

  • Initial release

example/lib/main.dart

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:native_pdf_renderer2/native_pdf_renderer2.dart';
import 'package:native_pdf_renderer2_example/has_support.dart';

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

class ExampleApp extends StatelessWidget {
  Future<PDFDocument> _getDocument() async {
    if (await hasSupport()) {
      return PDFDocument.openAsset('assets/sample.pdf');
    } else {
      throw Exception(
        'PDF Rendering does not '
        'support on the system of this version',
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
      statusBarColor: Colors.transparent,
      statusBarBrightness: Brightness.light,
      statusBarIconBrightness: Brightness.dark,
      systemNavigationBarColor: Colors.white,
      systemNavigationBarIconBrightness: Brightness.dark,
    ));

    final storage = Map<int, PDFPageImage>();

    return MaterialApp(
      title: 'PDF View example',
      color: Colors.white,
      home: Scaffold(
        body: FutureBuilder(
          future: _getDocument(),
          builder: (context, AsyncSnapshot<PDFDocument> snapshot) {
            if (!snapshot.hasData) {
              return Center(
                child: CircularProgressIndicator(),
              );
            }

            if (snapshot.hasError) {
              return Center(
                child: Text(snapshot.error.toString()),
              );
            }

            return PageView(
              children: <Widget>[
                ImageLoader(
                  storage: storage,
                  document: snapshot.data,
                  pageNumber: 1,
                ),
                ImageLoader(
                  storage: storage,
                  document: snapshot.data,
                  pageNumber: 2,
                ),
              ],
            );
          },
        ),
        bottomNavigationBar: BottomAppBar(
          child: Padding(
            padding: EdgeInsets.all(16),
            child: Row(
              mainAxisSize: MainAxisSize.max,
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text(
                  'Swipe to right',
                  style: Theme.of(context).textTheme.title,
                ),
                Icon(Icons.keyboard_arrow_right),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

class ImageLoader extends StatelessWidget {
  ImageLoader({
    @required this.storage,
    @required this.document,
    @required this.pageNumber,
    Key key,
  }) : super(key: key);

  final Map<int, PDFPageImage> storage;
  final PDFDocument document;
  final int pageNumber;

  @override
  Widget build(BuildContext context) => FutureBuilder(
        future: _renderPage(),
        builder: (context, AsyncSnapshot<PDFPageImage> snapshot) {
          if (snapshot.hasError) {
            return Center(
              child: Text('Error'),
            );
          }
          if (!snapshot.hasData) {
            return Center(
              child: CircularProgressIndicator(),
            );
          }

          return Image(
            image: MemoryImage(snapshot.data.bytes),
          );
        },
      );

  Future<PDFPageImage> _renderPage() async {
    if (storage.containsKey(pageNumber)) {
      return storage[pageNumber];
    }
    final page = await document.getPage(pageNumber);
    final pageImage = await page.render(
      width: page.width * 2,
      height: page.height * 2,
      format: PDFPageFormat.PNG,
    );
    await page.close();
    storage[pageNumber] = pageImage;
    return pageImage;
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  native_pdf_renderer2: ^1.8.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_renderer2/native_pdf_renderer2.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
49
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
70
Overall:
Weighted score of the above. [more]
69
Learn more about scoring.

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

  • Dart: 2.8.2
  • pana: 0.13.8-dev
  • Flutter: 1.17.1

Maintenance issues and suggestions

Homepage URL doesn't exist. (-20 points)

At the time of the analysis the homepage field https://github.com/rbcprolabs/packages.flutter/tree/master/packages/native_pdf_renderer2 was unreachable.

Support latest dependencies. (-10 points)

The version constraint in pubspec.yaml does not support the latest published versions for 1 dependency (extension).

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.6.0 <3.0.0
extension ^0.0.5 0.0.5 0.1.1
flutter 0.0.0
meta ^1.1.8 1.1.8
synchronized ^2.2.0 2.2.0
Transitive dependencies
collection 1.14.12
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test
pedantic ^1.8.0+1