Skip to content

Commit

Permalink
Merge pull request #47 from ia-toki/issue-40-print
Browse files Browse the repository at this point in the history
Add print feature for pdf view, remove unused code
  • Loading branch information
atnanahidiw authored Oct 29, 2023
2 parents 515d9dc + b00106e commit 4a64af9
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class _MaterialMenuState extends State<MaterialMenu> {
width: 130,
decoration: BoxDecoration(
border: Border.all(),
color: (filterIndex == index) ? Colors.black54 : Colors.white,
color: (filterIndex == index) ? Colors.black87 : Colors.white,
boxShadow: [
BoxShadow(
color: (filterIndex == index) ? Colors.white : Colors.black,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_pdfview/flutter_pdfview.dart';
import 'package:path_provider/path_provider.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:printing/printing.dart';


part 'pdf_viewer_page.dart';
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,34 @@ class PdfViewerPage extends StatefulWidget {
final String? description;

const PdfViewerPage({
Key? key,
required this.pdfUrl,
super.key ,
this.id,
this.title,
this.description,
}) : super(key: key);
});

@override
State<PdfViewerPage> createState() => _PdfViewerPageState();
}

class _PdfViewerPageState extends State<PdfViewerPage> {
String basePath =
"/storage/emulated/0/Android/data/com.toki.bebras_pandai/files/PDF_Download/";
String localPathPdf = "";
String remotePathPdf = "";
'/storage/emulated/0/Android/data/com.toki.bebras_pandai/files/PDF_Download/';
String localPathPdf = '';
String remotePathPdf = '';

@override
void initState() {
super.initState();
if (File(basePath + widget.id.toString() + ".pdf").existsSync()) {
if (File('$basePath${widget.id}.pdf').existsSync()) {
setState(() {
localPathPdf = basePath + widget.id.toString() + ".pdf";
localPathPdf = '$basePath${widget.id}.pdf';
});
}
WidgetsBinding.instance.addPostFrameCallback((_) {
fetchUrlPdfFile(widget.pdfUrl.toString());
});
// WidgetsBinding.instance.addPostFrameCallback((_){
// saveFile(widget.pdfUrl.toString(), "${widget.id}.pdf");
// });
}

@override
Expand All @@ -47,63 +44,63 @@ class _PdfViewerPageState extends State<PdfViewerPage> {
backgroundColor: Colors.black54,
title: Text(
widget.title.toString(), // name
style: TextStyle(color: Colors.white),
style: const TextStyle(color: Colors.white),
),
actions: [
IconButton(
onPressed: () async {
try {
await saveFile(remotePathPdf, "${widget.id}.pdf");
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text(
'successfully saved to internal storage',
style: TextStyle(color: Colors.white),
),
),
);
} catch (e) {
print(e);
}
await _generatePdf();
},
icon: const Icon(Icons.download_rounded),
icon: const Icon(Icons.print_rounded),
),
],
),
body: Stack(
children: [
localPathPdf == ''
? LinearProgressIndicator()
? const LinearProgressIndicator()
: PDFView(
filePath: localPathPdf,
onError: (error) {
print(error.toString());
// Do Nothing
},
onPageError: (page, error) {
print('$page: ${error.toString()}');
// Do Nothing
},
),
],
),
);
}

// Pdf Print
Future<void> _generatePdf() async {
try {
final file = File('$basePath${widget.id}.pdf');
final fileInByte = file.readAsBytesSync();
await Printing.layoutPdf(onLayout: (_) => fileInByte);
} catch (e) {
// Do Nothing
}
}

