browser.pdf - PDF Views

Reusable components for pdf generation.

class waeup.kofa.browser.pdf.A3LandscapePDFCreator[source]

Bases: waeup.kofa.browser.pdf.LandscapePDFCreator

A utility to help with generating PDF docs in A3 landscape format. No watermark is shown.

__doc__ = 'A utility to help with generating PDF docs in\n A3 landscape format. No watermark is shown.\n '
__module__ = 'waeup.kofa.browser.pdf'
pagesize = (1190.551181102362, 841.8897637795275)
waeup.kofa.browser.pdf.CODE_STYLE = <ParagraphStyle 'Code'>

A reportlab paragraph style for output of ‘code’.

waeup.kofa.browser.pdf.ENTRY1_STYLE = <ParagraphStyle 'Entry1'>

A reportlab paragraph style for regular form output.

waeup.kofa.browser.pdf.HEADING3_STYLE = <ParagraphStyle 'Heading3'>

A reportlab paragraph style for ‘normal’ output.

waeup.kofa.browser.pdf.HEADING_STYLE = <ParagraphStyle 'Heading3'>

A reportlab paragraph style for headings.

waeup.kofa.browser.pdf.HEADLINE1_STYLE = <ParagraphStyle 'Header1'>

A reportlab paragraph style for headlines or bold text in form output.

class waeup.kofa.browser.pdf.LandscapePDFCreator[source]

Bases: waeup.kofa.browser.pdf.PDFCreator

A utility to help with generating PDF docs in landscape format.

__doc__ = 'A utility to help with generating PDF docs in\n landscape format.\n '
__module__ = 'waeup.kofa.browser.pdf'
pagesize = (841.8897637795275, 595.275590551181)
class waeup.kofa.browser.pdf.LetterPDFCreator[source]

Bases: waeup.kofa.browser.pdf.PDFCreator

A utility to help with generating PDF docs with an original letterhead.

__doc__ = 'A utility to help with generating PDF docs with an original letterhead.\n '
__module__ = 'waeup.kofa.browser.pdf'
letterhead_pos = [0, 0]
paint_background(canvas, doc)[source]

Paint letterhead background of a PDF.

The doc is expected to be some reportlab SimpleDocTemplate or similar object.

This is a callback method that will be called from reportlab when creating PDFs with create_pdf().

waeup.kofa.browser.pdf.NORMAL_STYLE = <ParagraphStyle 'Normal'>

A reportlab paragraph style for ‘normal’ output.

waeup.kofa.browser.pdf.NOTE_STYLE = <ParagraphStyle 'Note'>

A reportlab paragraph style for notes output at end of documents.

class waeup.kofa.browser.pdf.NumberedCanvas(*args, **kw)[source]

Bases: reportlab.pdfgen.canvas.Canvas

A reportlab canvas for numbering pages after all docs are processed.

Taken from http://code.activestate.com/recipes/546511-page-x-of-y-with-reportlab/ http://code.activestate.com/recipes/576832/

__doc__ = 'A reportlab canvas for numbering pages after all docs are processed.\n\n Taken from\n http://code.activestate.com/recipes/546511-page-x-of-y-with-reportlab/\n http://code.activestate.com/recipes/576832/\n '
__init__(*args, **kw)[source]
__module__ = 'waeup.kofa.browser.pdf'
draw_page_number(page_count)[source]

draw string at bottom right with ‘page x of y’.

Location of the string is determined by canvas attributes kofa_footer_x_pos and kofa_footer_y_pos that have to be set manually.

If this canvas also provides an attribute kofa_footer_text, the contained text is rendered left of the page x of y string.

save()[source]

add page info to each page (page x of y)

showPage()[source]
class waeup.kofa.browser.pdf.PDFCreator[source]

Bases: grokcore.component.components.GlobalUtility

A utility to help with generating PDF docs.

__doc__ = 'A utility to help with generating PDF docs.\n '
__implemented__ = <implementedBy waeup.kofa.browser.pdf.PDFCreator>
__module__ = 'waeup.kofa.browser.pdf'
__provides__

Special descriptor for class __provides__

The descriptor caches the implementedBy info, so that we can get declarations for objects without instance-specific interfaces a bit quicker.

For example:

>>> from zope.interface import Interface
>>> class IFooFactory(Interface):
...     pass
>>> class IFoo(Interface):
...     pass
>>> class C(object):
...     implements(IFoo)
...     classProvides(IFooFactory)
>>> [i.getName() for i in C.__provides__]
['IFooFactory']
>>> [i.getName() for i in C().__provides__]
['IFoo']
classmethod _addCourse(table_data, row_num, course_label, course_link, lang, domain)[source]

Add course data to table_data.

classmethod _addDeptAndFaculty(table_data, row_num, dept, faculty, lang, domain)[source]

