OpenSlides/openslides/utils/pdf.py

247 lines
7.7 KiB
Python
Raw Normal View History

2011-07-31 10:46:29 +02:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
openslides.utils.pdf
~~~~~~~~~~~~~~~~~~~~
2012-04-25 22:29:19 +02:00
Additional definitions for creating PDF documents.
2011-07-31 10:46:29 +02:00
2012-04-25 22:29:19 +02:00
:copyright: 2011, 2012 by OpenSlides team, see AUTHORS.
2011-07-31 10:46:29 +02:00
:license: GNU GPL, see LICENSE for more details.
"""
from datetime import datetime
2012-07-10 13:19:12 +02:00
from os.path import join as path_join
2011-09-04 08:23:03 +02:00
2011-09-02 15:54:13 +02:00
from reportlab.lib.styles import StyleSheet1, ParagraphStyle
2012-07-10 13:19:12 +02:00
from reportlab.lib.units import cm
2011-07-31 10:46:29 +02:00
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
2012-07-10 13:19:12 +02:00
from reportlab.rl_config import defaultPageSize
2011-07-31 10:46:29 +02:00
2012-07-10 13:19:12 +02:00
from django.conf import settings
from django.utils.translation import ugettext as _
from openslides.config.models import config
2011-07-31 10:46:29 +02:00
2011-07-31 10:46:29 +02:00
# register new truetype fonts
2012-07-10 13:19:12 +02:00
pdfmetrics.registerFont(TTFont('Ubuntu', path_join(settings.SITE_ROOT,
'static/fonts/Ubuntu-R.ttf')))
pdfmetrics.registerFont(TTFont('Ubuntu-Bold', path_join(settings.SITE_ROOT,
'static/fonts/Ubuntu-B.ttf')))
pdfmetrics.registerFont(TTFont('Ubuntu-Italic', path_join(settings.SITE_ROOT,
'static/fonts/Ubuntu-RI.ttf')))
2011-07-31 10:46:29 +02:00
2011-07-31 10:46:29 +02:00
# set style information
2012-07-10 13:19:12 +02:00
PAGE_HEIGHT = defaultPageSize[1];
PAGE_WIDTH = defaultPageSize[0]
2011-07-31 10:46:29 +02:00
2011-09-02 15:54:13 +02:00
# set custom stylesheets
stylesheet = StyleSheet1()
2012-07-10 13:19:12 +02:00
stylesheet.add(ParagraphStyle(
name='Normal',
fontName='Ubuntu',
fontSize=10,
leading=12,
))
stylesheet.add(ParagraphStyle(
name='Paragraph',
parent=stylesheet['Normal'],
leading=14,
spaceAfter=15
))
stylesheet.add(ParagraphStyle(
name='Small',
parent=stylesheet['Normal'],
fontSize=8
))
stylesheet.add(ParagraphStyle(
name='Italic',
parent=stylesheet['Normal'],
fontName='Ubuntu-Italic',
spaceAfter=5
))
stylesheet.add(ParagraphStyle(
name='Bold',
parent=stylesheet['Normal'],
fontName='Ubuntu-Bold',
))
stylesheet.add(ParagraphStyle(
name='Heading1',
parent=stylesheet['Bold'],
fontSize=24,
leading=30,
spaceAfter=6,
), alias='h1')
stylesheet.add(ParagraphStyle(
name='Heading2',
parent=stylesheet['Bold'],
fontSize=14,
leading=24,
spaceAfter=10,
), alias='h2')
stylesheet.add(ParagraphStyle(
name='Heading3',
parent=stylesheet['Bold'],
fontSize=12,
leading=20,
), alias='h3')
stylesheet.add(ParagraphStyle(
name='Heading4',
2012-07-10 13:19:12 +02:00
parent=stylesheet['Bold'],
fontSize=10,
leading=20,
))
stylesheet.add(ParagraphStyle(
name='Item',
parent=stylesheet['Normal'],
fontSize=14,
leading=14,
leftIndent=0,
spaceAfter=15,
))
2011-09-02 15:54:13 +02:00
stylesheet.add(ParagraphStyle(name = 'Subitem',
parent = stylesheet['Normal'],
fontSize = 10,
2011-09-11 17:06:32 +02:00
leading = 10,
2011-09-02 15:54:13 +02:00
leftIndent = 20,
spaceAfter = 15)
)
stylesheet.add(ParagraphStyle(name = 'Tablecell',
parent = stylesheet['Normal'],
fontSize = 9)
)
stylesheet.add(ParagraphStyle(name = 'Signaturefield',
parent = stylesheet['Normal'],
spaceBefore = 15)
)
2011-09-02 17:39:32 +02:00
# Ballot stylesheets
stylesheet.add(ParagraphStyle(name = 'Ballot_title',
parent = stylesheet['Bold'],
fontSize = 12,
leading = 14,
leftIndent = 30),
)
stylesheet.add(ParagraphStyle(name = 'Ballot_subtitle',
parent = stylesheet['Normal'],
fontSize = 10,
leading = 12,
2011-09-02 17:39:32 +02:00
leftIndent = 30,
rightIndent = 20,
spaceAfter = 5),
2011-09-02 17:39:32 +02:00
)
stylesheet.add(ParagraphStyle(name = 'Ballot_description',
parent = stylesheet['Normal'],
fontSize = 7,
leading = 10,
2011-09-02 17:39:32 +02:00
leftIndent = 30),
)
stylesheet.add(ParagraphStyle(name = 'Ballot_option',
parent = stylesheet['Normal'],
fontSize = 12,
leading = 24,
leftIndent = 30),
)
stylesheet.add(ParagraphStyle(name = 'Monotype',
parent = stylesheet['Normal'],
fontName = 'Courier',
fontSize = 12,
leading = 24,
leftIndent = 30),
)
2011-09-02 17:39:32 +02:00
stylesheet.add(ParagraphStyle(name = 'Ballot_option_name',
parent = stylesheet['Normal'],
fontSize = 12,
leading = 15,
leftIndent = 30),
)
stylesheet.add(ParagraphStyle(name = 'Ballot_option_group',
parent = stylesheet['Normal'],
fontSize = 8,
leading = 15,
leftIndent = 30),
)
stylesheet.add(ParagraphStyle(name = 'Ballot_option_YNA',
parent = stylesheet['Normal'],
fontSize = 12,
leading = 15,
leftIndent = 49,
spaceAfter = 18),
2011-09-05 09:14:38 +02:00
)
2011-09-02 17:39:32 +02:00
stylesheet.add(ParagraphStyle(name = 'Ballot_option_group_right',
parent = stylesheet['Normal'],
fontSize = 8,
leading = 16,
leftIndent = 49),
)
2012-06-04 10:17:56 +02:00
stylesheet.add(ParagraphStyle(name = 'Badge_title',
parent = stylesheet['Bold'],
fontSize = 16,
leading = 22,
leftIndent = 30),
)
stylesheet.add(ParagraphStyle(name = 'Badge_subtitle',
parent = stylesheet['Normal'],
fontSize = 12,
leading = 24,
leftIndent = 30),
)
2012-07-10 13:19:12 +02:00
stylesheet.add(ParagraphStyle(
name = 'Badge_italic',
parent = stylesheet['Italic'],
fontSize = 12,
leading = 24,
leftIndent = 30,
))
stylesheet.add(ParagraphStyle(
name = 'Badge_qrcode',
fontSize = 12,
leftIndent = 190,
))
2011-07-31 10:46:29 +02:00
2011-09-02 15:54:13 +02:00
2011-07-31 10:46:29 +02:00
def firstPage(canvas, doc):
canvas.saveState()
# page header (with event information)
2012-02-21 13:32:39 +01:00
canvas.setFont('Ubuntu', 10)
2011-07-31 10:46:29 +02:00
canvas.setFillGray(0.4)
2012-06-30 14:50:52 +02:00
2012-07-10 13:19:12 +02:00
title_line = u"%s | %s" % (config["event_name"],
config["event_description"])
2012-06-30 14:50:52 +02:00
if len(title_line) > 75:
title_line = "%s ..." % title_line[:70]
canvas.drawString(2.75 * cm, 28 * cm, title_line)
2012-02-21 13:32:39 +01:00
if config["event_date"] and config["event_location"]:
2012-07-10 13:19:12 +02:00
canvas.drawString(2.75 * cm, 27.6 * cm, u"%s, %s"
% (config["event_date"], config["event_location"]))
2011-07-31 10:46:29 +02:00
# time
2012-02-21 13:32:39 +01:00
canvas.setFont('Ubuntu', 7)
2012-07-10 13:19:12 +02:00
time = datetime.now().strftime(_("%Y-%m-%d %H:%Mh"))
canvas.drawString(15 * cm, 28 * cm, _("Printed: %s") % time)
2011-07-31 10:46:29 +02:00
# title
if doc.title:
2012-02-21 13:32:39 +01:00
canvas.setFont('Ubuntu-Bold', 24)
canvas.setFillGray(0)
2012-07-10 13:19:12 +02:00
canvas.drawString(2.75 * cm, PAGE_HEIGHT - 108, doc.title)
2011-07-31 10:46:29 +02:00
# footer (with page number)
2012-02-21 13:32:39 +01:00
canvas.setFont('Ubuntu', 8)
2011-07-31 10:46:29 +02:00
canvas.setFillGray(0.4)
2012-07-10 13:19:12 +02:00
canvas.drawString(10 * cm, 1 * cm, _("Page %s") % doc.page)
2011-07-31 10:46:29 +02:00
canvas.restoreState()
2011-07-31 10:46:29 +02:00
def laterPages(canvas, doc):
canvas.saveState()
# footer (with page number)
2012-02-21 13:32:39 +01:00
canvas.setFont('Ubuntu', 7)
2011-07-31 10:46:29 +02:00
canvas.setFillGray(0.4)
2012-07-10 13:19:12 +02:00
canvas.drawString(10 * cm, 1 * cm, _("Page %s") % doc.page)
2012-02-21 13:32:39 +01:00
canvas.restoreState()