Skip to content

A Flutter plugin for IOS and Android providing a simple way to display PDFs.

License

Notifications You must be signed in to change notification settings

leonardopaim/Pdf_Viewer_Plugin

 
 

Repository files navigation

Pdf Viewer Plugin

pub package

A Flutter plugin for IOS and Android providing a simple way to display PDFs.

Features:

  • Display PDF.

android ........... ios

Installation

First, add pdf_viewer_plugin as a dependency in your pubspec.yaml file.

iOS

Add one row to the ios/Runner/info.plist:

...

<key>io.flutter.embedded_views_preview</key>
<true/>

You need to

Example

Here is an example flutter app.

import 'dart:async';
import 'dart:io';
import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'package:pdf_viewer_plugin/pdf_viewer_plugin.dart';
import 'package:path_provider/path_provider.dart';
import 'package:http/http.dart' as http;

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String path;

  @override
  initState() {
    super.initState();
  }

  Future<String> get _localPath async {
    final directory = await getApplicationDocumentsDirectory();

    return directory.path;
  }

  Future<File> get _localFile async {
    final path = await _localPath;
    return File('$path/teste.pdf');
  }

  Future<File> writeCounter(Uint8List stream) async {
    final file = await _localFile;

    // Write the file
    return file.writeAsBytes(stream);
  }

  Future<Uint8List> fetchPost() async {
    final response = await http.get(
        'https://expoforest.com.br/wp-content/uploads/2017/05/exemplo.pdf');
    final responseJson = response.bodyBytes;

    return responseJson;
  }

  loadPdf() async {
    writeCounter(await fetchPost());
    path = (await _localFile).path;

    if (!mounted) return;

    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Plugin example app'),
        ),
        body: Center(
          child: Column(
            children: <Widget>[
              if (path != null)
                Container(
                  height: 300.0,
                  child: PdfViewer(
                    filePath: path,
                  ),
                )
              else
                Text("Pdf is not Loaded"),
              RaisedButton(
                child: Text("Load pdf"),
                onPressed: loadPdf,
              ),
            ],
          ),
        ),
      ),
    );
  }
}

Feedback welcome and Pull Requests are most welcome!

About

A Flutter plugin for IOS and Android providing a simple way to display PDFs.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Dart 29.2%
  • Objective-C 28.2%
  • Java 21.3%
  • Ruby 18.3%
  • Shell 3.0%