-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathREADME.rst
185 lines (129 loc) · 5.85 KB
/
README.rst
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
Django-WeasyPrint
=================
|Build| |Coverage| |PyPI Download| |PyPI Python Versions| |PyPI License|
.. |Build| image:: https://github.com/fdemmer/django-weasyprint/workflows/CI/badge.svg?branch=main
:target: https://github.com/fdemmer/django-weasyprint/actions?workflow=CI
.. |Coverage| image:: https://codecov.io/gh/fdemmer/django-weasyprint/branch/master/graph/badge.svg
:target: https://codecov.io/gh/fdemmer/django-weasyprint
.. |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/
A `Django`_ class-based view generating PDF responses using `WeasyPrint`_.
Installation
------------
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!
.. tip::
In version 53 WeasyPrint switched to `pydyf`_ as PDF generator instead of Cairo.
With that change PNG output was dropped and you might encounter other
changes in the generated PDF.
You can continue using WeasyPrint/Cairo by installing django-weasyprint 1.x!
Usage
-----
Use ``WeasyTemplateView`` as class based view base class or the
mixin ``WeasyTemplateResponseMixin`` on a ``TemplateView`` (or subclass
thereof).
Example
-------
.. code:: python
# views.py
import functools
from django.conf import settings
from django.views.generic import DetailView
from django_weasyprint import WeasyTemplateResponseMixin
from django_weasyprint.views import WeasyTemplateResponse
from django_weasyprint.utils import django_url_fetcher
class MyDetailView(DetailView):
# vanilla Django DetailView
template_name = 'mymodel.html'
def custom_url_fetcher(url, *args, **kwargs):
# rewrite requests for CDN URLs to file path in STATIC_ROOT to use local file
cloud_storage_url = 'https://s3.amazonaws.com/django-weasyprint/static/'
if url.startswith(cloud_storage_url):
url = 'file://' + url.replace(cloud_storage_url, settings.STATIC_URL)
return django_url_fetcher(url, *args, **kwargs)
class CustomWeasyTemplateResponse(WeasyTemplateResponse):
# customized response class to pass a kwarg to 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(custom_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'
# set PDF variant to 'pdf/ua-1' (see weasyprint.DEFAULT_OPTIONS)
pdf_options = {'pdf_variant': 'pdf/ua-1'}
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'),
)
.. code:: python
# tasks.py
from celery import shared_task
from django.template.loader import render_to_string
from django_weasyprint.utils import django_url_fetcher
@shared_task
def generate_pdf(filename='mymodel.pdf'):
weasy_html = weasyprint.HTML(
string=render_to_string('mymodel.html'),
url_fetcher=django_url_fetcher,
base_url='file://',
)
weasy_html.write_pdf(filename)
.. code:: html
<!-- mymodel.html -->
<!doctype html>
<html>
<head>
<!-- Use "static" template tag and configure STATIC_URL as usual. -->
<link rel="stylesheet" href="{% static 'css/app.css' %}" />
</head>
<body>
Hello PDF-world!
</body>
</html>
Settings
--------
By default ``WeasyTemplateResponse`` determines the ``base_url`` for
`weasyprint.HTML`_ and `weasyprint.CSS`_ automatically using Django's
``request.build_absolute_uri()``.
To disable that set ``WEASYPRINT_BASEURL`` to a fixed value, e.g.:
.. code:: python
# Disable prefixing relative URLs with request.build_absolute_uri().
# Instead, handle them as absolute file paths.
WEASYPRINT_BASEURL = '/'
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
.. _pydyf: https://doc.courtbouillon.org/pydyf/stable/
.. _weasyprint.HTML: https://doc.courtbouillon.org/weasyprint/stable/api_reference.html?highlight=base_url#weasyprint.HTML
.. _weasyprint.CSS: https://doc.courtbouillon.org/weasyprint/stable/api_reference.html?#weasyprint.CSS
.. _CHANGELOG.md: https://github.com/fdemmer/django-weasyprint/blob/main/CHANGELOG.md