Skip to content

Commit 1ea0fe9

Browse files
committed
perf(print): open existing PDFs by file path
1 parent 2ba2e6d commit 1ea0fe9

7 files changed

Lines changed: 316 additions & 146 deletions

File tree

‎lib/pdf_book/view/pdf_book_screen.dart‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:otzaria/core/messages/common_messages.dart';
2+
23
import 'dart:io';
34

45
import 'package:otzaria/bookmarks/view/book_bookmarks_action.dart';
@@ -5957,7 +5958,7 @@ class _PdfBookScreenState extends State<PdfBookScreen>
59575958
builder: (_) => PrintingScreen(
59585959
data: Future.value(''),
59595960
bookId: widget.tab.book.title,
5960-
createPdfOverride: (_) => file.readAsBytes(),
5961+
pdfFilePath: file.path,
59615962
initialPage: initialPrintPage,
59625963
isBookView: currentLayoutMode.isBookView,
59635964
pdfOutline: widget.tab.outline.value ?? [],

‎lib/plugins/bridge/plugin_bridge_adapter.dart‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'dart:typed_data';
44
// dart:io מגדיר Link משלו (קישור בקובץ־מערכת) שמתנגש ב-Link של הקישורים.
55
import 'dart:io' hide Link;
66
import 'dart:math' as math;
7+
78
import 'package:collection/collection.dart';
89
import 'package:file_picker/file_picker.dart';
910
import 'package:otzaria/book_common/utils/link_anchor_markers.dart';
@@ -521,8 +522,10 @@ const int _pluginRawLinksMaxRecords = 10000;
521522
/// מספר הפריטים המרבי בקריאת `library.getLinkContent` אחת.
522523
const int _pluginLinkContentMaxItems = 25;
523524

524-
typedef PluginRpcEventSink =
525-
Future<void> Function(String topic, Map<String, dynamic> payload);
525+
typedef PluginRpcEventSink = Future<void> Function(
526+
String topic,
527+
Map<String, dynamic> payload,
528+
);
526529

527530
class _PluginNetworkRequest {
528531
final Uri uri;
@@ -5000,7 +5003,7 @@ class PluginBridgeAdapter {
50005003
return printed ?? false;
50015004
}
50025005

5003-
/// קורא את קובץ ה-PDF לפני פתיחת מסך ההדפסה בחלון הבקשה.
5006+
/// פותח קובץ PDF במסך ההדפסה בחלון הבקשה.
50045007
Future<bool> _defaultPrintPluginFile(
50055008
File file, {
50065009
required String jobName,
@@ -5012,9 +5015,9 @@ class PluginBridgeAdapter {
50125015
if (context == null || !context.mounted) {
50135016
throw Exception('error.unavailable: No window to show the print screen');
50145017
}
5015-
final Uint8List bytes;
50165018
try {
5017-
bytes = await file.readAsBytes();
5019+
final handle = await file.open();
5020+
await handle.close();
50185021
} on FileSystemException {
50195022
// בלי הנתיב: הגשר מחזיר לתוסף את הודעת החריגה כמות שהיא.
50205023
throw Exception('error.unavailable: the file could not be read');
@@ -5029,7 +5032,7 @@ class PluginBridgeAdapter {
50295032
data: Future.value(''),
50305033
// במסלול ה-PDF בלבד: תווית להצגה, לשם עבודת ההדפסה ולשם הקובץ בייצוא.
50315034
bookId: jobName,
5032-
createPdfOverride: (_) async => bytes,
5035+
pdfFilePath: file.path,
50335036
),
50345037
);
50355038
return printed ?? false;

‎lib/printing/print_pdf_source.dart‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import 'dart:io';
2+
import 'dart:math';
3+
import 'dart:typed_data';
4+
5+
import 'package:pdfrx/pdfrx.dart';
6+
7+
/// מקור PDF שנפתח מקובץ, או מבתים של מסמך שנוצר בזיכרון.
8+
class PrintPdfSource {
9+
const PrintPdfSource.file(String path) : _path = path, _data = null;
10+
const PrintPdfSource.data(Uint8List data) : _data = data, _path = null;
11+
12+
final String? _path;
13+
final Uint8List? _data;
14+
15+
Future<Uint8List> readBytes() async =>
16+
_data ?? await File(_path!).readAsBytes();
17+
18+
Future<PdfDocument> open({bool useProgressiveLoading = false}) {
19+
final path = _path;
20+
if (path != null) {
21+
return PdfDocument.openFile(
22+
path,
23+
useProgressiveLoading: useProgressiveLoading,
24+
);
25+
}
26+
final bytes = _data!;
27+
// Generated bytes must stay on the memory-loading path: native read
28+
// callbacks can deadlock when a previous document is being closed.
29+
return PdfDocument.openCustom(
30+
read: (buffer, position, size) {
31+
final count = min(size, bytes.length - position);
32+
if (count <= 0) return 0;
33+
buffer.setRange(0, count, bytes, position);
34+
return count;
35+
},
36+
fileSize: bytes.length,
37+
sourceName: 'print-memory-${identityHashCode(bytes)}',
38+
maxSizeToCacheOnMemory: bytes.length,
39+
useProgressiveLoading: useProgressiveLoading,
40+
);
41+
}
42+
}

0 commit comments

Comments
 (0)