5

I want to read user given PDF file. Convert it into text. But it should work both on android and iOS.

Alihaydar Gubatov
  • 988
  • 1
  • 12
  • 27

2 Answers2

3

This is a complex topic and I can only give you hints.

There are no dart libraries for this. You can either implement it natively on iOS/Android and use platform channels to communicate with the native code, or use an online service for the conversion.

For example, you can use Ghostscript in Firebase Cloud Functions. Ghostscript is capable of pdf to text conversion.

  1. How to extract text with Ghostscript
  2. How to use Ghostscript in Firebase Cloud Functions
  3. How to use Firebase Cloud Functions in Flutter
boformer
  • 28,207
  • 10
  • 81
  • 66
2

Try "pdf_text" plugin. You can convert pdf to text using this plugin.

  /// 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;
    });
  }
prahack
  • 1,267
  • 1
  • 15
  • 19