Open PDF Files in Flutter

PSPDFKit for Flutter allows you to open documents using a global present() method and a Flutter widget. This article serves as a step-by-step guide to get you started quickly.

Opening a PDF

If you’re trying to open a PDF file that has been stored as an asset, you first need to copy that file to a temporary directory. Once that’s done, you can open it.

Here’s a helper function to copy the file to a temporary directory:

Future<File> extractAsset(String assetPath) async {
    final bytes = await DefaultAssetBundle.of(context).load(assetPath);
    final list = bytes.buffer.asUint8List();

    final tempDir = await Pspdfkit.getTemporaryDirectory();
    final tempDocumentPath = '${tempDir.path}/$assetPath';

    final file = await File(tempDocumentPath).create(recursive: true);
    return file;

Here’s how you can open a PDF document using the global present() method:

final document = await extractAsset(documentPath);
wait Pspdfkit.present(document.path);

And here’s how you can open a PDF document using the native component API:

final extractedDocument = await extractAsset(_documentPath);
if (Theme.of(context).platform == TargetPlatform.iOS) {
    await Navigator.of(context).push<dynamic>(CupertinoPageRoute<dynamic>(
        builder: (_) => CupertinoPageScaffold(
            navigationBar: CupertinoNavigationBar(),
            child: SafeArea(
                bottom: false,
                child: PspdfkitWidget(
                    documentPath: extractedDocument.path)))));
} else {
    await Navigator.of(context).push<dynamic>(MaterialPageRoute<dynamic>(
        builder: (_) => Scaffold(
            extendBodyBehindAppBar: true,
            appBar: AppBar(),
            body: SafeArea(
                top: false,
                bottom: false,
                child: Container(
                    padding: const EdgeInsets.only(top: kToolbarHeight),
                    child: PspdfkitWidget(
                        documentPath: extractedDocument.path))))));

For more details about how to open a document using PSPDFKit for Flutter, see our getting started guides or take our Catalog example project for a spin.

Opening an Image

In addition to PDF documents, PSPDFKit for Flutter also allows you to open and annotate images (PNG and JPEG). The following example shows how to open a PNG image using the present() method:

final extractedImage = await extractAsset(imagePath);
await Pspdfkit.present(extractedImage.path);

We use cookies to improve your experience and for marketing. Learn more in our privacy policy.