flet-printing 0.2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,5 @@
1
+ from flet_printing.pdf_preview import PdfPreview
2
+ from flet_printing.printing import Printing
3
+
4
+ __version__ = "0.2.0"
5
+ __all__ = ["Printing", "PdfPreview", "__version__"]
@@ -0,0 +1,80 @@
1
+ """PdfPreview visual control — display a PDF with built-in print/share
2
+ actions (wraps `printing`'s PdfPreview widget).
3
+
4
+ Together with :class:`~flet_printing.Printing` this answers
5
+ `flet-dev/flet#894 <https://github.com/flet-dev/flet/issues/894>`_ in
6
+ full: the original 2023 request asked to *"display a PDF in a widget
7
+ that allows printing or saving it"*.
8
+ """
9
+
10
+ from typing import Optional, Union
11
+
12
+ import flet as ft
13
+
14
+ __all__ = ["PdfPreview"]
15
+
16
+
17
+ @ft.control("PdfPreview")
18
+ class PdfPreview(ft.LayoutControl):
19
+ """Display a PDF document with print / share actions.
20
+
21
+ Renders the pages of a PDF (scrollable) and, by default, shows an
22
+ action bar with print and share buttons — no extra wiring needed::
23
+
24
+ import flet as ft
25
+ from flet_printing import PdfPreview
26
+
27
+ def main(page: ft.Page):
28
+ pdf = open("document.pdf", "rb").read()
29
+ page.add(PdfPreview(src=pdf, expand=True))
30
+
31
+ ft.run(main)
32
+ """
33
+
34
+ src: Optional[Union[str, bytes]] = None
35
+ """
36
+ The PDF document: raw ``bytes`` or a base64-encoded string.
37
+ """
38
+
39
+ allow_printing: bool = True
40
+ """
41
+ Show the print action button.
42
+ """
43
+
44
+ allow_sharing: bool = True
45
+ """
46
+ Show the share action button (share sheet on mobile,
47
+ save dialog on desktop).
48
+ """
49
+
50
+ use_actions: bool = True
51
+ """
52
+ Show the action bar at all. Set to ``False`` for a bare preview.
53
+ """
54
+
55
+ can_change_page_format: bool = False
56
+ """
57
+ Let the viewer switch page formats. Off by default: the PDF passed
58
+ in ``src`` is already laid out, so reflowing is usually meaningless.
59
+ """
60
+
61
+ can_change_orientation: bool = False
62
+ """
63
+ Let the viewer switch page orientation. Off by default, same
64
+ reasoning as ``can_change_page_format``.
65
+ """
66
+
67
+ pdf_file_name: Optional[str] = None
68
+ """
69
+ Suggested file name when the viewer shares/saves the document.
70
+ """
71
+
72
+ on_printed: Optional[ft.ControlEventHandler["PdfPreview"]] = None
73
+ """
74
+ Fires after the document was sent to the printer.
75
+ """
76
+
77
+ on_shared: Optional[ft.ControlEventHandler["PdfPreview"]] = None
78
+ """
79
+ Fires after the document was shared/saved.
80
+ """
@@ -0,0 +1,106 @@
1
+ """Printing service control for Flet — native print dialog via the Flutter
2
+ ``printing`` package.
3
+
4
+ Flet has no built-in printing API (verified against Flet 0.85: nothing
5
+ print/PDF-related is exposed), while Flutter has a mature ``printing``
6
+ package (native print dialog, share sheet, PDF rasterization). This
7
+ extension bridges the two, the same way official extensions like
8
+ ``flet-audio`` wrap ``audioplayers``.
9
+
10
+ Usage::
11
+
12
+ import flet as ft
13
+ from flet_printing import Printing
14
+
15
+ def main(page: ft.Page):
16
+ printing = Printing()
17
+ page.services.append(printing)
18
+
19
+ async def print_click(e):
20
+ pdf = open("document.pdf", "rb").read()
21
+ await printing.print_pdf(pdf, name="document")
22
+
23
+ page.add(ft.FilledButton("印刷…", on_click=print_click))
24
+
25
+ ft.run(main)
26
+
27
+ Note:
28
+ Like every third-party Flet extension, this works with apps compiled
29
+ via ``flet build`` (the prebuilt ``flet run`` client only bundles
30
+ official extensions).
31
+ """
32
+
33
+ from typing import Optional, Union
34
+
35
+ import flet as ft
36
+
37
+ __all__ = ["Printing"]
38
+
39
+
40
+ @ft.control("Printing")
41
+ class Printing(ft.Service):
42
+ """Native printing — print dialog and share sheet for PDF documents.
43
+
44
+ A non-visual service control. Add it to ``page.services`` and call its
45
+ async methods. Wraps the Flutter `printing
46
+ <https://pub.dev/packages/printing>`_ package.
47
+ """
48
+
49
+ async def print_pdf(
50
+ self,
51
+ pdf: Union[bytes, str],
52
+ name: str = "document",
53
+ timeout: Optional[float] = None,
54
+ ) -> bool:
55
+ """Open the platform print dialog for a PDF document.
56
+
57
+ The dialog offers printer selection, copies, duplex, page ranges
58
+ and (on most platforms) "save as PDF" — everything a word
59
+ processor's File→Print is expected to provide.
60
+
61
+ Args:
62
+ pdf: PDF document bytes (or a base64-encoded string).
63
+ name: Print job name shown in the printer queue.
64
+ timeout: Seconds to wait for the dialog to be dismissed.
65
+ ``None`` waits indefinitely (the method returns after the
66
+ user prints or cancels).
67
+
68
+ Returns:
69
+ ``True`` if the document was sent to the printer,
70
+ ``False`` if the user cancelled the dialog.
71
+ """
72
+ return await self._invoke_method(
73
+ "print_pdf",
74
+ arguments={"pdf": pdf, "name": name},
75
+ timeout=timeout,
76
+ )
77
+
78
+ async def share_pdf(
79
+ self,
80
+ pdf: Union[bytes, str],
81
+ filename: str = "document.pdf",
82
+ timeout: Optional[float] = None,
83
+ ) -> bool:
84
+ """Open the platform share sheet for a PDF document.
85
+
86
+ On desktop this typically shows a save dialog; on mobile the
87
+ share sheet (mail, files, AirDrop, …).
88
+
89
+ Args:
90
+ pdf: PDF document bytes (or a base64-encoded string).
91
+ filename: Suggested file name.
92
+ timeout: Seconds to wait. ``None`` waits indefinitely.
93
+
94
+ Returns:
95
+ ``True`` on success.
96
+ """
97
+ return await self._invoke_method(
98
+ "share_pdf",
99
+ arguments={"pdf": pdf, "filename": filename},
100
+ timeout=timeout,
101
+ )
102
+
103
+ # print_html は載せない: printingパッケージのconvertHtmlは非推奨
104
+ # (deprecated)になっており、推奨経路は「PDFは自前で生成してprint_pdfへ
105
+ # 渡す」。HTMLの組版が要るならヘッドレスChrome等で確定的なPDFを作る方が
106
+ # 忠実度も高い(このパッケージの出自であるAISeed工房もその方式)。
@@ -0,0 +1,113 @@
1
+ Metadata-Version: 2.4
2
+ Name: flet-printing
3
+ Version: 0.2.0
4
+ Summary: Native print dialog, share sheet and PDF preview for Flet apps — wraps the Flutter printing package
5
+ Author: aiseed-dev
6
+ Project-URL: Homepage, https://github.com/aiseed-dev/flet-printing
7
+ Project-URL: Documentation, https://github.com/aiseed-dev/flet-printing
8
+ Project-URL: Repository, https://github.com/aiseed-dev/flet-printing
9
+ Project-URL: Issues, https://github.com/aiseed-dev/flet-printing/issues
10
+ Keywords: flet,flutter,printing,print,pdf
11
+ Requires-Python: >=3.10
12
+ Description-Content-Type: text/markdown
13
+ Requires-Dist: flet>=0.85.3
14
+
15
+ # flet-printing
16
+
17
+ Native print dialog / share sheet for [Flet](https://flet.dev) apps —
18
+ wraps the Flutter [printing](https://pub.dev/packages/printing) package.
19
+
20
+ Flet has no built-in printing API. Flutter has a mature one. This
21
+ extension bridges the gap, the same way official extensions like
22
+ `flet-audio` wrap `audioplayers`.
23
+
24
+ The demand is documented:
25
+ [flet-dev/flet#894](https://github.com/flet-dev/flet/issues/894)
26
+ (Jan 2023) asked for exactly this — PDF printing via the `printing`
27
+ package — was closed as "completed" without a shipped solution, and the
28
+ follow-up question *"What's the completed solution for this?"* (Feb
29
+ 2024) was never answered. This package is that answer.
30
+
31
+ 日本語補足: Fletには印刷APIがありません(0.85時点・全API確認済み)。
32
+ Flutterの`printing`パッケージ(ネイティブ印刷ダイアログ・共有シート)を
33
+ Flet拡張として包んだものです。ワープロ的なアプリの「ファイル→印刷…」を
34
+ Fletで実装できます。
35
+
36
+ ## Usage
37
+
38
+ ```python
39
+ import flet as ft
40
+ from flet_printing import Printing
41
+
42
+ def main(page: ft.Page):
43
+ printing = Printing()
44
+ page.services.append(printing)
45
+
46
+ async def print_click(e):
47
+ pdf = open("document.pdf", "rb").read()
48
+ sent = await printing.print_pdf(pdf, name="document")
49
+ # sent=True: 印刷された / False: ダイアログでキャンセル
50
+
51
+ page.add(ft.FilledButton("印刷…", on_click=print_click))
52
+
53
+ ft.run(main)
54
+ ```
55
+
56
+ API:
57
+
58
+ - `await printing.print_pdf(pdf, name="document")` — open the platform
59
+ print dialog (printer selection / copies / duplex / save-as-PDF) for
60
+ the given PDF bytes. Returns `False` if the user cancelled.
61
+ - `await printing.share_pdf(pdf, filename="document.pdf")` — open the
62
+ platform share sheet (mobile) / save dialog (desktop).
63
+
64
+ ### PdfPreview — display a PDF with print/share built in
65
+
66
+ ```python
67
+ from flet_printing import PdfPreview
68
+
69
+ page.add(PdfPreview(src=pdf_bytes, pdf_file_name="document.pdf",
70
+ expand=True))
71
+ ```
72
+
73
+ Renders the PDF pages (scrollable) with an action bar: print and share
74
+ buttons work out of the box. Properties: `src` (bytes or base64),
75
+ `allow_printing`, `allow_sharing`, `use_actions`, `pdf_file_name`,
76
+ `on_printed`/`on_shared` events. `can_change_page_format` /
77
+ `can_change_orientation` are off by default (the PDF you pass is
78
+ already laid out).
79
+
80
+ There is deliberately no HTML-to-PDF here: `printing`'s `convertHtml` is
81
+ deprecated. Generate the PDF yourself (e.g. headless Chrome for exact
82
+ CJK typesetting) and pass the bytes to `print_pdf`.
83
+
84
+ ## Installation
85
+
86
+ Like every third-party Flet extension, this works with apps compiled via
87
+ `flet build` (the prebuilt `flet run` client only bundles official
88
+ extensions).
89
+
90
+ Add to your app's `pyproject.toml`:
91
+
92
+ ```toml
93
+ dependencies = [
94
+ "flet-printing",
95
+ "flet>=0.85.3",
96
+ ]
97
+ ```
98
+
99
+ then `flet build linux` (or `windows` / `macos` / `apk` / `ipa`).
100
+
101
+ A runnable example lives in `examples/flet_printing_example`
102
+ (build verified on Linux: extension registers, app launches, print
103
+ dialog opens from the button).
104
+
105
+ ## Platform support
106
+
107
+ Follows the `printing` package: Android / iOS / macOS / Windows / Linux
108
+ (via CUPS) / web. See the
109
+ [printing package docs](https://pub.dev/packages/printing) for details.
110
+
111
+ ## License
112
+
113
+ MIT
@@ -0,0 +1,12 @@
1
+ flet_printing/__init__.py,sha256=fdAULf9wcgVfZidXR9NN0A2h3fCCQA2ZZ2Pz1K9G2oQ,168
2
+ flet_printing/pdf_preview.py,sha256=ex-mYlq7kr2cz34KJBMJXZkzLA2m0lrcl1M7yJwYZ8o,2177
3
+ flet_printing/printing.py,sha256=_3etZoKQuZIbq97mI3nha4KHuPjLCjNx23ZbCfvfGXU,3570
4
+ flutter/flet_printing/pubspec.yaml,sha256=UEzjC51PvRmh6TPrilrB5HI6nz1te0E1YjMOlgJuL0c,358
5
+ flutter/flet_printing/lib/flet_printing.dart,sha256=a9SiWWuIvlxmMupdiG_BY9pMFGgJPWh8D-lzQNtJ6ho,68
6
+ flutter/flet_printing/lib/src/extension.dart,sha256=4CQ6ZbxNTyLiSkwqR3-uIVs9MRsWXqIt91mJeJJazFw,616
7
+ flutter/flet_printing/lib/src/pdf_preview_control.dart,sha256=EZ5vQinBPyZ9jvLwgsOknFWmukDSjo7d1fZFrA6LPI8,1426
8
+ flutter/flet_printing/lib/src/printing_service.dart,sha256=ce8iguOGczDZdtFOT2GHJzuzVtl_zdcdAtXUcqcVhvw,1639
9
+ flet_printing-0.2.0.dist-info/METADATA,sha256=5N_uzjPvMrSwNYVxNZ0jQKHRsmlrAwYUaGxdlRhfbAs,3898
10
+ flet_printing-0.2.0.dist-info/WHEEL,sha256=K260EYznzXsJYBQGqmI8VTxEdiZYNvDZwW9cBh9-_MA,91
11
+ flet_printing-0.2.0.dist-info/top_level.txt,sha256=daF6i7TsfdlBG6gw82ou32Eod9qMDmdddLizHfvUKEw,22
12
+ flet_printing-0.2.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (83.0.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,2 @@
1
+ flet_printing
2
+ flutter
@@ -0,0 +1,3 @@
1
+ library flet_printing;
2
+
3
+ export "src/extension.dart" show Extension;
@@ -0,0 +1,27 @@
1
+ import 'package:flet/flet.dart';
2
+ import 'package:flutter/widgets.dart';
3
+
4
+ import 'pdf_preview_control.dart';
5
+ import 'printing_service.dart';
6
+
7
+ class Extension extends FletExtension {
8
+ @override
9
+ FletService? createService(Control control) {
10
+ switch (control.type) {
11
+ case "Printing":
12
+ return PrintingService(control: control);
13
+ default:
14
+ return null;
15
+ }
16
+ }
17
+
18
+ @override
19
+ Widget? createWidget(Key? key, Control control) {
20
+ switch (control.type) {
21
+ case "PdfPreview":
22
+ return PdfPreviewControl(key: key, control: control);
23
+ default:
24
+ return null;
25
+ }
26
+ }
27
+ }
@@ -0,0 +1,39 @@
1
+ import 'package:flet/flet.dart';
2
+ import 'package:flutter/material.dart';
3
+ import 'package:printing/printing.dart';
4
+
5
+ /// Visual PDF preview with built-in print/share actions
6
+ /// (wraps `printing`'s PdfPreview widget).
7
+ class PdfPreviewControl extends StatelessWidget {
8
+ final Control control;
9
+
10
+ const PdfPreviewControl({super.key, required this.control});
11
+
12
+ @override
13
+ Widget build(BuildContext context) {
14
+ final resolvedSrc = control.getSrc("src");
15
+ if (resolvedSrc.error != null) {
16
+ throw Exception("PdfPreview src decode error: ${resolvedSrc.error}");
17
+ }
18
+ final bytes = resolvedSrc.bytes;
19
+ if (bytes == null) {
20
+ throw Exception(
21
+ "PdfPreview must have \"src\" specified as PDF bytes or base64.");
22
+ }
23
+
24
+ final preview = PdfPreview(
25
+ build: (format) async => bytes,
26
+ allowPrinting: control.getBool("allow_printing", true)!,
27
+ allowSharing: control.getBool("allow_sharing", true)!,
28
+ canChangePageFormat: control.getBool("can_change_page_format", false)!,
29
+ canChangeOrientation: control.getBool("can_change_orientation", false)!,
30
+ canDebug: false,
31
+ useActions: control.getBool("use_actions", true)!,
32
+ pdfFileName: control.getString("pdf_file_name"),
33
+ onPrinted: (context) => control.triggerEvent("printed"),
34
+ onShared: (context) => control.triggerEvent("shared"),
35
+ );
36
+
37
+ return LayoutControl(control: control, child: preview);
38
+ }
39
+ }
@@ -0,0 +1,52 @@
1
+ import 'dart:convert';
2
+
3
+ import 'package:flet/flet.dart';
4
+ import 'package:flutter/foundation.dart';
5
+ import 'package:printing/printing.dart';
6
+
7
+ /// Non-visual service wrapping the `printing` package:
8
+ /// native print dialog / share sheet for PDF documents.
9
+ class PrintingService extends FletService {
10
+ PrintingService({required super.control});
11
+
12
+ @override
13
+ void init() {
14
+ super.init();
15
+ debugPrint("Printing(${control.id}).init");
16
+ control.addInvokeMethodListener(_invokeMethod);
17
+ }
18
+
19
+ /// PDF bytes arrive either as raw binary (msgpack) or base64 string.
20
+ Uint8List _pdfBytes(dynamic value) {
21
+ if (value is Uint8List) return value;
22
+ if (value is List<int>) return Uint8List.fromList(value);
23
+ if (value is String) return base64Decode(value);
24
+ throw Exception("printing: unsupported pdf argument type: "
25
+ "${value.runtimeType}");
26
+ }
27
+
28
+ Future<dynamic> _invokeMethod(String name, dynamic args) async {
29
+ debugPrint("Printing.$name()");
30
+ switch (name) {
31
+ case "print_pdf":
32
+ final pdf = _pdfBytes(args["pdf"]);
33
+ return Printing.layoutPdf(
34
+ onLayout: (format) async => pdf,
35
+ name: args["name"] as String? ?? "document");
36
+ case "share_pdf":
37
+ final pdf = _pdfBytes(args["pdf"]);
38
+ return Printing.sharePdf(
39
+ bytes: pdf,
40
+ filename: args["filename"] as String? ?? "document.pdf");
41
+ default:
42
+ throw Exception("Unknown Printing method: $name");
43
+ }
44
+ }
45
+
46
+ @override
47
+ void dispose() {
48
+ debugPrint("Printing(${control.id}).dispose");
49
+ control.removeInvokeMethodListener(_invokeMethod);
50
+ super.dispose();
51
+ }
52
+ }
@@ -0,0 +1,20 @@
1
+ name: flet_printing
2
+ description: Native print dialog / share sheet for Flet apps (wraps the printing package)
3
+ version: 0.2.0
4
+ publish_to: none
5
+
6
+ environment:
7
+ sdk: '>=3.3.0 <4.0.0'
8
+ flutter: ">=1.17.0"
9
+
10
+ dependencies:
11
+ flet: ^0.85.3
12
+ flutter:
13
+ sdk: flutter
14
+
15
+ printing: ^5.14.0
16
+
17
+ dev_dependencies:
18
+ flutter_test:
19
+ sdk: flutter
20
+ flutter_lints: ^3.0.0