Add dept and faculty as table rows to table_data.

dept and faculty are expected to be strings or None. In latter case they are not put into the table.

classmethod _drawSignatureBoxes(canvas, width, height, signatures=[])[source]

Draw signature boxes into canvas.

_getWidgetsTableData(widgets, separators, domain, lang, twoDataCols)[source]
classmethod _setUpWidgets(form_fields, context)[source]

Setup simple display widgets.

Returns the list of widgets.

_substitute_markers(text)[source]

If text contains some marker, substitute it.

Markers are standard library string template placeholders. See https://docs.python.org/2/library/string.html and string.Template for details. Roughly, placeholders look like ${some_marker}.

Valid markers:
test_signature_img_path – path to signature image stored in
test_signature_img_path` attribute.
create_pdf(data, headerline=None, title=None, author=None, footer='', note=None, sigs_in_footer=[], topMargin=1.5, letterhead_path=None, view=None)[source]

Returns a binary data stream which is a PDF document.

classmethod fromStringList(string_list)[source]

Generate a list of reportlab paragraphs out of a list of strings.

Strings are formatted with CODE_STYLE and a spacer is appended at end.

classmethod getImage(image_path, orientation='LEFT')[source]

Get an image located at image_path as reportlab flowable.

getWidgetsTable(form_fields, context, view, lang='en', domain='waeup.kofa', separators=None, course_label=None, course_link=None, dept=None, faculty=None, colWidths=None, twoDataCols=False)[source]

Return a reportlab Table instance, created from widgets determined by form_fields and context.

  • form_fields

    is a list of schema fields as created by grok.AutoFields.

  • context

    is some object whose content is rendered here.

  • view

    is currently not used but supposed to be a view which is actually rendering a PDF document.

  • lang

    the portal language. Used for translations of strings.

  • domain

    the translation domain used for translations of strings.

  • separators

    a list of separators.

  • course_label and course_link

    if a course should be added to the table, course_label and course_link can be given, both being strings. They will be rendered in an extra-row.

  • dept and faculty

    if these are given, we render extra rows with faculty and department.

  • colWidths

    defines the the column widths of the data in the right column of base data (right to the passport image).

  • twoDataCols

    renders data widgets in a parent table with two columns.

header_logo_left_path = None
header_logo_path = None
logo_left_pos = [0, 0, 0]
logo_pos = [0, 0, 0]
pagesize = (595.275590551181, 841.8897637795275)
paint_background(canvas, doc)[source]

Paint background of a PDF, including watermark, title, etc.

The doc is expected to be some reportlab SimpleDocTemplate or similar object.

Text of headerline is extracted from doc.kofa_headtitle, the document title (under the head) from doc.kofa_title.

This is a callback method that will be called from reportlab when creating PDFs with create_pdf().

test_signature_img_path = '/kofa/demo/src/waeup/kofa/browser/static/test_signature.png'
watermark_path = None
watermark_pos = [0, 0]
waeup.kofa.browser.pdf.SIGNATURE_TABLE_STYLE = [('VALIGN', (0, -1), (-1, -1), 'TOP'), ('BOTTOMPADDING', (0, 0), (-1, 0), 36), ('TOPPADDING', (0, -1), (-1, -1), 0)]

Base style for signature tables

waeup.kofa.browser.pdf.SMALL_PARA_STYLE = <ParagraphStyle 'Small1'>

A reportlab paragraph style for smaller form output.

waeup.kofa.browser.pdf.format_html(html)[source]

Make HTML code usable for use in reportlab paragraphs.

Main things fixed here: If html code: - remove newlines (not visible in HTML but visible in PDF) - add <br> tags after <div> (as divs break lines in HTML but not in PDF) - replace <li> tags (as lists are not supported by reportlab)

The link tag must not be used in html coded text. Use a tags instead.

If not html code: - just replace newlines by <br> tags

waeup.kofa.browser.pdf.format_signatures(signatures, max_per_row=3, lang='en', single_table=False, date_field=True, date_text=u'Date', base_style=[('VALIGN', (0, -1), (-1, -1), 'TOP'), ('BOTTOMPADDING', (0, 0), (-1, 0), 36), ('TOPPADDING', (0, -1), (-1, -1), 0)])[source]
waeup.kofa.browser.pdf.get_qrcode(text, width=60.0)[source]

Get a QR Code as Reportlab Flowable (actually a Drawing).

width gives box width in pixels (I think)

waeup.kofa.browser.pdf.get_sig_tables(signatures, lang='en', max_per_row=3, horizontal=None, single_table=False, landscape=False)[source]
waeup.kofa.browser.pdf.get_signature_tables(signatures, lang='en', max_per_row=3, horizontal=None, single_table=False, landscape=False)[source]

Get a list of reportlab flowables representing signature fields.

signatures is a list of signatures. Each signature can be a simple string or a tuple of format:

(<PRE-TEXT>, <SIGNATURE>, <POST-TEXT>)

where <PRE-TEXT> and <POST-TEXT> are texts that should appear on top (PRE) or below (POST) the signature cell. Both formats, string and tuple, can be mixed. A single signature would be given as [('Pre-Text', 'Signature', 'Post-Text'),] or simply as ['Signature'] if not pre or post-text is wanted.

All texts (pre, sig, post) are rendered as paragraphs, so you can pass in also longer texts with basic HTML formatting like <b>, <i>, <br />, etc.

lang sets the language to use in I18n context. All texts are translated to the given language (en by default) if a translation is available.

max_per_row gives the maximum number of signatures to put into a single row. The default is 3. If more signatures are passed in, these signatures are put into a new row. So, for example by default 8 signatures would be spread over 3 rows.

horizontal tells how the single signature cells should be rendered: horizontal or vertical. While horizontal cells render date and signature fields side by side, in vertical cells date is rendered on top of the signature.

This parameter accepts three different values: True, False, or None. While with True each cell is rendered in horizontal mode, False will create only vertical cells.

The None value (set by default) is different: if set, the mode will be dependent on the number of signatures per row. If a row contains exactly one signature (because only one sig was passed in, or because max_per_row was set to 1), then this signature is rendered in horizontal mode. Otherwise (with more than one sig per row) each cell is rendered in vertical mode. This pseudo-smart behaviour can be switched off by setting horizontal explicitly to True or False.

single_table is a boolean defaulting to False. By default we return the rows of a signature table in several tables, one of each row. This makes it easier for reportlab to perform pagebreaks in case the page is already full, without wasting space. If the parameter is set to True, then always a list with exactly one table is returned, which will contain all rows in one table.

Generally, if a row contains only one signature, only a part of the page width is used to render this signature. If two or more signatures are passed in, the complete page width will be filled and the single signature cells will be shrinked to fit.

waeup.kofa.browser.pdf.horiz_signature_cell(signature, date_field=True, date_text=u'Date', start_row=0, start_col=0)[source]

Generate a table part containing an horizontal signature cell

Returns the table data as list of lists and an according style.

signature:
a signature tuple containing (<PRE-TEXT, SIGNATURE-TEXT, POST-TEXT>)
date_field:
boolean indicating that a ‘Date:’ text should be rendered into this signature cell (or not).
date_text:
the text to be rendered into the signature field as ‘Date:’ text.
start_row:
starting row of the signature cell inside a broader table.
start_col:
starting column of the signature cell inside a broader table.

Horizontal signature cells look like this:

+------------+---+-----------+
|Pre text possibly filling   |
|the whole box               |
+------------+---+-----------+
|            |   |           |
|            |   |           |
+------------+---+-----------+
| ---------- |   | --------- |
| Date       |   | Signature |
+------------+---+-----------+
|Post                        |
+------------+---+-----------+
waeup.kofa.browser.pdf.normalize_signature(signature_tuple)[source]

Normalize a signature tuple.

Returns a tuple (<PRE-TEXT>, <SIGNATURE>, <POST-TEXT>) from input tuple. The following rules apply:

(pre, sig, post)  --> (pre, sig, post)
(pre, sig)        --> (pre, sig, None)
(sig)             --> (None, sig, None)

Also simple strings are accepted as input:

sig               --> (None, sig, None)

If input is not a tuple nor a basestring or if the tuple contains an invalid number of elements, ValueError is raised.

waeup.kofa.browser.pdf.sig_table(signatures, lang='en', max_per_row=3, horizontal=None, single_table=False, start_row=0, landscape=False)[source]
waeup.kofa.browser.pdf.signature_row(signatures, start_row=0, horizontal=None, max_per_row=3)[source]
waeup.kofa.browser.pdf.vert_signature_cell(signature, date_field=True, date_text=u'Date:', start_row=0, start_col=0, underline=True)[source]

Generate a table part containing a vertical signature cell.

Returns the table data as list of lists and an according style.

signature:
a signature tuple containing (<PRE-TEXT, SIGNATURE-TEXT, POST-TEXT>)
date_field:
boolean indicating that a ‘Date:’ text should be rendered into this signature cell (or not).
date_text:
the text to be rendered into the signature field as ‘Date:’ text.
start_row:
starting row of the signature cell inside a broader table.
start_col:
starting column of the signature cell inside a broader table.
underline:
boolean indicating that the signature cell should provide a line on top (True by default).

Vertical signature cells look like this:

+------------+
|Pre         |
+------------+
|Date:       |
|            |
+------------+
| ---------- |
| Signature  |
+------------+
|Post        |
+------------+