AlessioLuciani / flutter-pdf-text

A plugin for Flutter that allows you to read the text content of PDF documents and convert it into strings.
MIT License
18 stars 45 forks source link

PDFDoc.text is not re-entrant #19

Closed hicnar closed 3 years ago

hicnar commented 3 years ago

It is not safe to call the PDFDoc.text multiple times in succession, meaning to call it again before the previous call returns. I's a pretty common scenario when an app may be processing more than one pdf file at a time. If the program enters this function more than once before the previous call returns the app will crash. I have tested this on a real Android device only and it crashes every single time if multiple pdfs are processed. Not sure how it behaves on iOS though.

There seems to be a workaround and it could be outlined as follows:

class PDFReader{

static final _lock = Lock();

...

Future pdfToString(File aPdfFile) async { ... String result; PDFDoc doc = await PDFDoc.fromFile(aPdfFile); await _lock.synchronized(() async { result = await doc.text; }); ... return result; } ...

}

Where the Lock comes from https://pub.dev/packages/synchronized

hicnar commented 3 years ago

Fixed in the submitted PR.