Metadata-Version: 2.1
Name: django-weasyprint
Version: 1.0.1
Summary: Django WeasyPrint CBV
Home-page: https://github.com/fdemmer/django-weasyprint
Download-URL: https://github.com/fdemmer/django-weasyprint/archive/v1.0.1.tar.gz
Author: Florian Demmer
Author-email: fdemmer@gmail.com
License: Apache-2.0
Classifier: Development Status :: 5 - Production/Stable
Classifier: Environment :: Web Environment
Classifier: Framework :: Django
Classifier: Framework :: Django :: 2.2
Classifier: Framework :: Django :: 3.0
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.6
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.6
License-File: LICENSE
Requires-Dist: Django>=2.2
Requires-Dist: WeasyPrint>=43

django-weasyprint
=================

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

|TravisCI Build| |PyPI Download| |PyPI Python Versions| |PyPI License|

.. |PyPI Download| image:: https://img.shields.io/pypi/v/django-weasyprint.svg
   :target: https://pypi.python.org/pypi/django-weasyprint/

.. |PyPI Python Versions| image:: https://img.shields.io/pypi/pyversions/django-weasyprint.svg
   :target: https://pypi.python.org/pypi/django-weasyprint/

.. |PyPI License| image:: https://img.shields.io/pypi/l/django-weasyprint.svg
   :target: https://pypi.python.org/pypi/django-weasyprint/

.. |TravisCI Build| image:: https://travis-ci.org/fdemmer/django-weasyprint.svg?branch=master
    :target: https://travis-ci.org/fdemmer/django-weasyprint


Installing
----------

Install and update using `pip`_:

.. code-block:: text

    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
<https://weasyprint.readthedocs.io/en/latest/install.html>`_ for help!


Usage
-----

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


Example
-------

.. code:: python

    import functools

    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 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 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
        # custom response class to configure url-fetcher
        response_class = CustomWeasyTemplateResponse

    class MyModelDownloadView(WeasyTemplateResponseMixin, MyModelView):
        # suggested filename (is required for attachment/download!)
        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 'foo-{at}.pdf'.format(
                at=timezone.now().strftime('%Y%m%d-%H%M'),
            )


Changelog
---------

See `CHANGELOG.md`_


Links
-----

* Releases: https://pypi.python.org/pypi/django-weasyprint
* Issue tracker: https://github.com/fdemmer/django-weasyprint/issues
* Code: https://github.com/fdemmer/django-weasyprint


.. _pip: https://pip.pypa.io/en/stable/quickstart
.. _Django: https://www.djangoproject.com
.. _WeasyPrint: http://weasyprint.org

.. _CHANGELOG.md: https://github.com/fdemmer/django-weasyprint/blob/master/CHANGELOG.md
