Reputation: 351
Question is simple, I would like to open any pdf or doc file via default App with using Flutter.
Think a Raised button that related my pdf asset, when user press it, pdf will be opened via Acrobat reader or etc. I don't want to use any web_view for it.
is it supported in Flutter?
Upvotes: 35
Views: 90475
Reputation: 6033
A good and simple approach to this is the open_file package which lets you open a file with the given path. It supports numerous different file types.
UPDATE July 2022:
As the open_file package has some permissions in the manifest that are not allowed anymore for Store uploads there is a fork of this (open_filex) package that has this permission removed. The usage is the same:
import 'package:open_filex/open_filex.dart';
OpenFile.open("/sdcard/example.pdf");
Upvotes: 32
Reputation: 1483
without downloading file,open from URL using the package : 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';
}
}
Upvotes: 6
Reputation: 651
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"); }}
Upvotes: 3
Reputation: 4880
You can do this by opening google docs in a web browser :
In pubspec.yaml you need :
url_launcher: ^0.4.2+5
Includes :
import 'package:flutter/src/gestures/tap.dart';
import 'package:url_launcher/url_launcher.dart';
Code snippet :
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 :
class LinkTextSpan extends TextSpan {
LinkTextSpan({TextStyle style, String url, String text})
: super(
style: style,
text: text ?? url,
recognizer: new TapGestureRecognizer()
..onTap = () {
launch(url);
});
}
Upvotes: 18
Reputation: 5086
You can use url_launcher package for this. Just pass the path
of your document to the launch() method as the parameter.
Upvotes: 4