Skip to main content

A Python client for interfacing with the Gotenberg API

Project description

Gotenberg API Client

PyPI - Version PyPI - Python Version


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 Paths instead
  • Based on the modern httpx library
  • Full support for type hinting and concrete return types as mush 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.

  1. First, you add the file or files you want to process
  2. Then, configure the endpoint with its various options the route supports
  3. 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

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()
      Path("my-index.pdf").write_bytes(response.content)

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()
      Path("my-index.pdf").write_bytes(response.content)

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.A1a).run()
      Path("my-index.pdf").write_bytes(response.content)

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.html_to_pdf() as route:
      response = route.url("https://hello.world").orient(PageOrientation.Landscape).run()
      Path("my-world.pdf").write_bytes(response.content)

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()

License

gotenberg-client is distributed under the terms of the MPL 2.0 license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gotenberg_client-0.1.0.tar.gz (295.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gotenberg_client-0.1.0-py3-none-any.whl (20.1 kB view details)

Uploaded Python 3

File details

Details for the file gotenberg_client-0.1.0.tar.gz.

File metadata

  • Download URL: gotenberg_client-0.1.0.tar.gz
  • Upload date:
  • Size: 295.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for gotenberg_client-0.1.0.tar.gz
Algorithm Hash digest
SHA256 5ade1724eec834fb7b654208ba162424f356ae4c65e8fe28edfb4f6e68cb4a6d
MD5 ea5ad61d34ca35e3309fc2a54529fae3
BLAKE2b-256 5ca40dc8ce7837661d0311399f469d2fc92f684a5b94b66904a514f952f433a7

See more details on using hashes here.

File details

Details for the file gotenberg_client-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for gotenberg_client-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6a73d83545d0c8cf894e1547eab5e407aab44c9fde0120daa1c57d3b811d1b2a
MD5 be5d11ce6c47df4061efe2ed71d8ef6d
BLAKE2b-256 c5284c6f65106e34655b63e983a549169f3b28f4fbd4e39d8ebd03e4cce0c0af

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page