pdf_text 0.2.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 82

PDF Text Plugin #

Pub Version GitHub issues GitHub forks GitHub stars GitHub license

This plugin for Flutter allows you to read the text content of PDF documents and convert it into strings. It works on iOS and Android. On iOS it uses Apple's PDFKit. On Android it uses Apache's PdfBox Android porting.

Demo Example App

Getting Started #

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

dependencies:
  pdf_text: ^0.2.1

Usage #

Import the package with:

import 'package:pdf_text/pdf_text.dart';

Create a PDF document instance using a File object:

PDFDoc doc = await PDFDoc.fromFile(file);

or using a path string:

PDFDoc doc = await PDFDoc.fromPath(path);

or using a URL string:

PDFDoc doc = await PDFDoc.fromURL(url);

Pass a password for encrypted PDF documents:

PDFDoc doc = await PDFDoc.fromFile(file, password: password);

Read the text of the entire document:

String docText = await doc.text;

Retrieve the number of pages of the document:

int numPages = doc.length;

Access a page of the document:

PDFPage page = doc.pageAt(pageNumber);

Read the text of a page of the document:

String pageText = await page.text;

Optionally, you can delete the file of a document when you no longer need it. This can be useful when you import a PDF document from outside the local file system (e.g using a URL), since it is automatically stored in the temporary directory of the app.

Delete the file of a single document:

doc.deleteFile();

or delete all the files of all the documents imported from outside the local file system:

PDFDoc.deleteAllExternalFiles();

Functioning #

This plugin applies lazy loading for the text contents of the pages. The text is cached page per page. When you request the text of a page for the first time, it is parsed and stored in memory, so that the second access will be faster. Anyway, the text of pages that are not requested is not loaded. This mechanism allows you not to waste time loading text that you will probably not use. When you request the text content of the entire document, only the pages that have not been loaded yet are then loaded.

Public Methods #

PDFDoc #

| Return | Description |

| PDFPage | pageAt(int pageNumber)
Gets the page of the document at the given page number. |

| static Future<PDFDoc> | fromFile(File file, {String password = ""})
Creates a PDFDoc object with a File instance. Optionally, takes a password for encrypted PDF documents.|

| static Future<PDFDoc> | fromPath(String path, {String password = ""})
Creates a PDFDoc object with a file path. Optionally, takes a password for encrypted PDF documents.|

| static Future<PDFDoc> | fromURL(String url, {String password = ""})
Creates a PDFDoc object with a url. Optionally, takes a password for encrypted PDF documents. It downloads the PDF file located in the given URL and saves it in the app's temporary directory. |

| void | deleteFile()
Deletes the file related to this PDFDoc. Throws an exception if the FileSystemEntity cannot be deleted. |

| static Future | deleteAllExternalFiles()
Deletes all the files of the documents that have been imported from outside the local file system (e.g. using fromURL). |

Objects #

class PDFDoc {
  int length; // Number of pages of the document
  List<PDFPage> pages; // Pages of the document
  Future<String> text; // Text of the document
}

class PDFPage {
  int number; // Number of the page in the document
  Future<String> text; // Text of the page
}

Contribute #

If you have any suggestions, improvements or issues, feel free to contribute to this project. You can either submit a new issue or propose a pull request.

0.2.1 #

  • The support for password-encrypted PDF documents has been added.

0.2.0 #

  • Some new methods have been added to PDFDoc:
    • fromURL to load a PDF document from an URL.
    • deleteFile to delete the file related to the document.
    • deleteAllExternalFiles to delete all the files of the document imported from outside the local file system.

0.1.3 #

  • Part of the internal logic has been simplified.

0.1.2 #

  • A demo of the example app has been added.

0.1.1 #

  • An issue that caused a crash on iOS, when retrieving the text of the last page of the entire document, has been fixed.

0.1.0 #

  • The plugin is ready to be used.

0.0.1 #

  • The first release is out! It provides support for iOS and Android.

example/lib/main.dart

import 'dart:io';
import 'dart:math';

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

import 'package:pdf_text/pdf_text.dart';

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

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

class _MyAppState extends State<MyApp> {

  PDFDoc _pdfDoc;
  String _text = "";

  bool _buttonsEnabled = true;

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



  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('PDF Text Example'),
        ),
        body: Container(
          alignment: Alignment.center,
          padding: EdgeInsets.all(10),
          child: ListView(
            children: <Widget>[
              FlatButton(
                child: Text("Pick PDF document",
                  style: TextStyle(color: Colors.white),),
                color: Colors.blueAccent,
                onPressed: _pickPDFText,
                padding: EdgeInsets.all(5),
              ),
              FlatButton(
                child: Text("Read random page",
                  style: TextStyle(color: Colors.white),),
                color: Colors.blueAccent,
                onPressed: _buttonsEnabled ? _readRandomPage : () {},
                padding: EdgeInsets.all(5),
              ),
              FlatButton(
                child: Text("Read whole document",
                  style: TextStyle(color: Colors.white),),
                color: Colors.blueAccent,
                onPressed: _buttonsEnabled ? _readWholeDoc : () {},
                padding: EdgeInsets.all(5),
              ),

              Padding(
                child: Text(_pdfDoc == null ? "Pick a new PDF document and wait for it to load..."
                    : "PDF document loaded, ${_pdfDoc.length} pages\n",
                  style: TextStyle(fontSize: 18),
                  textAlign: TextAlign.center,),
                padding: EdgeInsets.all(15),
              ),
              Padding(
                child: Text(_text == "" ? "" : "Text:",
                  style: TextStyle(fontSize: 18),
                  textAlign: TextAlign.center,),
                padding: EdgeInsets.all(15),
              ),
              Text(_text),


            ],
          ),
        )

      ),
    );
  }

  /// Picks a new PDF document from the device
  Future _pickPDFText() async {
    File file = await FilePicker.getFile();
    _pdfDoc = await PDFDoc.fromFile(file);
    setState(() {});
  }

  /// Reads a random page of the document
  Future _readRandomPage() async {
    if (_pdfDoc == null) {
      return;
    }
    setState(() {
      _buttonsEnabled = false;
    });

    String text = await _pdfDoc.pageAt(Random().nextInt(_pdfDoc.length)+1).text;

    setState(() {
      _text = text;
      _buttonsEnabled = true;
    });
  }

  /// Reads the whole document
  Future _readWholeDoc() async {
    if (_pdfDoc == null) {
      return;
    }
    setState(() {
      _buttonsEnabled = false;
    });

    String text = await _pdfDoc.text;

    setState(() {
      _text = text;
      _buttonsEnabled = true;
    });
  }

}

Use this package as a library

1. Depend on it

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


dependencies:
  pdf_text: ^0.2.1

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_text/pdf_text.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
63
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]
82
Learn more about scoring.

We analyzed this package on May 30, 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

Health suggestions

Format lib/pdf_text.dart.

Run flutter format to format lib/pdf_text.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
http ^0.12.1 0.12.1
path_provider ^1.6.7 1.6.9
Transitive dependencies
charcode 1.1.3
collection 1.14.12
http_parser 3.1.4
meta 1.1.8
path 1.7.0
path_provider_macos 0.0.4+3
path_provider_platform_interface 1.0.2
pedantic 1.9.0
platform 2.2.1
plugin_platform_interface 1.0.2
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
vector_math 2.0.8
Dev dependencies
flutter_test