pdf_render 0.15.0

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 82

Introduction

pdf_render is a PDF renderer implementation. Currently for iOS only.

The implementation is based on iOS's Core Graphics but I also consider adoption of pdfium at least for supporting Android.

Usage

The following fragment illustrate overall usage:

import 'package:pdf_render/pdf_render.dart';

...

/// Open the document using either openFile, openAsset, or openData.
PdfDocument doc = await PdfDocument.openAsset('assets/hello.pdf');

// Get the number of pages in the PDF file
int pageCount = doc.pageCount;

// The first page is 1
PdfPage page = await doc.getPage(1);

// For the render function's return, see explanation below.
PdfPageImage pageImage = await page.render();

// PDFDocument must be disposed as soon as possible.
doc.dispose();

And, then, you can use PdfPageImage to get the actual RGBA image in dart.ui.Image.

To embed the image in the widget tree, you can use RawImage:

@override
Center(
  child: Container(
    padding: EdgeInsets.all(10.0),
    color: Colors.grey,
    child: Center(
      child: RawImage(image: pageImage.image, fit: BoxFit.contain))
  )
)

PdfDocument.openXXX

On PdfDocument class, there're three functions for opening PDF from a real file, or a asset file, or memory data.

// from an asset file
PdfDocument docFromFile = PdfDocument.openFile('assets/hello.pdf');

// from a file
PdfDocument docFromAsset = PdfDocument.openAsset('/somewhere/in/real/file/system/file.pdf');

// from PDF memory image on Uint8List
PdfDocument docFromData = PdfDocument.openData(data);

PdfDocument members

class PdfDocument {
  final int docId; // For internal purpose
  final int pageCount; // Number of pages in the document
  final int verMajor; // PDF major version
  final int verMinor; // PDF minor version
  final bool isEncrypted; // Whether the file is encrypted or not
  final bool allowsCopying; // Whether the file allows you to copy the texts
  final bool allowsPrinting; // Whether the file allows you to print the document

  // Get a page by page number (page number starts at 1)
  Future<PdfPage> getPage(int pageNumber);

  // Dispose the instance.
  void dispose();
}

PdfPage members

class PdfPage {
  final int docId; // For internal purpose
  final int pageNumber; // Page number (page number starts at 1)
  final int rotationAngle; // Rotation angle; one of 0, 90, 180, 270
  final double width; // Page width in points; pixel size on 72-dpi
  final double height; // Page height in points; pixel size on 72-dpi

  // render sub-region of the PDF page.
  Future<PdfPageImage> render({
    int x = 0, int y = 0,
    int width = 0, int height = 0,
    double fullWidth = 0.0, double fullHeight = 0.0 });

For render function extract a sub-region (x,y) - (x + width, y + height) of scaled (fullWidth x fullHeight) PDF page image. All the coordinates are in pixels.

The following fragment renders the page at 300 dpi:

const scale = 300.0 / 72.0;
const fullWidth = page.width * scale;
const fullHeight = page.height * scale;
var rendered = page.render(
  x: 0,
  y: 0,
  width: fullWidth.toInt(),
  height: fullHeight.toInt(),
  fullWidth: fullWidth,
  fullHeight: fullHeight);

PdfPageImage members

/// Page number. The first page is 1.
final int pageNumber;
/// Left X coordinate of the rendered area in pixels.
final int x;
/// Top Y coordinate of the rendered area in pixels.
final int y;
/// Width of the rendered area in pixels.
final int width;
/// Height of the rendered area in pixels.
final int height;
/// Full width of the rendered page image in pixels.
final int fullWidth;
/// Full height of the rendered page image in pixels.
final int fullHeight;
/// PDF page width in points (width in pixels at 72 dpi).
final double pageWidth;
/// PDF page height in points (height in pixels at 72 dpi).
final double pageHeight;
/// Rendered image in dart:ui.Image
final Image image;

0.0.1

  • TODO: Describe initial release.

example/lib/main.dart

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

import 'package:pdf_render/pdf_render.dart';

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

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

class _MyAppState extends State<MyApp> {
  PdfPageImage _pageImage;

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

  Future<void> initPlatformState() async {
    //
    // Loading PDF file
    //
    var doc = await PdfDocument.openAsset('assets/hello.pdf');
    var page = await doc.getPage(1); // The first page is 1
    // render at 100 dpi
    const scale = 100.0 / 72.0;
    var w = (page.width * scale).toInt();
    var h = (page.height * scale).toInt();
    var pageImage = await page.render(width: w, height: h);
    // PDFDocument must be disposed as soon as possible.
    doc.dispose();
    if (!mounted) return;
    setState(() {
      _pageImage = pageImage;
    });
  }

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new Scaffold(
        appBar: new AppBar(
          title: const Text('Pdf_render example app'),
        ),
        body: new Center(
          child: Container(
            padding: EdgeInsets.all(10.0),
            color: Colors.grey,
            child: Center(
              child:
                _pageImage?.image != null ?
                // _pageImage.image is dart:ui.Image and you should wrap it with RawImage
                // to embed it on widget tree.
                RawImage(image: _pageImage.image, fit: BoxFit.contain) : Container()
              )
          )
        ),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  pdf_render: ^0.15.0

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter packages get

Alternatively, your editor might support 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:pdf_render/pdf_render.dart';
  
Version Uploaded Documentation Archive
0.15.0 Sep 11, 2018 Go to the documentation of pdf_render 0.15.0 Download pdf_render 0.15.0 archive
0.14.0 Sep 11, 2018 Go to the documentation of pdf_render 0.14.0 Download pdf_render 0.14.0 archive
0.13.0 Sep 11, 2018 Go to the documentation of pdf_render 0.13.0 Download pdf_render 0.13.0 archive
0.11.0 Sep 11, 2018 Go to the documentation of pdf_render 0.11.0 Download pdf_render 0.11.0 archive
0.10.0 Sep 9, 2018 Go to the documentation of pdf_render 0.10.0 Download pdf_render 0.10.0 archive
0.9.0 Sep 9, 2018 Go to the documentation of pdf_render 0.9.0 Download pdf_render 0.9.0 archive
0.8.0 Sep 8, 2018 Go to the documentation of pdf_render 0.8.0 Download pdf_render 0.8.0 archive
0.0.1 Sep 8, 2018 Go to the documentation of pdf_render 0.0.1 Download pdf_render 0.0.1 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
72
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
80
Overall:
Weighted score of the above. [more]
82
Learn more about scoring.

We analyzed this package on Oct 26, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.1.0-dev.7.1.flutter-b99bcfd309
  • pana: 0.12.5
  • Flutter: 0.10.1

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health suggestions

Format lib/pdf_render.dart.

Run flutter format to format lib/pdf_render.dart.

Maintenance suggestions

The description is too short. (-20 points)

Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11
meta 1.1.6
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8