Django Gotenberg
Project description
Djangotenberg
Djangotenberg is a library for connecting and using the Gotenberg PDF generator within your modern Django project. It allows you to convert HTML to PDF and manage PDF files easily.
Supported Tools
Currently, Djangotenberg supports the following Gotenberg tools:
- Chromium: For converting HTML code to PDF files.
- PDF Engines: For manipulating PDF files.
- Merge PDFs: Combine multiple PDF files.
- Read Metadata: Read metadata from PDF files.
- Write Metadata: Edit or add metadata to PDF files.
Requirements
- Python >= 3.9
- Django >= 4.2
- djangorestframework >= 3.16
- requests >= 2.32
- Gotenberg Server: A running Gotenberg server is required (running via Docker is recommended).
Installation
Install the library via pip:
(Assuming the package is named djangotenberg)
pip install djangotenberg
Or install from Source code:
pip install .
Setup & Configuration
-
Add
djangotenbergtoINSTALLED_APPSin yoursettings.py:# settings.py INSTALLED_APPS = [ # ... 'rest_framework', 'djangotenberg', # ... ]
-
Define
GOTENBERG_CONFIGinsettings.pyto specify the Gotenberg server location:# settings.py GOTENBERG_CONFIG = { # Gotenberg server URL (Required) "GOTENBERG_URL": "http://localhost:3000", # API timeout in seconds (Optional, Default: 10) "GOTENBERG_API_TIMEOUT": 10, }
Usage
1. Using Client (Python Code)
You can use APIClient to send commands directly to Gotenberg.
from djangotenberg.client import APIClient
from djangotenberg.properties import Property, PageSize
client = APIClient()
# Example: Convert HTML string to PDF
html_content = "<h1>Hello, Djangotenberg!</h1>"
# Optional: Configure PDF properties
properties = Property() \
.single_page(True) \
.page_size(PageSize.A4_PAGE_SIZE) \
.margin_top(0.5) \
.build()
response = client.html_to_pdf(html_content, properties=properties)
if response.ok:
with open("output.pdf", "wb") as f:
f.write(response.content)
2. Using Django Views (REST Framework)
Djangotenberg provides a pre-configured URL configuration that you can include directly in your project's urls.py.
Note:
PDFViewhaspermission_classes = [IsAuthenticated]set by default. Therefore, calling the API requires a Token or Login.
urls.py
from django.urls import path, include
urlpatterns = [
# ...
path('api/', include('djangotenberg.urls')),
# ...
]
Available Endpoints:
Once installed and included (assuming mapped to /api/), you can access the API at the following endpoints:
POST /api/pdf-engine/html-to-pdf/: Convert HTML string to PDF- Body:
{ "html": "<h1>Content</h1>", "properties": { "singlePage": true, "paperWidth": 8.27, "paperHeight": 11.69 } }
- Body:
POST /api/pdf-engine/merge/: Merge multiple PDF filesPOST /api/pdf-engine/read-metadata/: Read PDF MetadataPOST /api/pdf-engine/write-metadata/: Write PDF Metadata
License
This project is licensed under the terms of the MIT lincense.
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file djangotenberg-0.2.0.tar.gz.
File metadata
- Download URL: djangotenberg-0.2.0.tar.gz
- Upload date:
- Size: 10.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
10b2e67666d7722220c84059d5942478ce80ffcd4ca5a77825ddb55cd420b12b
|
|
| MD5 |
20341aaf502c9e8076769d7085eb1ac1
|
|
| BLAKE2b-256 |
df06f8e843fd5b33a4fac098321f8984206cfdd0b514b6a77504c3ca1a90dc87
|
File details
Details for the file djangotenberg-0.2.0-py3-none-any.whl.
File metadata
- Download URL: djangotenberg-0.2.0-py3-none-any.whl
- Upload date:
- Size: 10.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8057c9ff01015f1d905e1116bc9a814d066e3e96897589882d413b98e6a3c9c7
|
|
| MD5 |
ec8863a2bb29bbc35b001c464a99fa39
|
|
| BLAKE2b-256 |
801ebc195485646422cdd314ac045fe393863c90fc56452c5f756ba288e5403e
|