Skip to main content

Django WeasyPrint CBV

Project description

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

TravisCI Build PyPI Download PyPI Python Versions PyPI License

Installing

Install and update using pip:

pip install -U django-weasyprint

WeasyPrint is automatically installed as a dependency of this package. If you run into any problems be sure to check their install instructions for help!

Usage

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

Example

import functools

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

from django_weasyprint import WeasyTemplateResponseMixin
from django_weasyprint.views import WeasyTemplateResponse


class MyDetailView(DetailView):
    # vanilla Django DetailView
    template_name = 'mymodel.html'

class CustomWeasyTemplateResponse(WeasyTemplateResponse):
    # customized response class to change the default URL fetcher
    def get_url_fetcher(self):
        # disable host and certificate check
        context = ssl.create_default_context()
        context.check_hostname = False
        context.verify_mode = ssl.CERT_NONE
        return functools.partial(django_url_fetcher, ssl_context=context)

class PrintView(WeasyTemplateResponseMixin, MyDetailView):
    # output of MyDetailView 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
    # custom response class to configure url-fetcher
    response_class = CustomWeasyTemplateResponse

class DownloadView(WeasyTemplateResponseMixin, MyDetailView):
    # suggested filename (is required for attachment/download!)
    pdf_filename = 'foo.pdf'

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

Changelog

See CHANGELOG.md

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-2.0.0.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

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

django_weasyprint-2.0.0-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django-weasyprint-2.0.0.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.6.13

File hashes

Hashes for django-weasyprint-2.0.0.tar.gz
Algorithm Hash digest
SHA256 3b82fc0bc9da649aa7f92efaa203f8c8b377893d1e5274d5476b487056962383
MD5 c01cb859f98442b342f94fa9c684ac17
BLAKE2b-256 554a7f59ec60d7b1cdeaea320481acc4daacc29e2d105b7f6f1d9b56b38fd93b

See more details on using hashes here.

File details

Details for the file django_weasyprint-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: django_weasyprint-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 8.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.6.13

File hashes

Hashes for django_weasyprint-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2f42899fff8cb13bf605cf0daddfcc8b2dac8195e7aaccd73c3330363a0a2b10
MD5 9a57fdb2c96c25af22c0b75f87e23b87
BLAKE2b-256 354a46c26f66a78ac6699afd5dc0eb72aac07719a38da057733e2b9b8fc7fff4

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