Flutter pdf viewer

Flutter PDF Viewer

Flutter PDF Viewer is a plugin that provides a PDF viewer widget for displaying PDF files in a Flutter application.

Installation

To use the Flutter PDF Viewer plugin, you need to add it as a dependency in your pubspec.yaml file:

dependencies:
  flutter_pdf_viewer: ^1.0.0
  

Usage

After adding the dependency, you can now import and use the PDF viewer widget in your Flutter application.

import 'package:flutter_pdf_viewer/flutter_pdf_viewer.dart';

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('PDF Viewer'),
        ),
        body: Center(
          child: RaisedButton(
            child: Text('Open PDF'),
            onPressed: () async {
              PDFViewer.loadAsset('assets/pdf/sample.pdf'); // Load a PDF file from asset
              // or
              PDFViewer.loadNetwork('https://example.com/sample.pdf'); // Load a PDF file from network
            },
          ),
        ),
      ),
    );
  }
}

In the above example, we are using the PDFViewer.loadAsset() method to load a PDF file from the assets folder, and the PDFViewer.loadNetwork() method to load a PDF file from a network URL.

Additional Configuration

The PDF viewer widget provides additional configuration options to customize the viewing experience. You can use the following methods to configure the PDF viewer:

  • .disableZooming: Disables zooming functionality in the PDF viewer.
  • .defaultPage: Sets the default page to be displayed when the PDF viewer is opened.
  • .userAgent: Sets the user agent string to be used when loading a PDF file from a network URL.
  • .nightMode: Enables the night mode in the PDF viewer.
  • .swipeHorizontal: Enables horizontal page swiping in the PDF viewer.
  • .onViewCreated: Callback function that is triggered when the PDF viewer is created.
  • .onDestroy: Callback function that is triggered when the PDF viewer is destroyed.
PDFViewer.loadAsset(
  'assets/pdf/sample.pdf',
  config: PDFViewerConfig(
    defaultPage: 1,
    disableZooming: false,
    userAgent: 'Custom User Agent',
    nightMode: false,
    swipeHorizontal: true,
    onViewCreated: (PDFViewController controller) {
      // Do something once PDF viewer is created
    },
    onDestroy: () {
      // Do something once PDF viewer is destroyed
    },
  ),
);

These are just a few examples of the available options. You can refer to the plugin’s documentation for a complete list of configuration options and their usage.

Leave a comment