A Django REST Framework package for generating PDF files with WeasyPrint.
Project description
DRF PDF
A Django REST Framework package for generating and serving PDF files in your web applications using weasyprint.
Features
- Generate PDF files dynamically.
- Serve PDFs as API responses.
- Customizable templates for PDF generation.
Installation
pip install drf-weasyprint
Prerequisites
Before using this package, ensure you have WeasyPrint installed. You can install it using pip:
pip install weasyprint
For detailed installation instructions and additional dependencies, refer to the WeasyPrint installation guide.
Usage
-
Configure weasyprint font:
# In settings.py file DRF_WEASYPRINT = { "DEFAULT_WEASYPRINT_FONT_CONFIG": "path.to.your.CustomFontConfiguration", # DEFAULT "weasyprint.text.fonts.FontConfiguration" "WEASYPRINT_LOG_PATH": "path/to/your/logfile",# DEFAULT None "DATA_CONTEXT_NAME": "data", # DEFAULT "data" }
-
Create a view to generate and serve PDFs:
from rest_framework.generics import GenericAPIView from drf_weasyprint.mixins import PDFMixin from drf_weasyprint.renderers import WeasyPrintPDFRenderer class MyPDFView(PDFFileMixin, GenericAPIView): renderer_classes = [WeasyPrintPDFRenderer] template_name = 'my_template.html' filename = 'output.pdf'
-
Add the view to your
urls.py:from django.urls import path from .views import MyPDFView urlpatterns = [ path('pdf/', MyPDFView.as_view(), name='pdf-view'), ]
-
Create a template (
my_template.html) for the PDF content.For more information on WeasyPrint and its capabilities, visit the WeasyPrint documentation.
Contributing
Contributions are welcome! Please submit a pull request or open an issue for any bugs or feature requests.
License
This project is licensed under the MIT License. See the LICENSE file for details.
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 drf_weasyprint-1.0.2.tar.gz.
File metadata
- Download URL: drf_weasyprint-1.0.2.tar.gz
- Upload date:
- Size: 5.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
71f1aa8bea181e2f4fbe8449e2d77864a3b027d4056165edc3d775e265702327
|
|
| MD5 |
27b4816dda9a4096aa7a271a6c1549dd
|
|
| BLAKE2b-256 |
f8a804844be1dce75eea99ab1a9a4c40ca9e2b69f299ca29acaadfb91226f412
|
File details
Details for the file drf_weasyprint-1.0.2-py3-none-any.whl.
File metadata
- Download URL: drf_weasyprint-1.0.2-py3-none-any.whl
- Upload date:
- Size: 6.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
10bc70a3d404e7a83523f4e3d56b0eddcecd3e71f689a1bba7693dba73012cab
|
|
| MD5 |
142dbb5e745255580218d187427cad16
|
|
| BLAKE2b-256 |
9ada28969e5b7614f000a53723914739b8d57a857528ebaa8cbecc9d363b1367
|