Future<bool> saveFile(String url, String fileName) async {
try {
Directory? directory;
directory = await getExternalStorageDirectory();
String newPath = "";
newPath = directory!.path + "/PDF_Download";
var newPath = '';
newPath = '${directory!.path}/PDF_Download';
directory = Directory(newPath);

File saveFile = File(directory.path + "/$fileName");
final saveFile = File('${directory.path}/$fileName');
if (kDebugMode) {
print(saveFile.path);
}
if (!await directory.exists()) {
final isDirectoryExist = directory.existsSync();
if (!isDirectoryExist) {
await directory.create(recursive: true);
}
if (await directory.exists()) {
if (isDirectoryExist) {
await Dio().download(
url,
saveFile.path,
Expand All @@ -121,26 +118,14 @@ class _PdfViewerPageState extends State<PdfViewerPage> {
}
}

Future<bool> _requestPermission(Permission permission) async {
if (await permission.isGranted) {
return true;
} else {
var result = await permission.request();
if (result == PermissionStatus.granted) {
return true;
}
}
return false;
}

Future<void> fetchUrlPdfFile(String pathReference) async {
try {
final storageRef = FirebaseStorage.instance.ref();
String url = await storageRef.child(pathReference).getDownloadURL();
final url = await storageRef.child(pathReference).getDownloadURL();
setState(() {
remotePathPdf = url;
});
saveFile(url, "${widget.id}.pdf");
await saveFile(url, '${widget.id}.pdf');
} catch (e) {
debugPrint(e.toString());
}
Expand Down
4 changes: 4 additions & 0 deletions app/linux/flutter/generated_plugin_registrant.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@

#include "generated_plugin_registrant.h"

#include <printing/printing_plugin.h>
#include <url_launcher_linux/url_launcher_plugin.h>

void fl_register_plugins(FlPluginRegistry* registry) {
g_autoptr(FlPluginRegistrar) printing_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "PrintingPlugin");
printing_plugin_register_with_registrar(printing_registrar);
g_autoptr(FlPluginRegistrar) url_launcher_linux_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "UrlLauncherPlugin");
url_launcher_plugin_register_with_registrar(url_launcher_linux_registrar);
Expand Down
1 change: 1 addition & 0 deletions app/linux/flutter/generated_plugins.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#

list(APPEND FLUTTER_PLUGIN_LIST
printing
url_launcher_linux
)

Expand Down
2 changes: 2 additions & 0 deletions app/macos/Flutter/GeneratedPluginRegistrant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import firebase_storage
import flutter_local_notifications
import geolocator_apple
import path_provider_foundation
import printing
import shared_preferences_foundation
import url_launcher_macos

Expand All @@ -23,6 +24,7 @@ func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
FlutterLocalNotificationsPlugin.register(with: registry.registrar(forPlugin: "FlutterLocalNotificationsPlugin"))
GeolocatorPlugin.register(with: registry.registrar(forPlugin: "GeolocatorPlugin"))
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
PrintingPlugin.register(with: registry.registrar(forPlugin: "PrintingPlugin"))
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
UrlLauncherPlugin.register(with: registry.registrar(forPlugin: "UrlLauncherPlugin"))
}
40 changes: 40 additions & 0 deletions app/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,22 @@ packages:
url: "https://pub.dev"
source: hosted
version: "3.0.0"
barcode:
dependency: transitive
description:
name: barcode
sha256: "789f898eef0bd88312470bdb2cc996f895ad7dd5f89e9adde84b204546a90b45"
url: "https://pub.dev"
source: hosted
version: "2.2.4"
bidi:
dependency: transitive
description:
name: bidi
sha256: "1a7d0c696324b2089f72e7671fd1f1f64fef44c980f3cebc84e803967c597b63"
url: "https://pub.dev"
source: hosted
version: "2.0.10"
bloc:
dependency: transitive
description:
Expand Down Expand Up @@ -896,6 +912,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.2.1"
pdf:
dependency: transitive
description:
name: pdf
sha256: "9f75fc7f5580ea5e635b5724de58fb27f684c9ad03ed46fdc1aac768e4557315"
url: "https://pub.dev"
source: hosted
version: "3.10.4"
permission_handler:
dependency: "direct main"
description:
Expand Down Expand Up @@ -976,6 +1000,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.5.1"
printing:
dependency: "direct main"
description:
name: printing
sha256: e7c383dca95ee7b88c02dc1c66638628d3dcdc2fb2cc47e7a595facd47e46b56
url: "https://pub.dev"
source: hosted
version: "5.11.0"
provider:
dependency: transitive
description:
Expand All @@ -1000,6 +1032,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.2.3"
qr:
dependency: transitive
description:
name: qr
sha256: "64957a3930367bf97cc211a5af99551d630f2f4625e38af10edd6b19131b64b3"
url: "https://pub.dev"
source: hosted
version: "3.0.1"
quiver:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ dependencies:
cloud_firestore: ^4.9.3
dropdown_search: ^5.0.6
firebase_storage: ^11.3.1
printing: ^5.11.0

dev_dependencies:
build_runner: null
Expand Down
3 changes: 3 additions & 0 deletions app/windows/flutter/generated_plugin_registrant.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <firebase_storage/firebase_storage_plugin_c_api.h>
#include <geolocator_windows/geolocator_windows.h>
#include <permission_handler_windows/permission_handler_windows_plugin.h>
#include <printing/printing_plugin.h>
#include <url_launcher_windows/url_launcher_windows.h>

void RegisterPlugins(flutter::PluginRegistry* registry) {
Expand All @@ -27,6 +28,8 @@ void RegisterPlugins(flutter::PluginRegistry* registry) {
registry->GetRegistrarForPlugin("GeolocatorWindows"));
PermissionHandlerWindowsPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("PermissionHandlerWindowsPlugin"));
PrintingPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("PrintingPlugin"));
UrlLauncherWindowsRegisterWithRegistrar(
registry->GetRegistrarForPlugin("UrlLauncherWindows"));
}
1 change: 1 addition & 0 deletions app/windows/flutter/generated_plugins.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ list(APPEND FLUTTER_PLUGIN_LIST
firebase_storage
geolocator_windows
permission_handler_windows
printing
url_launcher_windows
)

Expand Down

0 comments on commit 4a64af9

Please sign in to comment.