How can i get a PDF from local storage or upload the file and get the text in Flutter?

34 Views Asked by At

On the homepage of the Flutter app I have a button to upload a file pdf and read it and get the text

 SizedBox(
                    width: double.infinity,
                    child: ElevatedButton(
                      onPressed: () async {

                        final result = await FilePicker.platform.pickFiles(allowMultiple: false);

                        // if no file is picked
                        if (result == null) return;
                        print(result.files.first.path);
                        if(result.files.first.bytes != null){
                          print('+++++++++++++++++++++++++++++++++++++++++++++++');

                          getPDFtext(result.files.first.bytes).then((value) => {
                            print(value)
                          });
                        }
                      },
                      child: const Text('Get Text'),
                    ),
                  ),

Future<String> getPDFtext(Uint8List? uint8List) async {
    String text = "";

    try {
      PdfDocument document = PdfDocument(inputBytes: uint8List);
      PdfTextExtractor extractor = PdfTextExtractor(document);
      text = extractor.extractText();
    } on PlatformException {
      print('Failed to get PDF text.');
    }
    return text;
  }

LOG:

I/FilePickerUtils(31387): Caching from URI: content://com.android.providers.downloads.documents/document/raw%3A%2Fstorage%2Femulated%2F0%2FDownload%2Fexample.pdf
D/FilePickerUtils(31387): File loaded and cached at:/data/user/0/com.example.flutter_app/cache/file_picker/example.pdf
D/FilePickerDelegate(31387): File path:[com.mr.flutter.plugin.filepicker.FileInfo@c86228f]

But the text doesn't appear

I tried to load a file pdf or get an existing pdf in flutter and then i tried to get the text

0

There are 0 best solutions below