Tony
Tony

Reputation: 2748

Load pdf file from url

I have this url from server https://xxxx.pdf20200529". How can I load it as pdf in my flutter app?

I use this plugin, but nothing happened.

Code

OpenFile.open(value);  // value is https://xxxx.pdf20200529

Edit

 generatePDF(dynamic value) async {
    final filename = value.substring(value.lastIndexOf("/") + 1);
    var request = await HttpClient().getUrl(Uri.parse(value));
    var response = await request.close();
    var bytes = await consolidateHttpClientResponseBytes(response);
    String dir = (await getApplicationDocumentsDirectory()).path;
    File file = new File('$dir/$filename');
    await file.writeAsBytes(bytes);
    OpenFile.open(file.path);
  }

Upvotes: 1

Views: 4208

Answers (3)

Tony
Tony

Reputation: 2748

I miss .pdf in this line

File file = new File('$dir/$filename.pdf');

After add .pdf, all PDF viewer able to open.

Upvotes: 0

Benedikt J Schlegel
Benedikt J Schlegel

Reputation: 1939

Opening PDF's in Flutter is pretty fiddly. So I suggest you use a package. flutter_full_pdf_viewer works pretty well and is easy to use, but there are others out there.

https://pub.dev/packages/flutter_full_pdf_viewer

This will be your PDF Screen:

class PDFScreen extends StatelessWidget {
  String pathPDF = "";
  PDFScreen(this.pathPDF);

  @override
  Widget build(BuildContext context) {
    return PDFViewerScaffold(
        appBar: AppBar(
          title: Text("Document"),
          actions: <Widget>[
            IconButton(
              icon: Icon(Icons.share),
              onPressed: () {},
            ),
          ],
        ),
        path: pathPDF);
  }
}

Navigate to it like this. Make sure to pass the local path to the screen.:

Navigator.push(context, MaterialPageRoute(builder: (context) => PDFScreen(pathPDF)),

Upvotes: 1

Code Poet
Code Poet

Reputation: 8013

Have you tried url_launcher 5.4.10

As far as I know it opens pdfs as well.

Upvotes: 1

Related Questions