pdfrx is a rich and fast PDF viewer implementation built on the top of PDFium. The plugin supports Android, iOS, Windows, macOS, Linux, and Web.
A demo site using Flutter Web
- Android
- iOS
- Windows
- macOS
- Linux (even on Raspberry PI)
- Web (*using PDF.js)
The following fragment illustrates the easiest way to show a PDF file in assets:
import 'package:pdfrx/pdfrx.dart';
...
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Pdfrx example'),
),
body: PdfViewer.asset('assets/hello.pdf'),
),
);
}
}
Anyway, please follow the instructions below to install on your environment.
Add this to your package's pubspec.yaml
file and execute flutter pub get
:
dependencies:
pdfrx: ^1.0.97
Ensure your Windows installation enables Developer Mode.
The build process internally uses symbolic link and it requires Developer Mode to be enabled. Without this, you may encounter errors like this.
PdfViewer supports following functions to open PDF file on specific medium:
- PdfViewer.asset
- Open PDF of Flutter's asset
- PdfViewer.file
- Open PDF from file
- macOS: may be blocked by App Sandbox
- Open PDF from file
- PdfViewer.uri
- Open PDF from URI (
https://...
or relative path)- Flutter Web: may be blocked by CORS
- macOS: may be blocked by App Sandbox
- Open PDF from URI (
PdfViewer.asset(
'assets/test.pdf',
// Most easiest way to return some password
passwordProvider: () => 'password',
...
),
For more customization and considerations, see Deal with Password Protected PDF Files using PasswordProvider.
You can customize the behaviour and the viewer look and feel by configuring PdfViewerParams.
The following fragment enables text selection feature:
PdfViewer.asset(
'assets/test.pdf',
params: PdfViewerParams(
enableTextSelection: true,
...
),
...
),
For more text selection customization, see Text Selection.
- Page Layout (Horizontal Scroll View/Facing Pages)
- Showing Scroll Thumbs
- Dark/Night Mode Support
- Document Loading Indicator
- Viewer Customization using Widget Overlay
- Double‐tap to Zoom
- Adding Page Number on Page Bottom
- Per-page Customization using Widget Overlay
- Per-page Customization using Canvas
PdfPageView is just another PDF widget that shows only one page. It accepts PdfDocument and page number to show a page within the document.
PdfDocumentViewBuilder is used to safely manage PdfDocument inside widget tree and it accepts builder
parameter that creates child widgets.
The following fragment is a typical use of these widgets:
PdfDocumentViewBuilder.asset(
'asset/test.pdf',
builder: (context, document) => ListView.builder(
itemCount: document?.pages.length ?? 0,
itemBuilder: (context, index) {
return Container(
margin: const EdgeInsets.all(8),
height: 240,
child: Column(
children: [
SizedBox(
height: 220,
child: PdfPageView(
document: document,
pageNumber: index + 1,
alignment: Alignment.center,
),
),
Text(
'${index + 1}',
),
],
),
);
},
),
),
PdfDocumentViewBuilder can accept PdfDocumentRef from PdfViewer to safely share the same PdfDocument instance. For more information, see example/viewer/lib/thumbnails_view.dart.
- Easy to use Flutter widgets
- Easy to use PDF APIs
- PDFium bindings
- Not encouraged but you can import package:pdfrx/src/pdfium/pdfium_bindings.dart