To show a page from a PDF URL in Flutter web, you can use the flutter_pdfview package. This package allows you to render PDF documents and navigate through pages.
Here's an example of how you can use the flutter_pdfview package to show a specific page from a PDF URL:
- Add the
flutter_pdfview dependency to your pubspec.yaml file:
dependencies:
flutter_pdfview: ^1.0.0
- Import the necessary libraries in your Dart file:
import 'package:flutter/material.dart';
import 'package:flutter_pdfview/flutter_pdfview.dart';
- Create a widget that shows the PDF view:
class PdfViewPage extends StatefulWidget {
final String pdfUrl;
final int pageNumber;
PdfViewPage({required this.pdfUrl, required this.pageNumber});
@override
_PdfViewPageState createState() => _PdfViewPageState();
}
class _PdfViewPageState extends State<PdfViewPage> {
late PDFViewController _pdfViewController;
int totalPages = 0;
int currentPage = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('PDF Viewer'),
),
body: Column(
children: [
Expanded(
child: PDFView(
filePath: widget.pdfUrl,
onPageChanged: (page, total) {
setState(() {
currentPage = page;
totalPages = total;
});
},
onViewCreated: (PDFViewController viewController) {
_pdfViewController = viewController;
_pdfViewController.setPage(widget.pageNumber);
},
),
),
Padding(
padding: EdgeInsets.all(8.0),
child: Text('Page $currentPage/$totalPages'),
),
Row(
children: [
ElevatedButton(
onPressed: () {
if (currentPage > 0) {
_pdfViewController.setPage(currentPage - 1);
}
},
child: Icon(Icons.navigate_before),
),
ElevatedButton(
onPressed: () {
if (currentPage < totalPages - 1) {
_pdfViewController.setPage(currentPage + 1);
}
},
child: Icon(Icons.navigate_next),
),
],
),
],
),
);
}
}
- Use the
PdfViewPage widget in your app and provide the PDF URL and the desired page number:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: PdfViewPage(
pdfUrl: 'https://example.com/path/to/file.pdf',
pageNumber: 2, // Show the 2nd page of the PDF
),
);
}
}
Make sure to replace 'https://example.com/path/to/file.pdf' with the actual URL of your PDF file. Also, adjust the page number to the page you want to show.
Now, when you run your Flutter web app, it will display the specified page from the PDF file at the provided URL.