pdfconversionapi 0.1.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,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.
@@ -0,0 +1,16 @@
1
+ pdfconversionapi/__init__.py,sha256=d2s_1E3DvN0VlMUSSrfFe393AhPQ-8nlxVsxuYhrRPc,2023
2
+ pdfconversionapi/_async_client.py,sha256=_4e96HuL7CAr3zSMGqKg8UUfwVUaLPsNoCBWLdNR1Ac,11015
3
+ pdfconversionapi/_base.py,sha256=ms8lEuDkyFVBjq4uqfIf-qKZzhqtVHlPlx6a23YSvWU,4453
4
+ pdfconversionapi/_client.py,sha256=QGtEGFcJ_UoP6sYYlFLC_DD18VOKcYGVKyP3O0MmnwE,12577
5
+ pdfconversionapi/_core.py,sha256=pFpUi8K4QALM5NfzkaLAHUU4W7A5Xxl8rCmyKOR6kGI,21124
6
+ pdfconversionapi/_exceptions.py,sha256=Oj51NhC8kic7bIBN_wXW1InEf9yoXzRsXRAJ0UKw_L0,5023
7
+ pdfconversionapi/_files.py,sha256=tTITRLZVJgQoe0QUgndrnzjMUsi9qEM6UykUpCL2V5E,997
8
+ pdfconversionapi/_inputs.py,sha256=hjuZkYjICtp0Y_UQgbUvalalaDqSZRy9z_iko4ZsOSQ,9674
9
+ pdfconversionapi/_models.py,sha256=0yYFTVs_KlycjMb7Fc1MmUPUabQxwAKb7zLGb839ZhE,9432
10
+ pdfconversionapi/_multipart.py,sha256=_p1hHDOhQX1WvyjEE9wcMRfMxx85F0qLJKjeF9WbKgU,3142
11
+ pdfconversionapi/_version.py,sha256=kUR5RAFc7HCeiqdlX36dZOHkUI5wI6V_43RpEcD8b-0,22
12
+ pdfconversionapi/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
13
+ pdfconversionapi-0.1.0.dist-info/METADATA,sha256=jXZFSiuneWjHaMgwPqizqLYu2fLeoNvs0ZBOHVjl9EI,15977
14
+ pdfconversionapi-0.1.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
15
+ pdfconversionapi-0.1.0.dist-info/licenses/LICENSE,sha256=9qWMFGsmfi90UKvordXw_OWREgLXf2ZARp43QEQzNqk,1072
16
+ pdfconversionapi-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.33.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -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.