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

Uploaded Python 3

File details

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

File metadata

  • Download URL: drf_weasyprint-1.0.1.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.1.tar.gz
Algorithm Hash digest
SHA256 9a2d19c4b3c62a70abfaf3da5892fbffc5ca9fa5cc30dd265cb73b9f9c33e2da
MD5 2defa3eb6a1f960b7656e7f01c2b3f60
BLAKE2b-256 e656b20f42cffba3f0c7a547ab8dc5a91cb59a97ea69ac48d16d599065a8c4f3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: drf_weasyprint-1.0.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 99389e78c6b7a0298ce42c906a4c6dbcc8d821f54c48a5cde4f20450ef80be59
MD5 2a47b3f15570ab970cffe6fa27a9ae49
BLAKE2b-256 3c0d9697bb44315134bfc184fb04b7ae38c1b03487323127abf4040fdff35857

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