如何在[Flutter]中打开PDF或Word文档

33

问题很简单,我想使用Flutter打开任何pdf或doc文件的默认应用程序。

考虑一个与我的pdf资源相关联的Raised button,当用户按下它时,pdf将通过Acrobat Reader或其他应用程序打开。我不想使用任何web_view。

Flutter支持吗?

6个回答

31

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

2022年7月更新:

由于open_file包中的一些权限在商店上传时不再被允许,因此有一个该包的分支(open_filex)已经移除了这个权限。使用方式相同:

import 'package:open_filex/open_filex.dart';

OpenFile.open("/sdcard/example.pdf");

1
@s.j 是否出现了某种错误?你导入的对吗? - Bostrot
请勿使用open_file包。在此处提到了两个替代方案 https://github.com/crazecoder/open_file/issues/174 - Hasan El-Hefnawy
我遇到了这个错误 MissingPluginException(No implementation found for method open_file on channel open_file),我正在使用 open_filex - Debabrata Samal
@DebabrataSamal 在添加后,你是否已经完全重建了你的应用程序? - Bostrot
在重新构建时出现了这个错误 - Debabrata Samal
显示剩余4条评论

17

您可以通过在Web浏览器中打开Google文档来完成此操作:

在pubspec.yaml中需要:

url_launcher: ^0.4.2+5

包括:

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

代码片段:

new RichText(
  text: new LinkTextSpan(
      url: 'http://docs.google.com/viewer?url=http://www.pdf995.com/samples/pdf.pdf',
      text: 'Show My Pdf'),
),
类:
class LinkTextSpan extends TextSpan {
  LinkTextSpan({TextStyle style, String url, String text})
      : super(
      style: style,
      text: text ?? url,
      recognizer: new TapGestureRecognizer()
        ..onTap = () {
          launch(url);
        });
}

6
这个无法使用FirebaseStorage的url。我将读取权限设置为true,并将写入权限设置为auth != null。有什么想法吗? - Hemant Kaushik
1
如果您没有通过Firebase身份验证(即auth=null)对用户进行身份验证,则无法使用这些权限读取数据库。 - Ashutosh Singh
@HemantKaushik,你有没有找到在Flutter WebView中加载Firebase存储URL的解决方案? - saigopi.me

6
不下载文件,使用包通过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';
  }
}

4
你可以使用 [ flutter_full_pdf_viewer 1.0.6 ] 依赖包来实现 PDF 文件的全屏查看。

4
你可以使用 url_launcher 包来实现此操作。只需将你的文档路径作为参数传递给 launch() 方法即可。

根据Readmeurl_launcher不能用于在Android中打开文件。 - Debabrata Samal

3
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");  }}

在这里无法访问全局变量。 - s.j

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接