Articles in this section
Category / Section

How to save PDF Viewer document loaded in Flutter to local storage?

4 mins read

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 used the getTemporaryDirectory method in the pathprovider package to save the retrieved document to the 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();
      },
    ),
  );
}

 

Save PDF document in device-SfPdfViewer-Flutter.

 

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 know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our Flatter 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 check out 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 forumsDirect-Trac, or feedback portal. We are always happy to assist you!

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied