A Python client for interfacing with the Gotenberg API
Project description
Gotenberg API Client
Table of Contents
Installation
pip install gotenberg-client
What
This is a Python client for interfacing with Gotenberg, which in turn is a wrapper around powerful tools for PDF generation and creation in various ways, using a stateless API. It's a very powerful tool to generate and manipulate PDFs.
Why
As far as I can tell, no active Python library exists to interface with the Gotenberg API.
Features
- HTTP/2 enabled by default
- Abstract away the handling of multi-part/form-data and deal with
Path
s instead - Based on the modern httpx library
- Full support for type hinting and concrete return types as much as possible
- Nearly full test coverage run against an actual Gotenberg server for multiple Python and PyPy versions
How
All the routes and options from the Gotenberg routes are implemented, with the exception of the Prometheus metrics endpoint. All the routes use the same format and general idea.
- First, you add the file or files you want to process
- Then, configure the endpoint with its various options the route supports
- Finally, run the route and receive your resulting file
- Files will be PDF or ZIP, depending on what endpoint and its configuration. Endpoints which handle multiple files, but don't merge them, return a ZIP archive of the resulting PDFs
For more detailed examples, check the documentation
Examples
Converting a single HTML file into a PDF:
from gotenberg_client import GotenbergClient
with GotenbergClient("http://localhost:3000") as client:
with client.chromium.html_to_pdf() as route:
response = route.index("my-index.html").run()
response.to_file(Path("my-index.pdf"))
Converting an HTML file with additional resources into a PDF:
from gotenberg_client import GotenbergClient
with GotenbergClient("http://localhost:3000") as client:
with client.chromium.html_to_pdf() as route:
response = route.index("my-index.html").resource("image.png").resource("style.css").run()
response.to_file(Path("my-index.pdf"))
Converting an HTML file with additional resources into a PDF/A1a format:
from gotenberg_client import GotenbergClient
from gotenberg_client.options import PdfAFormat
with GotenbergClient("http://localhost:3000") as client:
with client.chromium.html_to_pdf() as route:
response = route.index("my-index.html").resources(["image.png", "style.css"]).pdf_format(PdfAFormat.A2b).run()
response.to_file(Path("my-index.pdf"))
Converting a URL into PDF, in landscape format
from gotenberg_client import GotenbergClient
from gotenberg_client.options import PageOrientation
with GotenbergClient("http://localhost:3000") as client:
with client.chromium.url_to_pdf() as route:
response = route.url("https://hello.world").orient(PageOrientation.Landscape).run()
response.to_file(Path("my-world.pdf"))
To ensure the proper clean up of all used resources, both the client and the route(s) should be
used as context manager. If for some reason you cannot, you should .close
the client and any
routes:
from gotenberg_client import GotenbergClient
try:
client = GotenbergClient("http://localhost:3000")
try:
route = client.merge(["myfile.pdf", "otherfile.pdf"]).run()
finally:
route.close()
finally:
client.close()
The response from any .run()
or .run_with_retry()
will be either a SingleFileResponse
or ZipFileResponse
.
There provide a slimmed down set of fields from an httpx.Response
, including the headers, the status code and
the response content. They also provide two convenience methods:
to_file
- Accepts a path and writes the content of the response to itextract_to
- Only on aZipFileResponse
, extracts the zip into the given directory (which must exist)
License
gotenberg-client
is distributed under the terms of the MPL 2.0 license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file gotenberg_client-0.7.0.tar.gz
.
File metadata
- Download URL: gotenberg_client-0.7.0.tar.gz
- Upload date:
- Size: 410.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3c1ee0aabb9107f0cf9aed84647575b322406ad9eb2c794e1da62bda6a4e74fa |
|
MD5 | eecc6ad0fe7720018c14cf8dde8db254 |
|
BLAKE2b-256 | 39a1500274f5c0a35cf4e6ad8e1f20a9b9a8cab2904af5c31ddf3e2e322c5caa |
File details
Details for the file gotenberg_client-0.7.0-py3-none-any.whl
.
File metadata
- Download URL: gotenberg_client-0.7.0-py3-none-any.whl
- Upload date:
- Size: 30.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8e626307f31bb8e07f2f7153eba57910c6bb15dbad15bc945e15d71aa847bc53 |
|
MD5 | 2c644263eaf13405fce38ba7af52c2e5 |
|
BLAKE2b-256 | 87dda0ce9c5b5beac6c4f9e2fb5887dc5ad655a29e06962bc027bad60dfc8244 |