【问题标题】:How to open and PDF or word document in the [Flutter][Flutter]中如何打开PDF或word文档
【发布时间】:2018-08-20 09:31:11
【问题描述】:

问题很简单,我想使用 Flutter 通过默认 App 打开任何 pdf 或 doc 文件。

想一个与我的 pdf 资产相关的凸起按钮,当用户按下它时,pdf 将通过 Acrobat 阅读器等打开。我不想为它使用任何 web_view。

Flutter 支持吗?

【问题讨论】:

    标签: android ios mobile flutter


    【解决方案1】:

    一个好的和简单的方法是open_file 包,它可以让你用给定的路径打开一个文件。它支持多种不同的文件类型:

    import 'package:open_file/open_file.dart';
    
    OpenFile.open("/sdcard/example.pdf");
    

    【讨论】:

    • @s.j 有什么错误吗?你导入对了吗?
    • 是的,我正确导入了它,现在在我传递的设备中使用静态文件地址,它查看文件时使用动态字符串变量传递的文件,如定义的文件路径变量不起作用。
    • 类 EmployeeViewModel { EmployeeModel _employeeModel = EmployeeModel();字符串 fname;
    【解决方案2】:

    您可以通过在网络浏览器中打开谷歌文档来做到这一点:

    在 pubspec.yaml 中你需要:

    url_launcher: ^0.4.2+5
    

    包括:

    import 'package:flutter/src/gestures/tap.dart';
    import 'package:url_launcher/url_launcher.dart';
    

    代码 sn-p:

    new RichText(
      text: new LinkTextSpan(
          url: 'http://docs.google.com/viewer?url=http://www.pdf995.com/samples/pdf.pdf',
          text: 'Show My Pdf'),
    ),
    

    LinkTextSpan 类:

    class LinkTextSpan extends TextSpan {
      LinkTextSpan({TextStyle style, String url, String text})
          : super(
          style: style,
          text: text ?? url,
          recognizer: new TapGestureRecognizer()
            ..onTap = () {
              launch(url);
            });
    }
    

    【讨论】:

    • 这不适用于 FirebaseStorage 网址。我将读取权限设置为 true 并写入 auth != null。有什么想法吗?
    • 如果您没有通过 firebase auth 对您的用户进行身份验证(即 auth=null),那么您将无法使用这些权限读取您的数据库。
    • @HemantKaushik 你有没有在flutter webview中加载firebase存储URL的解决方案?
    【解决方案3】:

    你可以使用[flutter_full_pdf_viewer 1.0.6]依赖

    【讨论】:

      【解决方案4】:

      无需下载文件,使用包从 URL 打开 :url_launcher

      import 'package:url_launcher/url_launcher.dart';
      
      _launchURL() async {
        const url = 'https://flutter.dev/exapmle.pdf';
        if (await canLaunch(url)) {
          await launch(url);
        } else {
          throw 'Could not launch $url';
        }
      }
      

      【讨论】:

        【解决方案5】:

        您可以为此使用url_launcher 包。只需将文档的path 作为参数传递给launch() 方法即可。

        【讨论】:

          【解决方案6】:
          class EmployeeViewModel {
            EmployeeModel _employeeModel = EmployeeModel();
          
             String fname;   
                                                                                                                                        void downloadFile(BuildContext context, String fileUrl, String fileName, ProgressListener listener) async {
          
              String _filePath = '';
          
              if (Platform.isAndroid) {
                String _directory = await ExtStorage.getExternalStoragePublicDirectory(ExtStorage.DIRECTORY_DOWNLOADS);
                print(_directory);
                _filePath = '$_directory/$fileName';
                //todo getting file name here
                print("file name" + fileName);
                fname = fileName;
                print("file fname" + fname);
                //APIKey.FILE_NAME: fileName;
              } else if (Platform.isIOS) {
                Directory _directory = await getApplicationDocumentsDirectory();
                _filePath = '${_directory.path}/$fileName';
          
                print("file name" + fileName);
                //log(fileName);
                debugPrint(_directory.path);
                print("directory path" + _directory.path);
              }
          
              var response = await Dio().downloadUri(Uri().resolve(fileUrl), _filePath);
          
              if (response.statusCode == 200) {
                listener.isProcessing(false);
                AlertMessageDialog(context,  UtilString.downloadCompleted, UtilString.downloadCompletedMessage, UtilString.open, AlertMessageDialogActionHandler());
              } else {
                listener.isProcessing(false);
                UtilAction.showSnackBar(context, response.statusMessage);
              }
            }                                                                                                  class AlertMessageDialogActionHandler implements AlertMessageDialogListener {
          
            @override
            Future<void> onPositiveButtonClick() async {
              String _filePath = '';
              String fileName;
              String _directory = await ExtStorage.getExternalStoragePublicDirectory(ExtStorage.DIRECTORY_DOWNLOADS);
              //todo geeting right directory path here
              print("directory" + _directory);
              _filePath = '$_directory/$fileName';
              print("file path" + _filePath);
             // print("filename" + fileName);
          
              OpenFile.open("/storage/emulated/0/Download/GA55-Estimated-SHRIGOPAL-VERMA-2020-2021.pdf");  }}
          

          【讨论】:

          • 在此无法访问全局变量
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-12-11
          相关资源
          最近更新 更多