Skip to main content

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

  1. 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"
    }
    
  2. 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'
    
  3. 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'),
    ]
    
  4. 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


Download files

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

Source Distribution

drf_weasyprint-1.0.0.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

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

drf_weasyprint-1.0.0-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file drf_weasyprint-1.0.0.tar.gz.

File metadata

  • Download URL: drf_weasyprint-1.0.0.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

Hashes for drf_weasyprint-1.0.0.tar.gz
Algorithm Hash digest
SHA256 bac4fd9275fa1cd97b328f2495c178b439d402a150eade1e06a65f9f85fba799
MD5 819b5fe3fadc4ff0d82261d63120eac8
BLAKE2b-256 ab7dd5fad43850cc162b711dae9b5c79d17dedf671d6b1226908214cad553840

See more details on using hashes here.

File details

Details for the file drf_weasyprint-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: drf_weasyprint-1.0.0-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

Hashes for drf_weasyprint-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f2b745db7e1f7841a606a7efd34896596ca4ef31f7379ff194d89f55e1c209a6
MD5 d0bc060ff7927258bbcdcc4eddf89b4a
BLAKE2b-256 d5abad79b033e1f8e64a6c493468fcbf8051b9926c442f0ee69105b720657ba5

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