flet-printing 0.2.0__tar.gz
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.
- flet_printing-0.2.0/PKG-INFO +113 -0
- flet_printing-0.2.0/README.md +99 -0
- flet_printing-0.2.0/pyproject.toml +39 -0
- flet_printing-0.2.0/setup.cfg +4 -0
- flet_printing-0.2.0/src/flet_printing/__init__.py +5 -0
- flet_printing-0.2.0/src/flet_printing/pdf_preview.py +80 -0
- flet_printing-0.2.0/src/flet_printing/printing.py +106 -0
- flet_printing-0.2.0/src/flet_printing.egg-info/PKG-INFO +113 -0
- flet_printing-0.2.0/src/flet_printing.egg-info/SOURCES.txt +15 -0
- flet_printing-0.2.0/src/flet_printing.egg-info/dependency_links.txt +1 -0
- flet_printing-0.2.0/src/flet_printing.egg-info/requires.txt +1 -0
- flet_printing-0.2.0/src/flet_printing.egg-info/top_level.txt +2 -0
- flet_printing-0.2.0/src/flutter/flet_printing/lib/flet_printing.dart +3 -0
- flet_printing-0.2.0/src/flutter/flet_printing/lib/src/extension.dart +27 -0
- flet_printing-0.2.0/src/flutter/flet_printing/lib/src/pdf_preview_control.dart +39 -0
- flet_printing-0.2.0/src/flutter/flet_printing/lib/src/printing_service.dart +52 -0
- flet_printing-0.2.0/src/flutter/flet_printing/pubspec.yaml +20 -0
|
@@ -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,99 @@
|
|
|
1
|
+
# flet-printing
|
|
2
|
+
|
|
3
|
+
Native print dialog / share sheet for [Flet](https://flet.dev) apps —
|
|
4
|
+
wraps the Flutter [printing](https://pub.dev/packages/printing) package.
|
|
5
|
+
|
|
6
|
+
Flet has no built-in printing API. Flutter has a mature one. This
|
|
7
|
+
extension bridges the gap, the same way official extensions like
|
|
8
|
+
`flet-audio` wrap `audioplayers`.
|
|
9
|
+
|
|
10
|
+
The demand is documented:
|
|
11
|
+
[flet-dev/flet#894](https://github.com/flet-dev/flet/issues/894)
|
|
12
|
+
(Jan 2023) asked for exactly this — PDF printing via the `printing`
|
|
13
|
+
package — was closed as "completed" without a shipped solution, and the
|
|
14
|
+
follow-up question *"What's the completed solution for this?"* (Feb
|
|
15
|
+
2024) was never answered. This package is that answer.
|
|
16
|
+
|
|
17
|
+
日本語補足: Fletには印刷APIがありません(0.85時点・全API確認済み)。
|
|
18
|
+
Flutterの`printing`パッケージ(ネイティブ印刷ダイアログ・共有シート)を
|
|
19
|
+
Flet拡張として包んだものです。ワープロ的なアプリの「ファイル→印刷…」を
|
|
20
|
+
Fletで実装できます。
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
```python
|
|
25
|
+
import flet as ft
|
|
26
|
+
from flet_printing import Printing
|
|
27
|
+
|
|
28
|
+
def main(page: ft.Page):
|
|
29
|
+
printing = Printing()
|
|
30
|
+
page.services.append(printing)
|
|
31
|
+
|
|
32
|
+
async def print_click(e):
|
|
33
|
+
pdf = open("document.pdf", "rb").read()
|
|
34
|
+
sent = await printing.print_pdf(pdf, name="document")
|
|
35
|
+
# sent=True: 印刷された / False: ダイアログでキャンセル
|
|
36
|
+
|
|
37
|
+
page.add(ft.FilledButton("印刷…", on_click=print_click))
|
|
38
|
+
|
|
39
|
+
ft.run(main)
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
API:
|
|
43
|
+
|
|
44
|
+
- `await printing.print_pdf(pdf, name="document")` — open the platform
|
|
45
|
+
print dialog (printer selection / copies / duplex / save-as-PDF) for
|
|
46
|
+
the given PDF bytes. Returns `False` if the user cancelled.
|
|
47
|
+
- `await printing.share_pdf(pdf, filename="document.pdf")` — open the
|
|
48
|
+
platform share sheet (mobile) / save dialog (desktop).
|
|
49
|
+
|
|
50
|
+
### PdfPreview — display a PDF with print/share built in
|
|
51
|
+
|
|
52
|
+
```python
|
|
53
|
+
from flet_printing import PdfPreview
|
|
54
|
+
|
|
55
|
+
page.add(PdfPreview(src=pdf_bytes, pdf_file_name="document.pdf",
|
|
56
|
+
expand=True))
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Renders the PDF pages (scrollable) with an action bar: print and share
|
|
60
|
+
buttons work out of the box. Properties: `src` (bytes or base64),
|
|
61
|
+
`allow_printing`, `allow_sharing`, `use_actions`, `pdf_file_name`,
|
|
62
|
+
`on_printed`/`on_shared` events. `can_change_page_format` /
|
|
63
|
+
`can_change_orientation` are off by default (the PDF you pass is
|
|
64
|
+
already laid out).
|
|
65
|
+
|
|
66
|
+
There is deliberately no HTML-to-PDF here: `printing`'s `convertHtml` is
|
|
67
|
+
deprecated. Generate the PDF yourself (e.g. headless Chrome for exact
|
|
68
|
+
CJK typesetting) and pass the bytes to `print_pdf`.
|
|
69
|
+
|
|
70
|
+
## Installation
|
|
71
|
+
|
|
72
|
+
Like every third-party Flet extension, this works with apps compiled via
|
|
73
|
+
`flet build` (the prebuilt `flet run` client only bundles official
|
|
74
|
+
extensions).
|
|
75
|
+
|
|
76
|
+
Add to your app's `pyproject.toml`:
|
|
77
|
+
|
|
78
|
+
```toml
|
|
79
|
+
dependencies = [
|
|
80
|
+
"flet-printing",
|
|
81
|
+
"flet>=0.85.3",
|
|
82
|
+
]
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
then `flet build linux` (or `windows` / `macos` / `apk` / `ipa`).
|
|
86
|
+
|
|
87
|
+
A runnable example lives in `examples/flet_printing_example`
|
|
88
|
+
(build verified on Linux: extension registers, app launches, print
|
|
89
|
+
dialog opens from the button).
|
|
90
|
+
|
|
91
|
+
## Platform support
|
|
92
|
+
|
|
93
|
+
Follows the `printing` package: Android / iOS / macOS / Windows / Linux
|
|
94
|
+
(via CUPS) / web. See the
|
|
95
|
+
[printing package docs](https://pub.dev/packages/printing) for details.
|
|
96
|
+
|
|
97
|
+
## License
|
|
98
|
+
|
|
99
|
+
MIT
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "flet-printing"
|
|
3
|
+
dynamic = ["version"]
|
|
4
|
+
description = "Native print dialog, share sheet and PDF preview for Flet apps — wraps the Flutter printing package"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.10"
|
|
7
|
+
authors = [
|
|
8
|
+
{ name = "aiseed-dev" }
|
|
9
|
+
]
|
|
10
|
+
keywords = ["flet", "flutter", "printing", "print", "pdf"]
|
|
11
|
+
dependencies = [
|
|
12
|
+
"flet>=0.85.3",
|
|
13
|
+
]
|
|
14
|
+
|
|
15
|
+
[dependency-groups]
|
|
16
|
+
dev = [
|
|
17
|
+
"mkdocs",
|
|
18
|
+
"mkdocs-material",
|
|
19
|
+
"mkdocstrings[python]"
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
[project.urls]
|
|
23
|
+
Homepage = "https://github.com/aiseed-dev/flet-printing"
|
|
24
|
+
Documentation = "https://github.com/aiseed-dev/flet-printing"
|
|
25
|
+
Repository = "https://github.com/aiseed-dev/flet-printing"
|
|
26
|
+
Issues = "https://github.com/aiseed-dev/flet-printing/issues"
|
|
27
|
+
|
|
28
|
+
[tool.setuptools.dynamic]
|
|
29
|
+
version = { attr = "flet_printing.__version__" }
|
|
30
|
+
|
|
31
|
+
[tool.setuptools.package-data]
|
|
32
|
+
"flutter.flet_printing" = ["**/*"]
|
|
33
|
+
|
|
34
|
+
[tool.setuptools]
|
|
35
|
+
license-files = []
|
|
36
|
+
|
|
37
|
+
[build-system]
|
|
38
|
+
requires = ["setuptools"]
|
|
39
|
+
build-backend = "setuptools.build_meta"
|
|
@@ -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,15 @@
|
|
|
1
|
+
README.md
|
|
2
|
+
pyproject.toml
|
|
3
|
+
src/flet_printing/__init__.py
|
|
4
|
+
src/flet_printing/pdf_preview.py
|
|
5
|
+
src/flet_printing/printing.py
|
|
6
|
+
src/flet_printing.egg-info/PKG-INFO
|
|
7
|
+
src/flet_printing.egg-info/SOURCES.txt
|
|
8
|
+
src/flet_printing.egg-info/dependency_links.txt
|
|
9
|
+
src/flet_printing.egg-info/requires.txt
|
|
10
|
+
src/flet_printing.egg-info/top_level.txt
|
|
11
|
+
src/flutter/flet_printing/pubspec.yaml
|
|
12
|
+
src/flutter/flet_printing/lib/flet_printing.dart
|
|
13
|
+
src/flutter/flet_printing/lib/src/extension.dart
|
|
14
|
+
src/flutter/flet_printing/lib/src/pdf_preview_control.dart
|
|
15
|
+
src/flutter/flet_printing/lib/src/printing_service.dart
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
flet>=0.85.3
|
|
@@ -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
|