pressjet 0.1.0b2__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.
- pressjet-0.1.0b2/.gitignore +12 -0
- pressjet-0.1.0b2/LICENSE +202 -0
- pressjet-0.1.0b2/PKG-INFO +334 -0
- pressjet-0.1.0b2/README.md +303 -0
- pressjet-0.1.0b2/examples/draft-diagonal.png +0 -0
- pressjet-0.1.0b2/examples/invoice.py +57 -0
- pressjet-0.1.0b2/examples/signature-styles.pdf +0 -0
- pressjet-0.1.0b2/examples/signatures.py +85 -0
- pressjet-0.1.0b2/pyproject.toml +72 -0
- pressjet-0.1.0b2/src/pressjet/__init__.py +182 -0
- pressjet-0.1.0b2/src/pressjet/_builders.py +409 -0
- pressjet-0.1.0b2/src/pressjet/_client.py +1109 -0
- pressjet-0.1.0b2/src/pressjet/_deadline.py +74 -0
- pressjet-0.1.0b2/src/pressjet/_errors.py +104 -0
- pressjet-0.1.0b2/src/pressjet/_transport.py +291 -0
- pressjet-0.1.0b2/src/pressjet/_types.py +406 -0
- pressjet-0.1.0b2/src/pressjet/_version.py +1 -0
- pressjet-0.1.0b2/src/pressjet/py.typed +0 -0
- pressjet-0.1.0b2/tests/conftest.py +142 -0
- pressjet-0.1.0b2/tests/test_builders.py +230 -0
- pressjet-0.1.0b2/tests/test_client.py +622 -0
- pressjet-0.1.0b2/tests/test_conformance.py +160 -0
- pressjet-0.1.0b2/tests/test_integration.py +230 -0
pressjet-0.1.0b2/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: pressjet
|
|
3
|
+
Version: 0.1.0b2
|
|
4
|
+
Summary: Official Python SDK for Pressjet, an open PDF rendering service.
|
|
5
|
+
Project-URL: Homepage, https://github.com/tonysantana1492/pressjet
|
|
6
|
+
Project-URL: Source, https://github.com/tonysantana1492/pressjet/tree/main/sdks/python
|
|
7
|
+
Project-URL: Documentation, https://github.com/tonysantana1492/pressjet/blob/main/docs/sdk-design.md
|
|
8
|
+
Author: Tony Santana
|
|
9
|
+
License-Expression: Apache-2.0
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: pdf,pdf-generation,pressjet,rendering
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
22
|
+
Classifier: Topic :: Printing
|
|
23
|
+
Classifier: Typing :: Typed
|
|
24
|
+
Requires-Python: >=3.11
|
|
25
|
+
Provides-Extra: dev
|
|
26
|
+
Requires-Dist: build>=1.6.1; extra == 'dev'
|
|
27
|
+
Requires-Dist: mypy>=2.4.1; extra == 'dev'
|
|
28
|
+
Requires-Dist: pytest>=9.2; extra == 'dev'
|
|
29
|
+
Requires-Dist: ruff>=0.17.6; extra == 'dev'
|
|
30
|
+
Description-Content-Type: text/markdown
|
|
31
|
+
|
|
32
|
+
# Pressjet for Python
|
|
33
|
+
|
|
34
|
+
**Beta 2** · [Install this SDK from the versioned GitHub release](https://github.com/tonysantana1492/pressjet/blob/v0.1.0-beta.2/docs/beta.md#choose-an-sdk).
|
|
35
|
+
|
|
36
|
+
The official Python SDK for [Pressjet](https://github.com/tonysantana1492/pressjet), an open PDF
|
|
37
|
+
rendering service. Build a document tree with plain functions and render it to PDF, preview it as
|
|
38
|
+
images, draw flows, merge, rasterize and sign PDFs.
|
|
39
|
+
|
|
40
|
+
- Python 3.11+, fully typed (`py.typed`), no dependencies beyond the standard library.
|
|
41
|
+
- A synchronous `Client` and an `asyncio` `AsyncClient` with the same methods.
|
|
42
|
+
- Retries, timeouts and typed errors as described in the
|
|
43
|
+
[SDK design](https://github.com/tonysantana1492/pressjet/blob/main/docs/sdk-design.md).
|
|
44
|
+
|
|
45
|
+
## Install
|
|
46
|
+
|
|
47
|
+
```sh
|
|
48
|
+
python -m pip install https://github.com/tonysantana1492/pressjet/releases/download/v0.1.0-beta.2/pressjet-0.1.0b2-py3-none-any.whl
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Quick start
|
|
52
|
+
|
|
53
|
+
```python
|
|
54
|
+
import pressjet
|
|
55
|
+
from pressjet import document, page, text
|
|
56
|
+
|
|
57
|
+
invoice = document(
|
|
58
|
+
page(
|
|
59
|
+
text("Invoice INV-001", style={"fontSize": 20, "fontWeight": "bold"}),
|
|
60
|
+
text("Billed to ", text("Acme Corp.", style={"fontWeight": "bold"})),
|
|
61
|
+
size="A4",
|
|
62
|
+
style={"padding": 40, "fontFamily": "Helvetica"},
|
|
63
|
+
),
|
|
64
|
+
title="Invoice INV-001",
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
with pressjet.Client(api_key="pj_sk_...", base_url="http://localhost:8080") as client:
|
|
68
|
+
pdf = client.generate(invoice)
|
|
69
|
+
|
|
70
|
+
with open("invoice.pdf", "wb") as file:
|
|
71
|
+
file.write(pdf)
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
`examples/invoice.py` is a complete example.
|
|
75
|
+
|
|
76
|
+
The [signature example](examples/signatures.py) builds a section you can append to a contract.
|
|
77
|
+
Choose `line` (default), `box` or `square` ([PDF sample](examples/signature-styles.pdf)).
|
|
78
|
+
To mix appearances, pass individual
|
|
79
|
+
`signature_field(name, appearance)` values to `signature_section`. With the matching CLI on
|
|
80
|
+
your path, this command generates a two-signer sheet with the optional diagonal draft stamp.
|
|
81
|
+
|
|
82
|
+
```sh
|
|
83
|
+
python examples/signatures.py --style box --watermark diagonal "Ana López" "Jordan Lee" | pressjet render - -o signatures.pdf
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## Builders
|
|
87
|
+
|
|
88
|
+
```python
|
|
89
|
+
from pressjet import document, page, view, text, image, barcode, table, page_number
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
| Builder | Children | Keyword properties |
|
|
93
|
+
| --- | --- | --- |
|
|
94
|
+
| `document(*pages, title=None, author=None, lang=None, created=None, conformance=None, verification=None)` | pages | `lang`: a language tag such as `"es-MX"`; `created`: ISO 8601; `conformance`: `"pdf-a-2b"`, `"pdf-ua-1"` or a sequence of them; `verification`: a `VerificationSettings` dict |
|
|
95
|
+
| `page(*children, size=None, style=None)` | views, text, images, barcodes | `size`: `"LETTER"`, `"A4"`, `"LEGAL"`, `"A5"`, optionally with `landscape` or `portrait` (`"A4 landscape"`), or a width and height (`"210mm 297mm"`) |
|
|
96
|
+
| `view(*children, style=None, fixed=None, hide_on_first_page=None, wrap=None, break_=None, keep_together=None, keep_with_next=None, role=None, level=None, alt=None, id=None)` | views, text, images, barcodes | |
|
|
97
|
+
| `text(*children, style=None, fixed=None, hide_on_first_page=None, wrap=None, break_=None, keep_together=None, keep_with_next=None, role=None, level=None, alt=None, id=None)` | strings, numbers, text, images | |
|
|
98
|
+
| `image(src, style=None, fixed=None, hide_on_first_page=None, wrap=None, break_=None, keep_with_next=None, role=None, alt=None, id=None)` | none | `src`: bytes, base64 or a `data:` URL |
|
|
99
|
+
| `barcode(format, value, error_correction=None, columns=None, style=None, fixed=None, hide_on_first_page=None, wrap=None, break_=None, keep_with_next=None, role=None, alt=None, id=None)` | none | `format`: `"qr"`, `"pdf417"`, `"datamatrix"` or `"code128"` |
|
|
100
|
+
| `table(columns, rows=None, style=None, header_style=None, row_style=None, repeat_header=None, keep_with_next=None, break_=None, id=None)` | none | `columns`: header strings or `{"header", "width", "align", "style"}`; each row holds one cell per column: a string, a number, `None` or a node |
|
|
101
|
+
| `page_number(template="{pageNumber} / {totalPages}", style=None, fixed=True, hide_on_first_page=None)` | none | |
|
|
102
|
+
|
|
103
|
+
- Positional arguments are children; properties are keyword-only.
|
|
104
|
+
- `fixed=True` repeats a node on every page (headers, footers), `wrap=False` keeps it on one page
|
|
105
|
+
and `break_=True` starts it on a new page. The trailing underscore avoids the `break` keyword;
|
|
106
|
+
the node holds the schema's `"break"` key.
|
|
107
|
+
- `keep_together=True` keeps a view or paragraph on one page when it fits, and
|
|
108
|
+
`keep_with_next=True` allows no page break between a node and the next one (a heading and its
|
|
109
|
+
first paragraph).
|
|
110
|
+
- `role` (a `Role` such as `"heading"`, `"table"` or `"artifact"`), `level` (a heading's level,
|
|
111
|
+
1 to 6) and `alt` (alternative text) tag the node for PDF/UA-1. `id` (unique, up to 64 of
|
|
112
|
+
`A-Z a-z 0-9 _ . : -`) lists the node in the layout report's `anchors` and makes it a named
|
|
113
|
+
destination in the PDF.
|
|
114
|
+
- `document(lang=..., created=..., conformance=...)` write a PDF/A-2b or PDF/UA-1 file:
|
|
115
|
+
PDF/A-2b needs `created`, since the engine reads no clock, and PDF/UA-1 needs `title` and
|
|
116
|
+
`lang`.
|
|
117
|
+
- `barcode` draws a symbol as vectors, never split across pages; it goes where a view or an
|
|
118
|
+
image block goes, not inside text. `value` holds 1 to 4000 characters, with `{pageNumber}` and
|
|
119
|
+
`{totalPages}` replaced on each page. `error_correction` is `"L"`, `"M"`, `"Q"` or `"H"` for QR
|
|
120
|
+
and 0 to 8 for PDF417, and `columns` (1 to 30) sets PDF417's data columns. Without a width or
|
|
121
|
+
height a code takes one point per module; `style={"color": ...}` sets the dark modules.
|
|
122
|
+
- `document(verification=...)` stamps every page's margin with a code that names the document, a
|
|
123
|
+
digest of its source, the page number and total and, when the server holds a verification key,
|
|
124
|
+
an Ed25519 signature. `id` is required (up to 64 of `A-Z a-z 0-9 _ . : / -`); `url` (an
|
|
125
|
+
`https://` address a phone camera opens), `format` (`"qr"`, `"pdf417"` or `"datamatrix"`),
|
|
126
|
+
`position` (`"bottom-right"` by default, or `"left-edge"` and so on), `size` (18 to 144
|
|
127
|
+
points), `inset` and `caption` are optional. The code is sized to fit the margin, so it never
|
|
128
|
+
covers content:
|
|
129
|
+
|
|
130
|
+
```python
|
|
131
|
+
lease = document(page(...), verification={"id": "OH-2026-0417", "url": "https://example.com/v"})
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
- `None` and `False` children are dropped and nested lists or tuples are flattened, so
|
|
135
|
+
conditional content and comprehensions read naturally:
|
|
136
|
+
|
|
137
|
+
```python
|
|
138
|
+
view(
|
|
139
|
+
[text(line.name) for line in lines],
|
|
140
|
+
is_paid and text("PAID", style={"color": "#16a34a"}),
|
|
141
|
+
)
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
- Numbers inside text become strings (`2.0` becomes `"2"`).
|
|
145
|
+
- Properties left unset are omitted, never sent as `null`.
|
|
146
|
+
- Image bytes are encoded as standard padded base64; a string `src` is passed through unchanged.
|
|
147
|
+
- Nodes are plain `dict`s (typed as `TypedDict`s such as `DocumentNode`) that `json.dumps`
|
|
148
|
+
serializes to [`spec/document.schema.json`](https://github.com/tonysantana1492/pressjet/blob/main/spec/document.schema.json).
|
|
149
|
+
- `Style` is a `TypedDict` with every style property of the schema, using its camelCase keys and
|
|
150
|
+
`Literal` types for the allowed values, so editors and type checkers complete and check them.
|
|
151
|
+
It includes `display` (`"flex"` or `"block"`), `boxSizing`, `marginVertical`,
|
|
152
|
+
`marginHorizontal`, `fontStyle`, `textDecoration`, `orphans`, `widows`, `transform`,
|
|
153
|
+
`transformOrigin`, the per-side borders (`borderTop`, or `borderLeftWidth`, `borderLeftColor`,
|
|
154
|
+
`borderLeftStyle` and so on) and `borderStyle` with `"none"` and `"hidden"`.
|
|
155
|
+
- `fontFamily` and `size` are open strings: `FontFamily` suggests the built-in families
|
|
156
|
+
(`BuiltInFontFamily`) but takes a CSS-style list such as `"Brand, Archivo, sans-serif"`, and
|
|
157
|
+
`PageSize` suggests the named sizes (`PageSizeName`).
|
|
158
|
+
|
|
159
|
+
## Client
|
|
160
|
+
|
|
161
|
+
| Method | Returns |
|
|
162
|
+
| --- | --- |
|
|
163
|
+
| `generate(document, *, fonts=None)` | PDF `bytes` |
|
|
164
|
+
| `html(html, settings=None)` | PDF `bytes` |
|
|
165
|
+
| `preview(document, options=None, *, fonts=None)` | `Preview(pdf, total_pages, pages)` |
|
|
166
|
+
| `check(document, *, fonts=None)` | `Check(ok, page_count, pages, diagnostics, dropped, anchors)`, the layout report |
|
|
167
|
+
| `check_html(html, settings=None)` | `Check`, the layout report of HTML as `html` prints it |
|
|
168
|
+
| `verify(code, *, public_key=None, layout=None)` | `Verification`, what a scanned verification code says |
|
|
169
|
+
| `draw(flow, append_pdfs=None)` | PDF `bytes` |
|
|
170
|
+
| `merge(pdfs)` | PDF `bytes` |
|
|
171
|
+
| `rasterize(data, options=None)` | `Raster(total_pages, pages)` |
|
|
172
|
+
| `sign(pdf, *, name, reason, signing_time)` | PDF `bytes` |
|
|
173
|
+
| `info()` | the server's capabilities as a `dict`, with `verificationKey` (base64url, or `None`) |
|
|
174
|
+
| `health()` | `{"status": "ok", "version": ...}` |
|
|
175
|
+
|
|
176
|
+
`check` reports what is wrong with a document without rendering it, each problem with a JSON
|
|
177
|
+
Pointer and a fix ([the codes](https://github.com/tonysantana1492/pressjet/blob/main/docs/layout-report.md)).
|
|
178
|
+
A document that cannot be rendered is a report with `ok` false, not an exception, so it may be any
|
|
179
|
+
JSON value, such as one a model wrote:
|
|
180
|
+
|
|
181
|
+
```python
|
|
182
|
+
report = client.check(json.loads(model_output))
|
|
183
|
+
for problem in report.errors:
|
|
184
|
+
print(problem.code, problem.pointer, problem.fix)
|
|
185
|
+
if any(problem.code == DiagnosticCode.MISSING_GLYPH for problem in report.diagnostics):
|
|
186
|
+
...
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
- `barcode_too_small` (with `module`, the module width in millimetres) and `barcode_covered` are
|
|
190
|
+
warnings about barcodes and verification codes.
|
|
191
|
+
- `anchors` is a list of `Anchor(id, page, x, y, width, height)`, one per node with an `id`: the
|
|
192
|
+
page it first appears on (from 1) and its box there, in points from the page's top left. It is
|
|
193
|
+
empty when no node has an `id`.
|
|
194
|
+
- `fonts` is a sequence of `FontFace` dicts, at most 32: `src` (TrueType or OpenType bytes, which
|
|
195
|
+
the SDK encodes as base64, or a base64 string) and optional `family`, `weight` (1 to 1000) and
|
|
196
|
+
`style` (`"normal"`, `"italic"` or `"oblique"`). The document may then name the family in
|
|
197
|
+
`fontFamily`:
|
|
198
|
+
|
|
199
|
+
```python
|
|
200
|
+
brand = {"family": "Brand", "src": Path("Brand-Regular.ttf").read_bytes()}
|
|
201
|
+
pdf = client.generate(invoice, fonts=[brand])
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
- `verify` takes the text a scanner read from a verification code, in either form, and returns
|
|
205
|
+
`Verification(valid, reason, id, digest, page, total_pages, signature, document_matches)`.
|
|
206
|
+
`signature` is `"valid"` or `"invalid"` when a key is known (`public_key`, else the server's
|
|
207
|
+
own), `"unchecked"` when none is, and `"absent"` for an unsigned code. Pass `layout`, the source
|
|
208
|
+
document you kept, to learn whether the code was printed from it. A code whose signature is
|
|
209
|
+
`"absent"` or `"unchecked"` proves only what `document_matches` says. A publishable key may call
|
|
210
|
+
it:
|
|
211
|
+
|
|
212
|
+
```python
|
|
213
|
+
result = client.verify(scanned_text, layout=lease)
|
|
214
|
+
if not result.valid:
|
|
215
|
+
print(result.reason or "the code does not match")
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
- `pages` is a list of `PageImage(page_number, width, height, png)`, where `png` holds the decoded
|
|
219
|
+
PNG bytes.
|
|
220
|
+
- `options` is a `RasterOptions` dict: `scale`, `maximumPages`, `maximumInputPixels`, `mimeType`,
|
|
221
|
+
`maximumImageDimension`. To rasterize an image instead of a PDF, set `mimeType`.
|
|
222
|
+
- Binary inputs (`pdfs`, `append_pdfs`, `data`, `pdf`) accept `bytes`, `bytearray` or
|
|
223
|
+
`memoryview`, which the SDK encodes as base64, or a string that is already base64 or a `data:`
|
|
224
|
+
URL.
|
|
225
|
+
- `flow` is a flow as plain JSON data ([`spec/flow.schema.json`](https://github.com/tonysantana1492/pressjet/blob/main/spec/flow.schema.json)).
|
|
226
|
+
- `signing_time` must be a timezone-aware `datetime`; it is sent as RFC 3339 in UTC. A naive
|
|
227
|
+
`datetime` raises `ValueError` rather than guessing its time zone. Use
|
|
228
|
+
`datetime.now(timezone.utc)`.
|
|
229
|
+
- Every method also takes keyword-only `timeout` (seconds) and `headers` that apply to that call.
|
|
230
|
+
|
|
231
|
+
## Async
|
|
232
|
+
|
|
233
|
+
`AsyncClient` has the same options and methods, as coroutines:
|
|
234
|
+
|
|
235
|
+
```python
|
|
236
|
+
import asyncio
|
|
237
|
+
import pressjet
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
async def main() -> None:
|
|
241
|
+
async with pressjet.AsyncClient() as client:
|
|
242
|
+
pdf = await client.generate(invoice)
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
asyncio.run(main())
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
It runs the standard-library HTTP transport in a worker thread with `asyncio.to_thread`, so calls
|
|
249
|
+
never block the event loop and the package keeps zero dependencies. Cancelling a call stops
|
|
250
|
+
waiting at once; the worker thread finishes the HTTP exchange in the background, bounded by the
|
|
251
|
+
timeout.
|
|
252
|
+
|
|
253
|
+
## Errors
|
|
254
|
+
|
|
255
|
+
All errors derive from `pressjet.PressjetError`.
|
|
256
|
+
|
|
257
|
+
| Error | When |
|
|
258
|
+
| --- | --- |
|
|
259
|
+
| `APIError` | The server refused the request. Has `status`, `code`, `title`, `detail`, `request_id` and `retry_after` (seconds or `None`). |
|
|
260
|
+
| `PressjetConnectionError` | The server could not be reached or the connection failed. `__cause__` keeps the underlying error. |
|
|
261
|
+
| `PressjetTimeoutError` | The call, including retries, did not finish within the timeout. A subclass of `PressjetConnectionError`. |
|
|
262
|
+
| `ResponseTooLargeError` | The answer was larger than `max_response_bytes`. |
|
|
263
|
+
|
|
264
|
+
`APIError`'s message reads `"<title>: <detail> (<code>, request <request_id>)"`. `ErrorCode` is a
|
|
265
|
+
`str` enum of every code in [`spec/openapi.yaml`](https://github.com/tonysantana1492/pressjet/blob/main/spec/openapi.yaml); an answer that is not a
|
|
266
|
+
problem document gets the code `http_<status>`.
|
|
267
|
+
|
|
268
|
+
```python
|
|
269
|
+
from pressjet import APIError, ErrorCode
|
|
270
|
+
|
|
271
|
+
try:
|
|
272
|
+
client.generate(invoice)
|
|
273
|
+
except APIError as error:
|
|
274
|
+
if error.code == ErrorCode.INVALID_DOCUMENT:
|
|
275
|
+
print("Fix the document:", error.detail) # names a JSON Pointer such as /children/0
|
|
276
|
+
else:
|
|
277
|
+
raise
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
## Configuration
|
|
281
|
+
|
|
282
|
+
```python
|
|
283
|
+
pressjet.Client(
|
|
284
|
+
api_key=None, # else PRESSJET_API_KEY; sent as Authorization: Bearer <key>
|
|
285
|
+
base_url=None, # else PRESSJET_URL, else http://localhost:8080
|
|
286
|
+
timeout=60.0, # seconds for the whole call, including retries
|
|
287
|
+
max_retries=2, # after 429, 502, 503, 504 and connection failures
|
|
288
|
+
max_response_bytes=128 * 2**20, # larger answers raise ResponseTooLargeError
|
|
289
|
+
headers=None, # extra headers on every request
|
|
290
|
+
)
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
Options are keyword-only. Rendering is a pure function of the request, so every operation is
|
|
294
|
+
safe to retry. Retries wait for `Retry-After` when the server sends it, otherwise
|
|
295
|
+
`0.5 s × 2^attempt` with ±25% jitter, capped at 8 s, and never past the timeout: when the wait
|
|
296
|
+
would end after the deadline, the last error is raised at once. The timeout bounds each socket
|
|
297
|
+
operation and is checked between reads of the answer. Every request sends
|
|
298
|
+
`User-Agent: pressjet-python/<version>` and a fresh `X-Request-Id`, one per attempt (pass your own
|
|
299
|
+
in `headers` to trace a call). Requests go through `urllib.request`, so the standard proxy
|
|
300
|
+
environment variables apply.
|
|
301
|
+
|
|
302
|
+
`Client` holds no open connections; `close()` and the context manager exist so code can manage it
|
|
303
|
+
like other clients.
|
|
304
|
+
|
|
305
|
+
## Development
|
|
306
|
+
|
|
307
|
+
```sh
|
|
308
|
+
docker run --rm -v "$PWD/../..:/repo" -w /repo/sdks/python python:3.13-slim \
|
|
309
|
+
sh -c "pip install -q -e '.[dev]' && pytest -q && mypy --strict src tests examples && ruff check ."
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
Integration tests run against a real server when `PRESSJET_TEST_URL` and `PRESSJET_TEST_API_KEY`
|
|
313
|
+
are set (`scripts/test-server.sh` starts one), and are skipped otherwise.
|
|
314
|
+
|
|
315
|
+
## License
|
|
316
|
+
|
|
317
|
+
Apache-2.0.
|
|
318
|
+
|
|
319
|
+
## HTML invoices
|
|
320
|
+
|
|
321
|
+
Use `client.html_multipart(html, images, settings=None)` to attach raw JPEG or PNG
|
|
322
|
+
bytes keyed by the image's `src`. See [binary HTML images](../../docs/html.md#binary-images).
|
|
323
|
+
|
|
324
|
+
`client.html("<p>Invoice 42</p>")` returns PDF bytes. `settings` is an `HtmlOptions` dict:
|
|
325
|
+
`stylesheets`, `images` (names mapped to bytes or base64), `omitImages`, `profile`
|
|
326
|
+
(`"standard"`, `"pdf-ua-1"` or `"pdf-a-2b"`), `conformance`, `created`, `lang` (when the HTML has
|
|
327
|
+
no `lang` attribute), `fontFamily`, `fonts` (`FontFace` dicts, as above), `engine`
|
|
328
|
+
(`"native"`, the default, or `"reference"` for the server's optional WeasyPrint service) and
|
|
329
|
+
`verification` (a `VerificationSettings` dict, as for `document`; the native engine only).
|
|
330
|
+
|
|
331
|
+
`client.check_html(html, settings)` sends the same HTML to `/v1/check` and returns the layout
|
|
332
|
+
report, the compiler's findings (`ignored_css`, `ignored_html`, `missing_resource`,
|
|
333
|
+
`font_substituted`, `invalid_request`) first. Its settings are an `HtmlCheckOptions` dict, the
|
|
334
|
+
same keys without `engine` and `verification`. [Deployment and limits](../../docs/self-hosting.md#print-existing-html)
|