Skip to main content

View mixin and template filters for PDF creation from the HTML-template.

Project description

View mixin and template filters for PDF creation from the HTML-template.

Detailed documentation is in the “docs” directory.

Quick start

  1. Add “weasy_pdf” to your INSTALLED_APPS setting like this:

  2. Help functions: safety_get_attribute(), safety_parse_dict(), format_date() (see

  3. Create data-object. When using the safety_get_attribute() function do not forget to join the related tables (prefetch_related, select_related):

    class PDFDataObject:
        def __init__(self, data_obj: MyModel):
            get = safety_get_attribute
            self.data_obj = data_obj
            self.first_name = get(data_obj, 'first_name')
  4. Prepare PDF template. Use built-in template filters: times, parse_dict:

    <!doctype html>
    <html lang="en">
            <meta charset="utf-8">
            {% load static %}
            {% load weasy_pdf %}
            <link rel="stylesheet" href="{% static '/styles/pdf.css' %}">
            <meta name="description" content="Description">
            <meta name="author" content="Author">
            <span>{{ data_object.first_name }}</span>
    <!-- Filters usage:
    {% for i in 8|times %}
        <td colspan="2"></td>
    {% endfor %}
    <td colspan="5">{{ data_object|parse_dict:'0__NAME' }}</td>
  5. Prepare view:

    class PDFView(WeasyPDFViewMixin):
        template_name = 'pdf.html'
        file_name = 'my_pdf.pdf'
        def get_object(self) -> Any:
            passport = get_object_or_404(Passport, pk=self.kwargs.get('pk'))
            return PDFDataObject(passport)


Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for btc-weasy-pdf, version 0.2
Filename, size File type Python version Upload date Hashes
Filename, size btc-weasy-pdf-0.2.tar.gz (4.2 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page