Created
July 23, 2024 07:50
-
-
Save ffkev/0989e09e55817ec3608b9e86a4bfa2e2 to your computer and use it in GitHub Desktop.
To save and open a PDF file that is received in a base64 format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ignore_for_file: use_build_context_synchronously | |
import 'dart:convert'; | |
import 'dart:io'; | |
import 'package:advance_pdf_viewer2/advance_pdf_viewer.dart'; | |
import 'package:flutter/foundation.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:path_provider/path_provider.dart'; | |
import 'package:pdf_viewer_project/pages/pdf_viewer_page.dart'; | |
void main() { | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter PDF Viewer', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: const HomePage(), | |
); | |
} | |
} | |
class HomePage extends StatefulWidget { | |
const HomePage({super.key}); | |
@override | |
State<HomePage> createState() => _HomePageState(); | |
} | |
class _HomePageState extends State<HomePage> { | |
final String base64String = | |
"JVBERi0xLjEKJcKlwrHDqwoKMSAwIG9iagogIDw8IC9UeXBlIC9DYXRhbG9nCiAgICAgL1BhZ2VzIDIgMCBSCiAgPj4KZW5kb2JqCgoyIDAgb2JqCiAgPDwgL1R5cGUgL1BhZ2VzCiAgICAgL0tpZHMgWzMgMCBSXQogICAgIC9Db3VudCAxCiAgICAgL01lZGlhQm94IFswIDAgMzAwIDE0NF0KICA+PgplbmRvYmoKCjMgMCBvYmoKICA8PCAgL1R5cGUgL1BhZ2UKICAgICAgL1BhcmVudCAyIDAgUgogICAgICAvUmVzb3VyY2VzCiAgICAgICA8PCAvRm9udAogICAgICAgICAgIDw8IC9GMQogICAgICAgICAgICAgICA8PCAvVHlwZSAvRm9udAogICAgICAgICAgICAgICAgICAvU3VidHlwZSAvVHlwZTEKICAgICAgICAgICAgICAgICAgL0Jhc2VGb250IC9UaW1lcy1Sb21hbgogICAgICAgICAgICAgICA+PgogICAgICAgICAgID4+CiAgICAgICA+PgogICAgICAvQ29udGVudHMgNCAwIFIKICA+PgplbmRvYmoKCjQgMCBvYmoKICA8PCAvTGVuZ3RoIDU1ID4+CnN0cmVhbQogIEJUCiAgICAvRjEgMTggVGYKICAgIDAgMCBUZAogICAgKEhlbGxvIFdvcmxkKSBUagogIEVUCmVuZHN0cmVhbQplbmRvYmoKCnhyZWYKMCA1CjAwMDAwMDAwMDAgNjU1MzUgZiAKMDAwMDAwMDAxOCAwMDAwMCBuIAowMDAwMDAwMDc3IDAwMDAwIG4gCjAwMDAwMDAxNzggMDAwMDAgbiAKMDAwMDAwMDQ1NyAwMDAwMCBuIAp0cmFpbGVyCiAgPDwgIC9Sb290IDEgMCBSCiAgICAgIC9TaXplIDUKICA+PgpzdGFydHhyZWYKNTY1CiUlRU9GCg=="; | |
Future<File> saveBase64ToFile(String base64String, String fileName) async { | |
try { | |
final directory = await getApplicationDocumentsDirectory(); | |
final filePath = '${directory.path}/$fileName'; | |
final bytes = base64Decode(base64String); | |
final file = File(filePath); | |
await file.writeAsBytes(bytes); | |
return file; | |
} catch (e) { | |
print("Error saving file: $e"); | |
rethrow; | |
} | |
} | |
Future<void> saveAndOpenPdf(String base64String, String fileName, BuildContext context) async { | |
try { | |
final file = await saveBase64ToFile(base64String, fileName); | |
Navigator.push( | |
context, | |
MaterialPageRoute( | |
builder: (context) => PdfViewerPage(filePath: file.path), | |
), | |
); | |
} catch (e) { | |
print("Error opening PDF: $e"); | |
rethrow; | |
} | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: const Text("Home Page"), | |
), | |
body: Center( | |
child: ElevatedButton( | |
onPressed: () async { | |
try { | |
await saveAndOpenPdf(base64String, "example.pdf", context); | |
} catch (e) { | |
if (kDebugMode) { | |
print("Error: $e"); | |
} | |
ScaffoldMessenger.of(context).showSnackBar( | |
SnackBar(content: Text("Error opening PDF: $e")), | |
); | |
} | |
}, | |
child: const Text("Open PDF"), | |
), | |
), | |
); | |
} | |
} | |
// 'JVBERi0xLjEKJcKlwrHDqwoKMSAwIG9iagogIDw8IC9UeXBlIC9DYXRhbG9nCiAgICAgL1BhZ2VzIDIgMCBSCiAgPj4KZW5kb2JqCgoyIDAgb2JqCiAgPDwgL1R5cGUgL1BhZ2VzCiAgICAgL0tpZHMgWzMgMCBSXQogICAgIC9Db3VudCAxCiAgICAgL01lZGlhQm94IFswIDAgMzAwIDE0NF0KICA+PgplbmRvYmoKCjMgMCBvYmoKICA8PCAgL1R5cGUgL1BhZ2UKICAgICAgL1BhcmVudCAyIDAgUgogICAgICAvUmVzb3VyY2VzCiAgICAgICA8PCAvRm9udAogICAgICAgICAgIDw8IC9GMQogICAgICAgICAgICAgICA8PCAvVHlwZSAvRm9udAogICAgICAgICAgICAgICAgICAvU3VidHlwZSAvVHlwZTEKICAgICAgICAgICAgICAgICAgL0Jhc2VGb250IC9UaW1lcy1Sb21hbgogICAgICAgICAgICAgICA+PgogICAgICAgICAgID4+CiAgICAgICA+PgogICAgICAvQ29udGVudHMgNCAwIFIKICA+PgplbmRvYmoKCjQgMCBvYmoKICA8PCAvTGVuZ3RoIDU1ID4+CnN0cmVhbQogIEJUCiAgICAvRjEgMTggVGYKICAgIDAgMCBUZAogICAgKEhlbGxvIFdvcmxkKSBUagogIEVUCmVuZHN0cmVhbQplbmRvYmoKCnhyZWYKMCA1CjAwMDAwMDAwMDAgNjU1MzUgZiAKMDAwMDAwMDAxOCAwMDAwMCBuIAowMDAwMDAwMDc3IDAwMDAwIG4gCjAwMDAwMDAxNzggMDAwMDAgbiAKMDAwMDAwMDQ1NyAwMDAwMCBuIAp0cmFpbGVyCiAgPDwgIC9Sb290IDEgMCBSCiAgICAgIC9TaXplIDUKICA+PgpzdGFydHhyZWYKNTY1CiUlRU9GCg==' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:io'; | |
import 'package:advance_pdf_viewer2/advance_pdf_viewer.dart'; | |
import 'package:flutter/material.dart'; | |
class PdfViewerPage extends StatefulWidget { | |
final String filePath; | |
PdfViewerPage({required this.filePath}); | |
@override | |
_PdfViewerPageState createState() => _PdfViewerPageState(); | |
} | |
class _PdfViewerPageState extends State<PdfViewerPage> { | |
bool _isLoading = true; | |
late PDFDocument document; | |
@override | |
void initState() { | |
super.initState(); | |
loadDocument(); | |
} | |
loadDocument() async { | |
try { | |
document = await PDFDocument.fromFile(File(widget.filePath)); | |
setState(() { | |
_isLoading = false; | |
}); | |
} catch (e) { | |
print("Error loading document: $e"); | |
ScaffoldMessenger.of(context).showSnackBar( | |
SnackBar(content: Text("Error loading PDF: $e")), | |
); | |
} | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: const Text("PDF Viewer"), | |
), | |
body: _isLoading ? const Center(child: CircularProgressIndicator()) : PDFViewer(document: document), | |
); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Add these as dependencies along with the other configurations that are present in the pubspec.yaml file | |
dependencies: | |
flutter: | |
sdk: flutter | |
advance_pdf_viewer2: ^2.0.1 | |
path_provider: ^2.0.8 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment