Skip to main content

Django PDFutils

Project description

django-pdfutils

A simple django app to generate PDF documents.

Installation

  1. In your settings.py, add pdfutils to your INSTALLED_APPS.

  2. (r’^reports/’, include(pdfutils.site.urls)), to your urls.py

  3. Add pdfutils.autodiscover() to your urls.py

  4. Create a report.py file in any installed django application.

  5. Create your report(s)

  6. Profit!

Note: If you are using buildout, don’t forget to put pdfutils in your eggs section or else the django-pdfutils dependencies wont be installed.

Example report

Reports are basically views with custom methods and properties.

# -*- coding: utf-8 -*-

from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext as _

from pdfutils.reports import Report
from pdfutils.sites import site
from pdfutils.utils import memoize


class MyUserReport(Report):
    title = _('Users')
    template_name = 'myapp/reports/users-report.html'
    slug = 'users-report'
    orientation = 'portrait'

    @memoize
    def get_users(self):
        """
        This method is not necessary, it is used to showcase the
        memoize decorator which is included in utils. This prevent
        methods from computing their output twice.
        """
        return User.objects.filter(is_staff=True)

    def get_styles(self):
        """
        It is possible to add or override style like so
        """
        self.styles.append('myapp/css/users-report.css')
        return super(AccountStatementReport, self).get_styles()

    def filename(self):
        """
        The filename can be generated dynamically and translated
        """
        return _('Users-report-%(count)s.pdf') % {'count': self.get_users().count() }

    def get_context_data(self):
        """
        Context data is injected just like a normal view
        """
        context = super(AccountStatementReport, self).get_context_data()
        context['user_list'] = self.get_users()
        return context

site.register(MyUserReport)

The slug should obviously be unique since it is used to build the report URL.

For example, with the default settings and URLs, the URL for report above would be /reports/users-report/.

Example template

<html>
    <head>
        {{ STYLES|safe }}
    </head>
    <body class="{% if landscape %}landscape{% else %}portrait{% endif %}">
        <ul>
            {% for user in user_list %}
            <li>{{ user }}</li>
            {% endif %}
        </ul>
        <a href="{% url 'pdfutils:your_report_slug' %}?format=html">Add ?format=html for easy template debug</a>
    </body>
</html>

Some template variables are injected by default in reports:

  • title

  • slug

  • orientation

  • MEDIA_URL

  • STATIC_URL

  • STYLES

Overriding default CSS

Since the default CSS (base.css, portrait.css, landscape.css) are normal static files, they can be overrided from any other django app which has a pdfutils folder in their static folder.

Note: Be sure your applications are listed in the right order in INSTALLED_APPS !

Dependencies

  • django >=1.4, < 1.5.99

  • decorator == 3.4.0, <= 3.9.9

  • PIL == 1.1.7

  • reportlab == 2.5

  • html5lib == 0.90

  • httplib2 == 0.9

  • pyPdf == 1.13

  • xhtml2pdf == 0.0.4

  • django-xhtml2pdf == 0.0.3

Note: dependencies versions are specified in setup.py. The amount of time required to find the right combination of dependency versions is largely to blame for the creation of this project.

Changelog

v1.0.9 (2015-03-28)

  • Fixed setup.py. [Maxime Haineault]

v1.0.8 (2015-03-28)

  • Fixed important memory leak in style loading (thanks Dom) [Maxime Haineault]

v1.0.7 (2015-03-28)

  • Merge pull request #3 from dulaccc/format-filter. [Maxime Haineault]

    Add a format filter to simplify debugging

  • Add a filter using GET parameters to simplify debugging. [Pierre Dulac]

  • Add autodiscover step to the readme file. [Pierre Dulac]

  • Merge branch ‘dulaccc-add-url-name’ [Maxime Haineault]

  • Add a url name property to the urlpattern generated. [Maxime Haineault]

  • Add a url name property to the urlpattern generated. [Pierre Dulac]

  • Merge branch ‘dulaccc-fix-resource-remote-url’ [Maxime Haineault]

  • Let pisa handle remote resources. [Pierre Dulac]

  • Merge pull request #6 from dominicmontreuil/patch-1. [Maxime Haineault]

    Update setup.py @wip

  • Update setup.py. [Dominic Montreuil]

    I needed to use pdfutils along with another package that required httplib2 0.9 or higher.

    I tested pdfutils with httplib2 0.9 and everything works. It should be safe to update to the newer version of httplib2.

  • Cleaning up venv after build. [Maxime Haineault]

v1.0.6 (2015-03-27)

  • Cleanup. [Maxime Haineault]

v1.0.5 (2015-03-27)

  • Fixed build script. [Maxime Haineault]

v1.0.4 (2015-03-27)

v1.0.3 (2015-03-27)

  • Wheel doesn’t build. well.. no wheel. [Maxime Haineault]

v1.0.2 (2015-03-27)

  • Updated .gitignore. [Maxime Haineault]

  • Updated build script, now building wheel. [Maxime Haineault]

v1.0.1 (2015-03-27)

  • Updated .gitignore, almost finished build script, updated setup.py. [Maxime Haineault]

  • Added a release script. [Maxime Haineault]

  • Merged. [Maxime Haineault]

  • Update setup.py. [Maxime Haineault]

  • Updated urls for django 1.5+ (unused), updated gitignore. [Maxime Haineault]

  • Fixed small bug in get_context. [Maxime Haineault]

  • Removed maximum django version restriction, updated gitignore. [Maxime Haineault]

  • Removed PIL dependency. [Maxime Haineault]

  • Removed django-xhtml2pdf dependency. [Maxime Haineault]

  • Removed THUMBNAIL_MEDIA_URL from template variables. [Maxime Haineault]

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

PDFutils-1.1.0.tar.gz (10.2 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page