flutter-pdf-text icon indicating copy to clipboard operation
flutter-pdf-text copied to clipboard

How to open a PDF document from Assets folder?

Open real1900 opened this issue 4 years ago • 4 comments

How do I open a PDF document from Assets folder?

real1900 avatar Sep 04 '20 08:09 real1900

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);
  }

clangenb avatar Sep 04 '20 10:09 clangenb

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

janbildsoehansen avatar Apr 05 '21 01:04 janbildsoehansen

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 avatar Aug 19 '22 22:08 BeTure20

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']; // ... }

BeTure20 avatar Aug 19 '22 23:08 BeTure20