• Stars
    star
    145
  • Rank 254,144 (Top 6 %)
  • Language
    Python
  • License
    BSD 3-Clause "New...
  • Created over 14 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

ReportLab-wrapper

PDFDocument

This is a wrapper for ReportLab which allows easy creation of PDF documents:

from io import BytesIO
from pdfdocument.document import PDFDocument

def say_hello():
    f = BytesIO()
    pdf = PDFDocument(f)
    pdf.init_report()
    pdf.h1('Hello World')
    pdf.p('Creating PDFs made easy.')
    pdf.generate()
    return f.getvalue()

Letters and reports

PDFDocument comes with two different PDF templates, letters and reports. The only difference is the layout of the first page: The letter has an additional frame for the address at the top and a smaller main content area.

Usage is as follows:

pdf.init_report()
# Or:
pdf.init_letter()

The letter generates default styles using 9 point fonts as base size, the report uses 8 points. This can be changed by calling pdf.generate_style again.

There exists also a special type of report, the confidential report, the only differences being that the confidentiality is marked using a red cross at the top of the first page and a watermark in the background.

Styles

The call to pdf.generate_style generates a set of predefined styles. (Yes it does!) That includes the following styles; this list is neither exhaustive nor a promise:

  • pdf.style.normal
  • pdf.style.heading1
  • pdf.style.heading2
  • pdf.style.heading3
  • pdf.style.small
  • pdf.style.bold
  • pdf.style.right
  • pdf.style.indented
  • pdf.style.paragraph
  • pdf.style.table

Most of the time you will not use those attributes directly, except in the case of tables. Convenience methods exist for almost all styles as described in the next chapter.

Content

All content passed to the following methods is escaped by default. ReportLab supports a HTML-like markup language, if you want to use it directly you'll have to either use only pdf.p_markup or resort to creating pdfdocument.document.MarkupParagraph instances by hand.

Headings

pdf.h1, pdf.h2, pdf.h3

Paragraphs

pdf.p, pdf.p_markup, pdf.small, pdf.smaller

Unordered lists

pdf.ul

Mini-HTML

pdf.mini_html

Various elements

pdf.hr, pdf.hr_mini, pdf.spacer, pdf.pagebreak, pdf.start_keeptogether, pdf.end_keeptogether, pdf.next_frame,

Tables

pdf.table, pdf.bottom_table

Canvas methods

Canvas methods work with the canvas directly, and not with Platypus objects. They are mostly useful inside stationery functions. You'll mostly use ReportLab's canvas methods directly, and only resort to the following methods for special cases.

pdf.confidential, pdf.draw_watermark, pdf.draw_svg

Additional methods

pdf.append, pdf.restart

Django integration

PDFDocument has a few helpers for generating PDFs in Django views, most notably pdfdocument.utils.pdf_response:

from pdfdocument.utils import pdf_response

def pdf_view(request):
    pdf, response = pdf_response('filename_without_extension')
    # ... more code

    pdf.generate()
    return response

The SVG support uses svglib by Dinu Gherman. It can be found on PyPI: <http://pypi.python.org/pypi/svglib/>

More Repositories

1

plata

Plata - the lean and mean Django-based Shop
Python
197
star
2

html-sanitizer

Allowlist-based HTML cleaner
Python
125
star
3

django-translated-fields

Django model translation without magic-inflicted pain.
Python
108
star
4

django-imagefield

You should probably use this image field instead of Django's built-in models.ImageField.
Python
100
star
5

django-prose-editor

ProseMirror-based HTML editor for Django
JavaScript
91
star
6

towel

Towel: Keeping you DRY since 2010
Python
81
star
7

django-authlib

Utilities for passwordless authentication (using magic links, Google, Facebook and Twitter OAuth currently)
Python
65
star
8

django-admin-ordering

Drag-drop orderable change lists and inlines done right.
Python
64
star
9

workbench

Django-based Agency Software (time tracking, project management, addressbook, offering and invoicing)
Python
42
star
10

django-json-schema-editor

JavaScript
35
star
11

blacknoise

Python
33
star
12

django-email-registration

So simple you'll burst into tears right away.
Python
30
star
13

django-recent-objects

Python
27
star
14

django-newsletter-subscription

Python
27
star
15

django-cte-forest

django-cte-forest implements efficient adjacency list trees using Django and PostgreSQL Common Table Expressions (CTE).
Python
26
star
16

django-user-messages

Offline addon for django.contrib.messages
Python
26
star
17

django-newswall

This is my version of a Tumblelog
Python
24
star
18

django-js-asset

Python
19
star
19

django-geolocation

Python
13
star
20

django-user-payments

User payments and subscriptions for Django
Python
13
star
21

survey

Django-based survey application
Python
11
star
22

django-sitemaps

sitemap.xml generation using lxml with support for alternates.
Python
11
star
23

django-specifications

A place to store auxiliary information for your Django models.
Python
10
star
24

pinging

Simple module offering pinging capabilities for any Django model
Python
8
star
25

traduire

Traduire (french for ยซtranslateยป) is a web-based platform for editing gettext translations.
Python
8
star
26

django-mooch

Reusable interfaces to a few payment providers
Python
7
star
27

django-http-fallback-storage

Storage which automatically downloads missing files from production on access.
Python
5
star
28

django-fast-export

Python
5
star
29

cldr_countries

Support for using the CLDR core territories data in Django
Python
5
star
30

django-curtains

Middleware for only allowing access to staff members
Python
5
star
31

swisdk2

Simple Web Infrastructure SDK
PHP
5
star
32

speckenv

Load environment from .env. Includes some optional Django goodies.
Python
4
star
33

xlsxdocument

Python
4
star
34

hypercube

Simple wireframe hypercube with SDL
C++
4
star
35

plata-options-product

Plata options product (formerly plata/product/modules/options/)
Python
4
star
36

django-webpack-bundle

Python
3
star
37

django-prune-uploads

Python
3
star
38

slack-logbot

Python
3
star
39

django-canonical-domain

Canonical domain redirection for Django
Python
3
star
40

django-fineforms

Form rendering for Django
Python
3
star
41

django-spark

Event sourcing and handling
Python
3
star
42

django-keyed-urls

An app for those cases when you need language-specific URLs in the database for use in templates or as redirects.
Python
3
star
43

django-fhadmin

Python
3
star
44

tusk

Version -1 of FeinCMS (historical)
JavaScript
2
star
45

yatzee

<1h hack
HTML
2
star
46

django-variable-admin

Administration interface styling using CSS variables
CSS
2
star
47

towel-foundation

Utilities for using towel with Zurb Foundation 4
JavaScript
2
star
48

dotfiles

Python
2
star
49

digitalinitiative

2
star
50

borkbak

Rebuild git-managed backup histories, which hold too much cruft from times long gone.
Python
2
star
51

jquery-moodboard

jQuery-based moodboard / slideshow script (v42)
JavaScript
2
star
52

south

South mirror
Python
2
star
53

towel-bootstrap

towel_bootstrap
JavaScript
2
star
54

zivinetz

Zivinetz
Python
2
star
55

django-flock

Python
1
star
56

mmmoney

mmmoney
Python
1
star
57

aoc

Python
1
star
58

fhp

JavaScript
1
star
59

fhp-presentations

1
star
60

django-tinyforum

Python
1
star
61

feincms-photos

Whatever...
Python
1
star
62

django-chet

JavaScript
1
star
63

django-mkadmin

Adds a dashboard and a quick menu to the topbar for creating content and accessing modules
Python
1
star
64

django-milkdown

Python
1
star