Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
real1900 opened this issue Sep 4, 2020 · 4 comments
Open

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

real1900 opened this issue Sep 4, 2020 · 4 comments

Comments

@real1900
Copy link

real1900 commented Sep 4, 2020

How do I open a PDF document from Assets folder?

@clangenb
Copy link

clangenb commented Sep 4, 2020

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
Copy link

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

@BeTure20
Copy link

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants