Cache PDF from network
Hi,
I'm using SfPdfViewer to access pdf files that are publicly available online. These files rarely change: does this package cache the file when it's accessed for the first time using the SfPdfViewer.network widget? If it doesn't, do you have a suggested method to implement that?
Thank you very much
Gianluca
SIGN IN To post a reply.
1 Reply
1 reply marked as answer
DB
Dilli Babu Nandha Gopal
Syncfusion Team
May 27, 2021 04:18 PM UTC
Hi Gianluca,
Greetings from Syncfusion.
SfPdfViewer doesn’t caches the PDF file. So, we recommend to save the PDF document in application directory once it is loaded in SfPdfViewer widget as mentioned in below steps.
1. With the help of path_provider package, the cache files can be created.
2. Subscribe onDocumentLoaded callback which returns PDFDocument instance.
3. PDF document bytes can be written to the created cache file.
But path_provider is supported only for mobile platforms, so this approach can’t be used for Web platform. The following code example illustrates how to cache a PDF document while loading document in SfPdfViewer widget.
|
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'package:syncfusion_flutter_pdfviewer/pdfviewer.dart';
void main() {
runApp(MaterialApp(
title: 'Syncfusion PDF Viewer Demo',
home: HomePage(),
));
}
/// Represents Homepage for Navigation
class HomePage extends StatefulWidget {
@override
_HomePage createState() => _HomePage();
}
class _HomePage extends State<HomePage> {
final GlobalKey<SfPdfViewerState> _pdfViewerKey = GlobalKey();
File? _tempFile;
@override
void initState() {
initializeFile();
super.initState();
}
// ignore: avoid_void_async
void initializeFile() async {
final Directory tempPath = await getApplicationDocumentsDirectory();
final File tempFile = File(tempPath.path + '/flutter_succinctly.pdf');
final bool checkFileExist = await tempFile.exists();
if (checkFileExist) {
_tempFile = tempFile;
}
}
@override
Widget build(BuildContext context) {
Widget child;
if (_tempFile == null) {
child = SfPdfViewer.network(
'https://cdn.syncfusion.com/content/PDFViewer/flutter-succinctly.pdf',
key: _pdfViewerKey,
onDocumentLoaded: (PdfDocumentLoadedDetails details) async {
final Directory tempPath = await getApplicationDocumentsDirectory();
_tempFile = await File(tempPath.path + '/flutter_succinctly.pdf')
.writeAsBytes(details.document.save());
});
} else {
child = SfPdfViewer.file(
_tempFile!,
key: _pdfViewerKey,
);
}
return Scaffold(
appBar: AppBar(
title: const Text('Syncfusion Flutter PDF Viewer'),
actions: <Widget>[
IconButton(
icon: const Icon(
Icons.bookmark,
color: Colors.white,
),
onPressed: () {
_pdfViewerKey.currentState?.openBookmarkView();
},
),
],
),
body: child,
);
}
} |
Please let us know if you have any doubts.
Regards,
Dilli babu.
Dilli babu.
Marked as answer
SIGN IN To post a reply.
- 1 Reply
- 2 Participants
- Marked answer
-
GI Gianluca
- May 26, 2021 11:02 AM UTC
- May 27, 2021 04:18 PM UTC