How to save PDF Viewer document loaded in Flutter to local storage?
This article explains how to download or save a loaded PDF document in the Flutter PDF Viewer to local storage.
Currently, the Syncfusion® Flutter PDF Viewer widget doesn’t allow editing or reviewing a PDF document, so the save support has not been provided yet. However, as a workaround solution, use the PdfDocument.saveSync method to obtain the loaded document bytes and save the document locally.
In the following code example, the loaded document bytes are retrieved from the document callback details and the getTemporaryDirectory method in the path_provider package is used to save the retrieved document to local storage when the download button is pressed.
final GlobalKey<SfPdfViewerState> _pdfViewerKey = GlobalKey(); List<int>? _documentBytes; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Syncfusion Flutter PDF Viewer'), actions: <Widget>[ IconButton( icon: const Icon( Icons.download, color: Colors.white, ), onPressed: () async { if (_documentBytes != null) { Directory directory = await getTemporaryDirectory(); String path = directory.path; // Create the empty file. File file = File('$path/sample.pdf'); // Write the PDF data retrieved from the SfPdfViewer. await file.writeAsBytes(_documentBytes!, flush: true); print(path); } }, ), ], ), body: SfPdfViewer.asset( 'assets/sample.pdf', key: _pdfViewerKey, onDocumentLoaded: (PdfDocumentLoadedDetails details) { // Document bytes of a PDF document loaded in SfPdfViewer. _documentBytes = details.document.saveSync(); }, ), ); }
A complete working example can be downloaded from here.
Conclusion
I hope you enjoyed learning how to save PDF Viewer documents loaded in Flutter to local storage.
You can refer to our Flutter PDF Viewer feature tour page to learn about its other groundbreaking features and documentation, and how to quickly get started with configuration specifications. You can also explore our Flutter PDF Viewer example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to explore our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums or feedback portal. We are always happy to assist you!