pdf_image_renderer 0.2.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 75

pdf_image_renderer #

pub package

Renders PDFs to bitmaps using native renderers.

Usage #

See the example folder for a fully working flutter example.

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

class _MyAppState extends State<MyApp> {
  Uint8List image;

  void renderPdfImage() async {
    // Get a path from a pdf file (we are using the file_picker package (https://pub.dev/packages/file_picker))
    String path = await FilePicker.getFilePath(type: FileType.custom, allowedExtensions: ['pdf']);

    // Initialize the renderer
    final pdf = PdfImageRendererPdf(path: path);

    // open the pdf document
    await pdf.open()

    // open a page from the pdf document using the page index
    await pdf.openPage(pageIndex: 0);

    // get the render size after the page is loaded
    final size = await pdf.getPageSize(pageIndex: 0);

    // get the actual image of the page
    final img = await pdf.renderPage(
          pageIndex: pageIndex,
          x: 0,
          y: 0,
          width: size.width, // you can pass a custom size here to crop the image
          height: size.height, // you can pass a custom size here to crop the image
          scale: 1, // increase the scale for better quality (e.g. for zooming)
          background: '#ffffffff',
        );

    // use setState to update the renderer
    setState(() {
      image = img;
    });
  }

  @override
  void initState() {
    super.initState();
  }


  // you can use this image later in your build function
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
          appBar: AppBar(
            title: const Text('pdf_image_renderer'),
          ),
          body: Center(
            child: image != null ? Image(image: MemoryImage(image)) : Text("Loading..."),
          )
        )
    );
  }
}

0.2.0 #

  • BREAKING background color now requires a Color object
  • Make a lot more options optional and use default values instead.

0.1.5 #

  • Improve iOS pdf rendering performance.

0.1.4 #

  • Improve iOS pdf rendering performance.

0.1.3 #

  • Fixed pdf not rendering correctly on iOS with scale property larger than 1

0.1.2 #

  • Fixed pdf rotation ignored on iOS devices.

0.1.1+2

  • Finally found out what the problem for the android problem was -> see commits

0.1.1+1

  • Try to make android work

0.1.1 #

  • Improve the description to better describe the package.

0.1.0 #

  • Initial release. Can have some stability issues. Do not use in production!

example/lib/main.dart

import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'package:file_picker/file_picker.dart';
import 'package:pdf_image_renderer/pdf_image_renderer.dart';

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

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

class _MyAppState extends State<MyApp> {
  int pageIndex = 0;
  Uint8List image;

  bool open;

  PdfImageRendererPdf pdf;
  int count;
  PdfImageRendererPageSize size;

  bool cropped = false;

  @override
  void initState() {
    super.initState();
  }

  renderPage() async {
    size = await pdf.getPageSize(pageIndex: pageIndex);
    final i = await pdf.renderPage(
      pageIndex: pageIndex,
      x: cropped ? 100 : 0,
      y: cropped ? 100 : 0,
      width: cropped ? 100 : size.width,
      height: cropped ? 100 : size.height,
      scale: 3,
      background: Color(0xFFFFFFFF),
    );

    setState(() {
      image = i;
    });
  }

  openPdf({@required String path}) async {
    if (pdf != null) {
      await pdf.close();
    }
    pdf = PdfImageRendererPdf(path: path);
    await pdf.open();
    setState(() {
      open = true;
    });
  }

  closePdf() async {
    if (pdf != null) {
      await pdf.close();
      setState(() {
        open = false;
      });
    }
  }

  openPdfPage({@required int pageIndex}) async {
    await pdf.openPage(pageIndex: pageIndex);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
          actions: <Widget>[
            if (open == true)
              IconButton(
                icon: Icon(Icons.crop),
                onPressed: () {
                  cropped = !cropped;
                  renderPage();
                },
              )
          ],
        ),
        body: Center(
          child: SingleChildScrollView(
            child: Column(
              children: <Widget>[
                RaisedButton(
                  child: Text('Select PDF'),
                  onPressed: () async {
                    String path = await FilePicker.getFilePath(type: FileType.custom, allowedExtensions: ['pdf']);

                    if (path != null) {
                      await openPdf(path: path);
                      pageIndex = 0;
                      count = await pdf.getPageCount();
                      await openPdfPage(pageIndex: pageIndex);
                      renderPage();
                    }
                  },
                ),
                if (count != null) Text('The selected PDF has $count pages.'),
                if (image != null) Text('It is ${size.width} wide and ${size.height} high.'),
                if (open == true)
                  RaisedButton(
                    child: Text('Close PDF'),
                    onPressed: () async {
                      await closePdf();
                    },
                  ),
                if (image != null) ...[
                  Text('Rendered image area:'),
                  Image(image: MemoryImage(image)),
                ],
                if (open == true)
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      FlatButton.icon(
                        onPressed: pageIndex > 0
                            ? () async {
                                pageIndex -= 1;
                                await openPdfPage(pageIndex: pageIndex);
                                renderPage();
                              }
                            : null,
                        icon: Icon(Icons.chevron_left),
                        label: Text('Previous'),
                      ),
                      FlatButton.icon(
                        onPressed: pageIndex < (count - 1)
                            ? () async {
                                pageIndex += 1;
                                await openPdfPage(pageIndex: pageIndex);
                                renderPage();
                              }
                            : null,
                        icon: Icon(Icons.chevron_right),
                        label: Text('Next'),
                      ),
                    ],
                  ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  pdf_image_renderer: ^0.2.0

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:pdf_image_renderer/pdf_image_renderer.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]
100
Overall:
Weighted score of the above. [more]
75
Learn more about scoring.

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

  • Dart: 2.8.4
  • pana: 0.13.15
  • Flutter: 1.17.5

Analysis suggestions

Package does not support Flutter platform Linux

Because:

  • package:pdf_image_renderer/pdf_image_renderer.dart that declares support for platforms: Android, iOS

Package does not support Flutter platform Web

Because:

  • package:pdf_image_renderer/pdf_image_renderer.dart that declares support for platforms: Android, iOS

Package does not support Flutter platform Windows

Because:

  • package:pdf_image_renderer/pdf_image_renderer.dart that declares support for platforms: Android, iOS

Package does not support Flutter platform macOS

Because:

  • package:pdf_image_renderer/pdf_image_renderer.dart that declares support for platforms: Android, iOS

Package not compatible with SDK dart

Because:

  • pdf_image_renderer that is a package requiring null.

Health suggestions

Format lib/pdf_image_renderer.dart.

Run flutter format to format lib/pdf_image_renderer.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.12 1.14.13
meta 1.1.8 1.2.2
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
vector_math 2.0.8 2.1.0-nullsafety
Dev dependencies
flutter_test