If you don't want to use the url_launcher package and prefer to open the PDF within the Flutter app using the flutter_pdfview package, you can follow these steps:
Import the required package in your Flutter project's pubspec.yaml file:
dependencies:
flutter_pdfview: ^1.0.0
Execute flutter pub get to install the package.
Create a web viewer widget that opens the PDF document using the package:
Execute flutter pub get to install the packages.
Create a web viewer widget that opens the PDF document:
Make sure to replace 'your-pdf-url-here' with the actual URL of the PDF document you want to open. Adjust startPage and endPage according to your page range requirements.
When this widget is rendered, it will automatically launch a new tab or window with the provided PDF URL and the specified page range.
Note: The url_launcher library is used to launch the PDF URL in a new tab or window.
flutter_pdfview
import 'package:flutter/material.dart';
import 'package:flutter_pdfview/flutter_pdfview.dart';
class PDFWebViewer extends StatelessWidget {
final String pdfUrl;
final int startPage;
final int endPage;
PDFWebViewer({required this.pdfUrl, required this.startPage, required this.endPage});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('PDF Viewer'),
),
body: PDFView(
onViewCreated: (PDFViewController controller) {
controller.setPageFragments(startPage: startPage, endPage: endPage);
},
documentLoader: Center(child: CircularProgressIndicator()),
filePath: pdfUrl,
),
);
}
}
Use the PDFWebViewer widget in your widget tree and pass the necessary parameters:
Make sure to replace 'your-pdf-url-here' with the actual URL or file path of the PDF document you want to open. Adjust startPage and endPage according to your page range requirements.
When this widget is rendered, it will open the PDF within the Flutter app using the flutter_pdfview package. Please note that the flutter_pdfview package may have limitations or issues related to rendering certain PDF features, so you should test it thoroughly before using it in a production environment.