Skip to main content

Django WeasyPrint CBV

Project description

A Django class-based view generating PDF responses using WeasyPrint.

Installing

Install and update using pip:

pip install -U django-weasyprint

Usage

Use WeasyTemplateView as class based view base class or the just the mixin WeasyTemplateResponseMixin on a TemplateView (or subclass thereof).

Example

from django.conf import settings
from django.views.generic import DetailView

from django_weasyprint import WeasyTemplateResponseMixin
from django_weasyprint.views import CONTENT_TYPE_PNG


class MyModelView(DetailView):
    # vanilla Django DetailView
    model = MyModel
    template_name = 'mymodel.html'


class MyModelPrintView(WeasyTemplateResponseMixin, MyModelView):
    # output of MyModelView rendered as PDF with hardcoded CSS
    pdf_stylesheets = [
        settings.STATIC_ROOT + 'css/app.css',
    ]
    # show pdf in-line (default: True, show download dialog)
    pdf_attachment = False
    # suggested filename (is required for attachment!)
    pdf_filename = 'foo.pdf'


class MyModelImageView(WeasyTemplateResponseMixin, MyModelView):
    # generate a PNG image instead
    content_type = CONTENT_TYPE_PNG

    # dynamically generate filename
    def get_pdf_filename(self):
        return 'bar-{at}.pdf'.format(
            at=timezone.now().strftime('%Y%m%d-%H%M'),
        )

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

django-weasyprint-0.5.5.tar.gz (5.1 kB view details)

Uploaded Source

File details

Details for the file django-weasyprint-0.5.5.tar.gz.

File metadata

  • Download URL: django-weasyprint-0.5.5.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.41.0 CPython/3.6.8

File hashes

Hashes for django-weasyprint-0.5.5.tar.gz
Algorithm Hash digest
SHA256 ee0dbdcbf666eee2b037e0c2246f60a0088286b71903f81dc29306c5f9f6abae
MD5 dd0d06bca6df8a15b2eb62a55441afe2
BLAKE2b-256 9743b350a6dc2e35b1be9644097fd589178fea276846e37b27a048882a607625

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