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.2.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.2-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

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

Hashes for drf_weasyprint-1.0.2.tar.gz
Algorithm Hash digest
SHA256 71f1aa8bea181e2f4fbe8449e2d77864a3b027d4056165edc3d775e265702327
MD5 27b4816dda9a4096aa7a271a6c1549dd
BLAKE2b-256 f8a804844be1dce75eea99ab1a9a4c40ca9e2b69f299ca29acaadfb91226f412

See more details on using hashes here.

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

Hashes for drf_weasyprint-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 10bc70a3d404e7a83523f4e3d56b0eddcecd3e71f689a1bba7693dba73012cab
MD5 142dbb5e745255580218d187427cad16
BLAKE2b-256 9ada28969e5b7614f000a53723914739b8d57a857528ebaa8cbecc9d363b1367

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