pdfconversionapi 0.1.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.
- pdfconversionapi-0.1.0/.gitignore +12 -0
- pdfconversionapi-0.1.0/CHANGELOG.md +14 -0
- pdfconversionapi-0.1.0/LICENSE +21 -0
- pdfconversionapi-0.1.0/PKG-INFO +410 -0
- pdfconversionapi-0.1.0/README.md +377 -0
- pdfconversionapi-0.1.0/conformance/README.md +130 -0
- pdfconversionapi-0.1.0/examples/async_batch.py +32 -0
- pdfconversionapi-0.1.0/examples/conversions.py +24 -0
- pdfconversionapi-0.1.0/examples/handle_errors.py +43 -0
- pdfconversionapi-0.1.0/examples/image_watermark.py +27 -0
- pdfconversionapi-0.1.0/examples/merge.py +22 -0
- pdfconversionapi-0.1.0/examples/pdf_to_jpg.py +26 -0
- pdfconversionapi-0.1.0/examples/qr_code.py +21 -0
- pdfconversionapi-0.1.0/examples/quickstart.py +24 -0
- pdfconversionapi-0.1.0/examples/stored_files.py +29 -0
- pdfconversionapi-0.1.0/examples/usage.py +20 -0
- pdfconversionapi-0.1.0/pyproject.toml +75 -0
- pdfconversionapi-0.1.0/src/pdfconversionapi/__init__.py +80 -0
- pdfconversionapi-0.1.0/src/pdfconversionapi/_async_client.py +290 -0
- pdfconversionapi-0.1.0/src/pdfconversionapi/_base.py +134 -0
- pdfconversionapi-0.1.0/src/pdfconversionapi/_client.py +315 -0
- pdfconversionapi-0.1.0/src/pdfconversionapi/_core.py +593 -0
- pdfconversionapi-0.1.0/src/pdfconversionapi/_exceptions.py +170 -0
- pdfconversionapi-0.1.0/src/pdfconversionapi/_files.py +36 -0
- pdfconversionapi-0.1.0/src/pdfconversionapi/_inputs.py +266 -0
- pdfconversionapi-0.1.0/src/pdfconversionapi/_models.py +281 -0
- pdfconversionapi-0.1.0/src/pdfconversionapi/_multipart.py +80 -0
- pdfconversionapi-0.1.0/src/pdfconversionapi/_version.py +1 -0
- pdfconversionapi-0.1.0/src/pdfconversionapi/py.typed +0 -0
- pdfconversionapi-0.1.0/tests/__init__.py +0 -0
- pdfconversionapi-0.1.0/tests/conftest.py +112 -0
- pdfconversionapi-0.1.0/tests/test_client.py +498 -0
- pdfconversionapi-0.1.0/tests/test_conformance.py +334 -0
- pdfconversionapi-0.1.0/tests/test_encoding.py +364 -0
- pdfconversionapi-0.1.0/tests/test_live.py +171 -0
- pdfconversionapi-0.1.0/tests/test_readme.py +110 -0
- pdfconversionapi-0.1.0/tests/test_responses.py +333 -0
- pdfconversionapi-0.1.0/tests/test_retries.py +385 -0
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.1.0 (unreleased)
|
|
4
|
+
|
|
5
|
+
The first release.
|
|
6
|
+
|
|
7
|
+
- `PdfConversionApi` and `AsyncPdfConversionApi`, with `convert`, `convert_file`, `usage`,
|
|
8
|
+
`conversions`, `download`, `download_to` and `delete_stored_file`.
|
|
9
|
+
- Inputs from paths, bytes, binary streams and `UploadFile`; several inputs as repeated `Files`;
|
|
10
|
+
named file parameters such as `ImageFile`; conversions without an input file.
|
|
11
|
+
- Results with `files`, `file`, `save_all`, and stored files that download on `save()`.
|
|
12
|
+
- Typed errors for every status the API documents, and `str(error)` with the request id.
|
|
13
|
+
- Retries that follow the API's documentation: dropped connections, `Retry-After` on 429 and 503
|
|
14
|
+
up to 60 seconds, and one retry of a 500. A 240-second timeout per attempt.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Zeel Shekhaliya
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,410 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: pdfconversionapi
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Official Python client for the PDF Conversion API: convert documents, images and PDFs with one call.
|
|
5
|
+
Project-URL: Homepage, https://pdfconversionapi.com/docs
|
|
6
|
+
Project-URL: Documentation, https://pdfconversionapi.com/docs
|
|
7
|
+
Project-URL: Repository, https://github.com/pdfconversionapi/pdfconversionapi-python
|
|
8
|
+
Project-URL: Issues, https://github.com/pdfconversionapi/pdfconversionapi-python/issues
|
|
9
|
+
Project-URL: Changelog, https://github.com/pdfconversionapi/pdfconversionapi-python/blob/main/CHANGELOG.md
|
|
10
|
+
Project-URL: Pricing, https://pdfconversionapi.com/pricing
|
|
11
|
+
Project-URL: Dashboard, https://app.pdfconversionapi.com
|
|
12
|
+
Author: Zeel Shekhaliya
|
|
13
|
+
License-Expression: MIT
|
|
14
|
+
License-File: LICENSE
|
|
15
|
+
Keywords: api,conversion,docx to pdf,merge pdf,pdf,pdf to jpg,sdk
|
|
16
|
+
Classifier: Development Status :: 4 - Beta
|
|
17
|
+
Classifier: Intended Audience :: Developers
|
|
18
|
+
Classifier: Operating System :: OS Independent
|
|
19
|
+
Classifier: Programming Language :: Python :: 3
|
|
20
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
24
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
25
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
26
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
27
|
+
Classifier: Topic :: Office/Business
|
|
28
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
29
|
+
Classifier: Typing :: Typed
|
|
30
|
+
Requires-Python: >=3.9
|
|
31
|
+
Requires-Dist: httpx<1,>=0.23
|
|
32
|
+
Description-Content-Type: text/markdown
|
|
33
|
+
|
|
34
|
+
# pdfconversionapi
|
|
35
|
+
|
|
36
|
+
The official Python client for the [PDF Conversion API](https://pdfconversionapi.com): convert
|
|
37
|
+
Word, Excel, PowerPoint, images, HTML and more to PDF and back, and merge, split, compress,
|
|
38
|
+
protect or watermark PDFs, with one call.
|
|
39
|
+
|
|
40
|
+
- Docs: https://pdfconversionapi.com/docs
|
|
41
|
+
- API keys: https://app.pdfconversionapi.com
|
|
42
|
+
- Pricing: https://pdfconversionapi.com/pricing
|
|
43
|
+
|
|
44
|
+
It has a synchronous client, `PdfConversionApi`, and an asynchronous one,
|
|
45
|
+
`AsyncPdfConversionApi`, with the same methods. Python 3.9 or later; its only dependency is
|
|
46
|
+
[httpx](https://www.python-httpx.org).
|
|
47
|
+
|
|
48
|
+
## Install
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
pip install pdfconversionapi
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Your API key
|
|
55
|
+
|
|
56
|
+
Sign in at https://app.pdfconversionapi.com to get a free key, and put it in the
|
|
57
|
+
`PDFCONVERSIONAPI_KEY` environment variable, where the client reads it:
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
export PDFCONVERSIONAPI_KEY="paste-your-key-here"
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
You can also pass it as `PdfConversionApi(api_key=...)`. Keep the key on your server: anyone
|
|
64
|
+
who has it can convert files on your account, and the API sends no CORS headers, so it can't be
|
|
65
|
+
called from a web page anyway.
|
|
66
|
+
|
|
67
|
+
## Quick start
|
|
68
|
+
|
|
69
|
+
```python
|
|
70
|
+
from pdfconversionapi import PdfConversionApi
|
|
71
|
+
|
|
72
|
+
client = PdfConversionApi() # reads PDFCONVERSIONAPI_KEY
|
|
73
|
+
result = client.convert_file("report.docx", "pdf")
|
|
74
|
+
result.save_all("out") # writes out/report.pdf
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
`convert_file` takes the input format from the file's extension. `convert` names both formats,
|
|
78
|
+
and takes one input file or a list of them:
|
|
79
|
+
|
|
80
|
+
```text
|
|
81
|
+
client.convert(from_format, to_format, files=None, params=None, *,
|
|
82
|
+
file_name=None, store_file=False, timeout=None)
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
The examples below use our sample files. To run them as written, save
|
|
86
|
+
[report.docx](https://pdfconversionapi.com/samples/docx-to-pdf/input-quarterly-report.docx),
|
|
87
|
+
[season-guide.pdf](https://pdfconversionapi.com/samples/pdf-to-jpg/input-season-guide.pdf),
|
|
88
|
+
[invoice.pdf](https://pdfconversionapi.com/samples/merge/input-invoice.pdf),
|
|
89
|
+
[terms.pdf](https://pdfconversionapi.com/samples/merge/input-terms.pdf),
|
|
90
|
+
[timesheet.pdf](https://pdfconversionapi.com/samples/merge/input-timesheet.pdf) and
|
|
91
|
+
[proposal.pdf](https://pdfconversionapi.com/samples/watermark/input-proposal.pdf) under those
|
|
92
|
+
names, plus any PNG image as `logo.png`. The `examples` folder of the source distribution has
|
|
93
|
+
the same programs as scripts.
|
|
94
|
+
|
|
95
|
+
## Input files
|
|
96
|
+
|
|
97
|
+
An input can be any of these:
|
|
98
|
+
|
|
99
|
+
| You pass | Sent as |
|
|
100
|
+
|---|---|
|
|
101
|
+
| A path: `"report.docx"` or `Path("report.docx")` | The file's own name, `report.docx`. The file is read again for each retry. |
|
|
102
|
+
| Bytes: `b"..."` | `file` |
|
|
103
|
+
| A binary stream, such as `open("report.docx", "rb")` or `io.BytesIO(...)` | An open file's own name, or `file` |
|
|
104
|
+
| `UploadFile.from_path(path, name)`, `UploadFile.from_bytes(data, name)`, `UploadFile.from_stream(stream, name)` | The name you give |
|
|
105
|
+
|
|
106
|
+
**A `str` is always a path, never the file's content.** Only the file's own name is sent,
|
|
107
|
+
never its folders, and the API names the output after it (`report.docx` becomes `report.pdf`).
|
|
108
|
+
A seekable stream is rewound for each retry; any other stream is read into memory once.
|
|
109
|
+
|
|
110
|
+
```python
|
|
111
|
+
from pathlib import Path
|
|
112
|
+
|
|
113
|
+
from pdfconversionapi import PdfConversionApi, UploadFile
|
|
114
|
+
|
|
115
|
+
client = PdfConversionApi()
|
|
116
|
+
data = Path("report.docx").read_bytes()
|
|
117
|
+
result = client.convert("docx", "pdf", UploadFile.from_bytes(data, "Q3 report.docx"))
|
|
118
|
+
print(result.file.name) # Q3 report.pdf
|
|
119
|
+
result.file.save(".") # writes ./Q3 report.pdf
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## Several output files: PDF to JPG
|
|
123
|
+
|
|
124
|
+
Some conversions return one file per page. `result.files` holds them in order, and
|
|
125
|
+
`result.file` is the first.
|
|
126
|
+
|
|
127
|
+
```python
|
|
128
|
+
from pdfconversionapi import PdfConversionApi
|
|
129
|
+
|
|
130
|
+
client = PdfConversionApi()
|
|
131
|
+
result = client.convert_file("season-guide.pdf", "jpg", {"ImageResolution": 72})
|
|
132
|
+
for file in result.files:
|
|
133
|
+
print(file.name, file.size)
|
|
134
|
+
result.save_all("pages") # writes pages/season-guide-1.jpg and pages/season-guide-2.jpg
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
```text
|
|
138
|
+
season-guide-1.jpg 65272
|
|
139
|
+
season-guide-2.jpg 66627
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
## Several input files: merge
|
|
143
|
+
|
|
144
|
+
A list of inputs is sent in the order given. `pdf/to/merge`, `images/to/pdf`,
|
|
145
|
+
`images/to/join` and `file/to/zip` take several files; the others take one.
|
|
146
|
+
|
|
147
|
+
```python
|
|
148
|
+
from pdfconversionapi import PdfConversionApi
|
|
149
|
+
|
|
150
|
+
client = PdfConversionApi()
|
|
151
|
+
result = client.convert(
|
|
152
|
+
"pdf", "merge", ["invoice.pdf", "terms.pdf", "timesheet.pdf"], file_name="october-billing"
|
|
153
|
+
)
|
|
154
|
+
print(result.file.name, result.file.size) # october-billing.pdf 107308
|
|
155
|
+
result.file.save(".")
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
## A second file: image watermark
|
|
159
|
+
|
|
160
|
+
A parameter whose value is a file is sent as a file field of that name, such as `ImageFile` for
|
|
161
|
+
`pdf/to/image-watermark` or `OverlayFile` for `pdf/to/pdf-watermark`. Pass the file as a
|
|
162
|
+
`Path`, bytes, a stream or an `UploadFile`: a `str` parameter value is always text.
|
|
163
|
+
|
|
164
|
+
```python
|
|
165
|
+
from pathlib import Path
|
|
166
|
+
|
|
167
|
+
from pdfconversionapi import PdfConversionApi
|
|
168
|
+
|
|
169
|
+
client = PdfConversionApi()
|
|
170
|
+
result = client.convert(
|
|
171
|
+
"pdf", "image-watermark", "proposal.pdf", {"ImageFile": Path("logo.png"), "Opacity": 30}
|
|
172
|
+
)
|
|
173
|
+
result.file.save("proposal-stamped.pdf")
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
## Parameters
|
|
177
|
+
|
|
178
|
+
`params` is a dict of the conversion's parameters, named as on each conversion's page (for
|
|
179
|
+
example [PDF to JPG](https://pdfconversionapi.com/convert/pdf-to-jpg)). Numbers are sent in
|
|
180
|
+
plain decimal, booleans as `true` or `false`, and `None` is left out. Two parameters work on
|
|
181
|
+
every conversion and have their own keyword arguments: `file_name` (the output's name, without
|
|
182
|
+
the extension) and `store_file` (below). They win over the same names in `params`.
|
|
183
|
+
|
|
184
|
+
```python
|
|
185
|
+
from pdfconversionapi import PdfConversionApi
|
|
186
|
+
|
|
187
|
+
client = PdfConversionApi()
|
|
188
|
+
result = client.convert_file(
|
|
189
|
+
"season-guide.pdf",
|
|
190
|
+
"jpg",
|
|
191
|
+
{"PageRange": "2", "ImageResolution": 72, "ImageWidth": None}, # None is left out
|
|
192
|
+
file_name="guide",
|
|
193
|
+
)
|
|
194
|
+
print([file.name for file in result.files]) # ['guide-2.jpg']
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
The client doesn't check parameter names or values: the API answers a mistake with a
|
|
198
|
+
`BadRequestError` that names the parameter.
|
|
199
|
+
|
|
200
|
+
## No input file: QR codes and barcodes
|
|
201
|
+
|
|
202
|
+
`data/to/qrcode` and `data/to/barcode` take no file, only the `Data` parameter.
|
|
203
|
+
|
|
204
|
+
```python
|
|
205
|
+
from pdfconversionapi import PdfConversionApi
|
|
206
|
+
|
|
207
|
+
client = PdfConversionApi()
|
|
208
|
+
result = client.convert("data", "qrcode", params={"Data": "https://pdfconversionapi.com"})
|
|
209
|
+
result.file.save(".") # writes ./qrcode.svg
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
## Stored files: download and delete
|
|
213
|
+
|
|
214
|
+
By default the converted files come back inside the response. With `store_file=True` the API
|
|
215
|
+
keeps them instead and returns a link per file that works for 3 hours and needs no key. Treat
|
|
216
|
+
the link like a password. Reading `file.data` of a stored file raises `StoredFileError`;
|
|
217
|
+
`save()`, `save_all()`, `download()` and `download_to()` fetch it, and saving streams it to
|
|
218
|
+
disk.
|
|
219
|
+
|
|
220
|
+
```python
|
|
221
|
+
from pdfconversionapi import PdfConversionApi
|
|
222
|
+
|
|
223
|
+
client = PdfConversionApi()
|
|
224
|
+
result = client.convert_file("season-guide.pdf", "jpg", {"ImageResolution": 72}, store_file=True)
|
|
225
|
+
result.save_all("stored") # downloads every file
|
|
226
|
+
for file in result.files:
|
|
227
|
+
print(file.name, file.url)
|
|
228
|
+
deleted = client.delete_stored_file(file.file_id) # True: deleted, False: nothing to delete
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
`file.download()` returns the bytes, and `client.download(url)` and
|
|
232
|
+
`client.download_to(url, path)` take a link you kept. Downloads never send your key.
|
|
233
|
+
|
|
234
|
+
## Usage
|
|
235
|
+
|
|
236
|
+
```python
|
|
237
|
+
from pdfconversionapi import PdfConversionApi
|
|
238
|
+
|
|
239
|
+
client = PdfConversionApi()
|
|
240
|
+
usage = client.usage()
|
|
241
|
+
print(usage.plan, usage.used, usage.included, usage.remaining)
|
|
242
|
+
print("the count starts again on", usage.period_end) # a UTC datetime
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
`client.conversions()` lists every conversion the API offers (`from_format`, `to_format`,
|
|
246
|
+
`title`, `category` and `inputs`); it needs no key.
|
|
247
|
+
|
|
248
|
+
## Async
|
|
249
|
+
|
|
250
|
+
`AsyncPdfConversionApi` has the same methods, awaited, and so do its results (`save_all`,
|
|
251
|
+
`save`, `download`, `download_to`). Share one client between tasks, and keep to your plan's
|
|
252
|
+
conversions at once.
|
|
253
|
+
|
|
254
|
+
```python
|
|
255
|
+
import asyncio
|
|
256
|
+
|
|
257
|
+
from pdfconversionapi import AsyncPdfConversionApi
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
async def main() -> None:
|
|
261
|
+
async with AsyncPdfConversionApi() as client:
|
|
262
|
+
results = await asyncio.gather(
|
|
263
|
+
client.convert_file("report.docx", "pdf"),
|
|
264
|
+
client.convert_file("season-guide.pdf", "jpg", {"ImageResolution": 72}),
|
|
265
|
+
)
|
|
266
|
+
for result in results:
|
|
267
|
+
await result.save_all("out")
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
asyncio.run(main())
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
## Errors and retries
|
|
274
|
+
|
|
275
|
+
Every error is a `PdfConversionApiError`. An error status from the API raises an `ApiError`
|
|
276
|
+
subclass with `status`, `message` (the API's own text), `request_id` (the `x-request-id`
|
|
277
|
+
header; quote it when you write to support), `retry_after` and `body`. `str(error)` reads like
|
|
278
|
+
`HTTP 429: too many requests; slow down (request id: 1a2b...)`, and never contains your key.
|
|
279
|
+
|
|
280
|
+
| Error | When |
|
|
281
|
+
|---|---|
|
|
282
|
+
| `BadRequestError` | 400: a parameter or the request is wrong; the message names it |
|
|
283
|
+
| `AuthenticationError` | 401: the key is missing, unknown or replaced |
|
|
284
|
+
| `PermissionDeniedError` | 403: the key's plan isn't set up |
|
|
285
|
+
| `NotFoundError` | 404: an unsupported conversion, or a stored file that expired or was deleted |
|
|
286
|
+
| `PayloadTooLargeError` | 413: the upload, an image or a page is over a limit |
|
|
287
|
+
| `UnprocessableEntityError` | 422: the file can't be read or converted as asked |
|
|
288
|
+
| `RateLimitError` | 429: too many requests or conversions at once, or the month's conversions are used up |
|
|
289
|
+
| `InternalServerError` | 500 |
|
|
290
|
+
| `ServiceUnavailableError` | 503: the servers are busy, or conversions on the plan are paused |
|
|
291
|
+
| `GatewayTimeoutError` | 504: the conversion ran out of time on the server |
|
|
292
|
+
| `ApiError` | any other status |
|
|
293
|
+
| `MissingApiKeyError` | no key for a call that needs one; nothing was sent |
|
|
294
|
+
| `APITimeoutError` | the client's own timeout ran out |
|
|
295
|
+
| `APIConnectionError` | no response, after the retries |
|
|
296
|
+
| `InvalidResponseError` | a success response the client can't use |
|
|
297
|
+
| `StoredFileError` | `data` of a stored file was read; use `download()` |
|
|
298
|
+
|
|
299
|
+
Bad arguments, such as a format name that isn't 1 to 20 letters, digits or hyphens, raise
|
|
300
|
+
`ValueError` or `TypeError` before anything is sent.
|
|
301
|
+
|
|
302
|
+
```python
|
|
303
|
+
from pdfconversionapi import (
|
|
304
|
+
APIConnectionError,
|
|
305
|
+
ApiError,
|
|
306
|
+
APITimeoutError,
|
|
307
|
+
NotFoundError,
|
|
308
|
+
PdfConversionApi,
|
|
309
|
+
RateLimitError,
|
|
310
|
+
)
|
|
311
|
+
|
|
312
|
+
client = PdfConversionApi()
|
|
313
|
+
try:
|
|
314
|
+
client.convert_file("report.docx", "dwg")
|
|
315
|
+
except NotFoundError as error:
|
|
316
|
+
print(error.status, error.message) # 404 conversion from docx to dwg is not supported
|
|
317
|
+
print(error.request_id)
|
|
318
|
+
except RateLimitError as error:
|
|
319
|
+
print("retry after", error.retry_after, "seconds:", error.message)
|
|
320
|
+
except ApiError as error:
|
|
321
|
+
print(error)
|
|
322
|
+
except (APITimeoutError, APIConnectionError) as error:
|
|
323
|
+
print(error)
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
**Retries happen for you** before an error is raised, up to 4 times (5 attempts):
|
|
327
|
+
|
|
328
|
+
- A request that got no response (refused or dropped connection, a connect timeout) is
|
|
329
|
+
retried after a random wait of up to 2, 4, 8 and 16 seconds.
|
|
330
|
+
- 429 and 503 are retried after the `Retry-After` seconds plus that random wait, when
|
|
331
|
+
`Retry-After` is 60 seconds or less. A longer one, such as a used-up month or paused
|
|
332
|
+
conversions, is raised at once with `retry_after` set.
|
|
333
|
+
- A 500 is retried once.
|
|
334
|
+
- Nothing else is retried: not 400, 401, 403, 404, 413, 422 or 504, not the client's own
|
|
335
|
+
timeout, and not a connection that drops while the response is arriving, because the
|
|
336
|
+
conversion may already have run and counted.
|
|
337
|
+
|
|
338
|
+
Retrying is safe: a conversion changes nothing on the server, and only successful attempts
|
|
339
|
+
count toward your month. `max_retries=0` turns retries off.
|
|
340
|
+
|
|
341
|
+
## Timeouts
|
|
342
|
+
|
|
343
|
+
Each attempt may take 240 seconds for the whole response, the time the API needs for its
|
|
344
|
+
slowest conversions. When the client's timeout runs out it raises `APITimeoutError` and doesn't
|
|
345
|
+
retry: the conversion may still be running on the server, and it counts toward your month if it
|
|
346
|
+
succeeds. Raise the timeout rather than retrying at once, for every call or for one:
|
|
347
|
+
|
|
348
|
+
```python
|
|
349
|
+
from pdfconversionapi import PdfConversionApi
|
|
350
|
+
|
|
351
|
+
client = PdfConversionApi(timeout=600)
|
|
352
|
+
result = client.convert_file("report.docx", "pdf", timeout=900)
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
## Large files
|
|
356
|
+
|
|
357
|
+
The whole response is held in memory while it is read, and files come back base64-encoded, so a
|
|
358
|
+
100 MB result is about 133 MB of JSON. For large outputs, ask for `store_file=True` and save
|
|
359
|
+
the files: each one is streamed from its link to disk. Uploads are streamed from disk.
|
|
360
|
+
|
|
361
|
+
## Configuration
|
|
362
|
+
|
|
363
|
+
```python
|
|
364
|
+
import os
|
|
365
|
+
|
|
366
|
+
import httpx
|
|
367
|
+
|
|
368
|
+
from pdfconversionapi import PdfConversionApi
|
|
369
|
+
|
|
370
|
+
client = PdfConversionApi(
|
|
371
|
+
api_key=os.environ["PDFCONVERSIONAPI_KEY"], # the default
|
|
372
|
+
base_url=os.environ.get("PDFCONVERSIONAPI_BASE_URL"), # None: https://api.pdfconversionapi.com
|
|
373
|
+
timeout=240, # seconds per attempt, for the whole response
|
|
374
|
+
connect_timeout=10, # seconds to open a connection
|
|
375
|
+
max_retries=4, # retries after the first attempt; 0 turns them off
|
|
376
|
+
max_retry_wait=60, # the longest Retry-After to wait for
|
|
377
|
+
http_client=httpx.Client(limits=httpx.Limits(max_connections=20)),
|
|
378
|
+
)
|
|
379
|
+
```
|
|
380
|
+
|
|
381
|
+
| Option | Default |
|
|
382
|
+
|---|---|
|
|
383
|
+
| `api_key` | `PDFCONVERSIONAPI_KEY`. It may be missing: `conversions()` and downloads need no key, and other calls raise `MissingApiKeyError` before sending anything. |
|
|
384
|
+
| `base_url` | `PDFCONVERSIONAPI_BASE_URL`, else `https://api.pdfconversionapi.com` |
|
|
385
|
+
| `timeout` | 240 seconds |
|
|
386
|
+
| `connect_timeout` | 10 seconds |
|
|
387
|
+
| `max_retries` | 4 |
|
|
388
|
+
| `max_retry_wait` | 60 seconds |
|
|
389
|
+
| `http_client` | the client's own. Pass an `httpx.Client` (an `httpx.AsyncClient` for the async client) for proxies (`httpx.Client(proxy=...)`), your own certificates or connection limits. The client doesn't close one you pass in. |
|
|
390
|
+
|
|
391
|
+
One client is safe to share between threads (or tasks, for the async one). Close it with
|
|
392
|
+
`client.close()` (`await client.aclose()`), or use it in a `with` (`async with`) block.
|
|
393
|
+
|
|
394
|
+
Retries are logged at INFO level on the `pdfconversionapi` logger:
|
|
395
|
+
|
|
396
|
+
```python
|
|
397
|
+
import logging
|
|
398
|
+
|
|
399
|
+
logging.basicConfig(level=logging.WARNING)
|
|
400
|
+
logging.getLogger("pdfconversionapi").setLevel(logging.INFO)
|
|
401
|
+
```
|
|
402
|
+
|
|
403
|
+
## Links
|
|
404
|
+
|
|
405
|
+
- [Docs](https://pdfconversionapi.com/docs): requests, responses, errors, limits and every
|
|
406
|
+
conversion
|
|
407
|
+
- [Dashboard](https://app.pdfconversionapi.com): your API key, usage and plan
|
|
408
|
+
- [Pricing](https://pdfconversionapi.com/pricing)
|
|
409
|
+
|
|
410
|
+
MIT license.
|