mirror of
https://github.com/venera-app/venera.git
synced 2025-09-27 15:57:25 +00:00
export comic as pdf
This commit is contained in:
@@ -76,7 +76,7 @@ class LocalComic with HistoryMixin implements Comic {
|
||||
cover,
|
||||
));
|
||||
|
||||
String get baseDir => directory.contains("/") ? directory : FilePath.join(LocalManager().path, directory);
|
||||
String get baseDir => (directory.contains('/') || directory.contains('\\')) ? directory : FilePath.join(LocalManager().path, directory);
|
||||
|
||||
@override
|
||||
String get description => "";
|
||||
|
@@ -4,9 +4,11 @@ import 'package:venera/foundation/app.dart';
|
||||
import 'package:venera/foundation/appdata.dart';
|
||||
import 'package:venera/foundation/comic_source/comic_source.dart';
|
||||
import 'package:venera/foundation/local.dart';
|
||||
import 'package:venera/foundation/log.dart';
|
||||
import 'package:venera/pages/downloading_page.dart';
|
||||
import 'package:venera/utils/cbz.dart';
|
||||
import 'package:venera/utils/io.dart';
|
||||
import 'package:venera/utils/pdf.dart';
|
||||
import 'package:venera/utils/translations.dart';
|
||||
|
||||
class LocalComicsPage extends StatefulWidget {
|
||||
@@ -299,8 +301,7 @@ class _LocalComicsPageState extends State<LocalComicsPage> {
|
||||
return ContentDialog(
|
||||
title: "Delete".tl,
|
||||
content: CheckboxListTile(
|
||||
title:
|
||||
Text("Also remove files on disk".tl),
|
||||
title: Text("Also remove files on disk".tl),
|
||||
value: removeComicFile,
|
||||
onChanged: (v) {
|
||||
state(() {
|
||||
@@ -361,6 +362,34 @@ class _LocalComicsPageState extends State<LocalComicsPage> {
|
||||
}
|
||||
controller.close();
|
||||
}),
|
||||
if (!multiSelectMode)
|
||||
MenuEntry(
|
||||
icon: Icons.picture_as_pdf_outlined,
|
||||
text: "Export as pdf".tl,
|
||||
onClick: () async {
|
||||
var cache = FilePath.join(App.cachePath, 'temp.pdf');
|
||||
var controller = showLoadingDialog(
|
||||
context,
|
||||
allowCancel: false,
|
||||
);
|
||||
try {
|
||||
await createPdfFromComicIsolate(
|
||||
comic: c as LocalComic,
|
||||
savePath: cache,
|
||||
);
|
||||
await saveFile(
|
||||
file: File(cache),
|
||||
filename: "${c.title}.pdf",
|
||||
);
|
||||
} catch (e, s) {
|
||||
Log.error("PDF Export", e, s);
|
||||
context.showMessage(message: e.toString());
|
||||
} finally {
|
||||
controller.close();
|
||||
File(cache).deleteIgnoreError();
|
||||
}
|
||||
},
|
||||
)
|
||||
];
|
||||
},
|
||||
),
|
||||
|
@@ -360,8 +360,8 @@ class _IOOverrides extends IOOverrides {
|
||||
}
|
||||
}
|
||||
|
||||
void overrideIO(void Function() f) {
|
||||
IOOverrides.runWithIOOverrides(
|
||||
T overrideIO<T>(T Function() f) {
|
||||
return IOOverrides.runWithIOOverrides<T>(
|
||||
f,
|
||||
_IOOverrides(),
|
||||
);
|
||||
|
92
lib/utils/pdf.dart
Normal file
92
lib/utils/pdf.dart
Normal file
@@ -0,0 +1,92 @@
|
||||
import 'dart:isolate';
|
||||
|
||||
import 'package:pdf/widgets.dart';
|
||||
import 'package:venera/foundation/local.dart';
|
||||
import 'package:venera/utils/io.dart';
|
||||
|
||||
Future<void> _createPdfFromComic({
|
||||
required LocalComic comic,
|
||||
required String savePath,
|
||||
required String localPath,
|
||||
}) async {
|
||||
final pdf = Document(
|
||||
title: comic.title,
|
||||
author: comic.subTitle ?? "",
|
||||
producer: "Venera",
|
||||
);
|
||||
|
||||
pdf.document.outline;
|
||||
|
||||
var baseDir = comic.directory.contains('/') || comic.directory.contains('\\')
|
||||
? comic.directory
|
||||
: FilePath.join(localPath, comic.directory);
|
||||
|
||||
// add cover
|
||||
var imageData = File(FilePath.join(baseDir, comic.cover)).readAsBytesSync();
|
||||
pdf.addPage(Page(
|
||||
build: (Context context) {
|
||||
return Image(MemoryImage(imageData), fit: BoxFit.contain);
|
||||
},
|
||||
));
|
||||
|
||||
bool multiChapters = comic.chapters != null;
|
||||
|
||||
void reorderFiles(List<FileSystemEntity> files) {
|
||||
files.removeWhere(
|
||||
(element) => element is! File || element.path.startsWith('cover'));
|
||||
files.sort((a, b) {
|
||||
var aName = (a as File).name;
|
||||
var bName = (b as File).name;
|
||||
var aNumber = int.tryParse(aName);
|
||||
var bNumber = int.tryParse(bName);
|
||||
if (aNumber != null && bNumber != null) {
|
||||
return aNumber.compareTo(bNumber);
|
||||
}
|
||||
return aName.compareTo(bName);
|
||||
});
|
||||
}
|
||||
|
||||
if (!multiChapters) {
|
||||
var files = Directory(baseDir).listSync();
|
||||
reorderFiles(files);
|
||||
|
||||
for (var file in files) {
|
||||
var imageData = (file as File).readAsBytesSync();
|
||||
pdf.addPage(Page(
|
||||
build: (Context context) {
|
||||
return Image(MemoryImage(imageData), fit: BoxFit.contain);
|
||||
},
|
||||
));
|
||||
}
|
||||
} else {
|
||||
for (var chapter in comic.chapters!.keys) {
|
||||
var files = Directory(FilePath.join(baseDir, chapter)).listSync();
|
||||
reorderFiles(files);
|
||||
for (var file in files) {
|
||||
var imageData = (file as File).readAsBytesSync();
|
||||
pdf.addPage(Page(
|
||||
build: (Context context) {
|
||||
return Image(MemoryImage(imageData), fit: BoxFit.contain);
|
||||
},
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final file = File(savePath);
|
||||
file.writeAsBytesSync(await pdf.save());
|
||||
}
|
||||
|
||||
Future<void> createPdfFromComicIsolate({
|
||||
required LocalComic comic,
|
||||
required String savePath,
|
||||
}) async {
|
||||
var localPath = LocalManager().path;
|
||||
return Isolate.run(() => overrideIO(() async {
|
||||
return await _createPdfFromComic(
|
||||
comic: comic,
|
||||
savePath: savePath,
|
||||
localPath: localPath,
|
||||
);
|
||||
}));
|
||||
}
|
Reference in New Issue
Block a user