Use Android Intent to open PDF in Flutter

1,071

You can use the open_file package to open PDF files in Android and iOS.

import 'package:open_file/open_file.dart';

OpenFile.open('/sdcard/sample.pdf');

If you want to render and show a PDF file within your app, you can use the native_pdf_view.

import 'package:native_pdf_view/native_pdf_view.dart';

final pdfController = PdfController(
  document: PdfDocument.openAsset('assets/sample.pdf'),
);

Widget pdfView() => PdfView(
  controller: pdfController,
);
Share:
1,071
Tiago Paza
Author by

Tiago Paza

Updated on December 29, 2022

Comments

  • Tiago Paza
    Tiago Paza over 1 year

    I'm using the package android_intent and never work in Android native, but searching in official documentation, I create this code based in Kotlin codes.

                String uriFile =
                    Uri.encodeFull("content://${contentFile.path}");
    
                if (Platform.isAndroid) {
                  final AndroidIntent intent = AndroidIntent(
                    action: "action_view",
                    data: uriFile,
                    type: "application/pdf",
                    // type: "multipart/",
                    flags: [
                      Flag.FLAG_GRANT_READ_URI_PERMISSION,
                      Flag.FLAG_GRANT_PERSISTABLE_URI_PERMISSION,
                      Flag.FLAG_ACTIVITY_NEW_TASK
                    ],
                  );
                  intent.launch();
    

    This example generate this log:

    V/IntentSender(25283): Sending intent Intent { act=android.intent.action.VIEW dat=content:///data/user/0/br.com.encaixaonline/app_flutter/encaixa-online/arquivos/boleto_facil_exemplo.pdf typ=application/pdf flg=0x10000041 (has extras) }
    

    It generates the alert and displays the applications that allow you to open the PDF, but it does not display the file in any of the applications.

  • Tiago Paza
    Tiago Paza about 3 years
    Before implementing with Intent I tested this and other plugins to share file but, some PDF files are bank slips and my client wants to open in banking applications and already read the barcode and no plugin allowed this
  • Allan Wolski
    Allan Wolski about 3 years
    The open_file package uses intent to open files on Android and can send PDF files to banking apps.