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

How to open a PDF document from Assets folder? #15

Open real1900 opened 4 years ago

real1900 commented 4 years ago

How do I open a PDF document from Assets folder?

clangenb commented 4 years ago

Something like that should work:

// example asset: asset = 'assets/your_doc.pdf' 
  Future<PDFDoc> _pdfFromAsset(String asset) async {
    File file;
    try {
      var dir = await getApplicationDocumentsDirectory();
      file = File("${dir.path}/file.pdf");
      var data = await rootBundle.load(asset);
      var bytes = data.buffer.asUint8List();
      await file.writeAsBytes(bytes, flush: true);
    } catch (e) {
      throw Exception('Error parsing asset file!');
    }
    return await PDFDoc.fromFile(file);
  }
janbildsoehansen commented 3 years ago

how do you load a local pdf from a local json file

BeTure20 commented 2 years ago

static Future pdfFromAsset(String asset) async { File file; try { final filename = basename(asset); var dir = await getApplicationDocumentsDirectory();

  var data = await rootBundle.load(asset);
  var bytes = data.buffer.asUint8List();
  file = File('${dir.path}/$filename');
  await file.writeAsBytes(bytes, flush: true);
} catch (e) {
  throw Exception('Error parsing asset file!');
}
PDFDoc doc = await PDFDoc.fromFile(file);
String docText = await doc.text;
return docText;

} //after that you will load

final file = await pdfFromAsset(path);

BeTure20 commented 2 years ago

how do you load a local pdf from a local json file

{
"pdfdata":[ { "id":1, "name":"doc1", "file":"doc1.pdf" }, { "id":2, "name":"doc2", "file":"doc2.pdf" } ] } //then to load

Future readJson() async { final String response = await rootBundle.loadString('assets/sample.json'); final data = await json.decode(response); final pdfdata = data['pdfdata']; // ... }