My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
Templates  
sample "fill-in-the-blank" documents
Featured, Phase-Support
Updated May 27, 2011 by reingart

Introduction

Templates are predefined documents (like invoices, tax forms, etc.), where each element (text, lines, barcodes, etc.) has a fixed position (x1, y1, x2, y2), style (font, size, etc.) and a default text.

This elements can act as placeholders, so the program can change the default text "filling" the document.

Also, the elements can be defined in a CSV file or in a database, so the user can easily adapt the form to his printing needs. Template is used like a dict, setting it items values.

Details

Elements have the following properties (columns in a CSV, fields in a database):

  • name: placeholder identification
  • type: 'T': texts, 'L': lines, 'I': images, 'B': boxes, 'BC': barcodes
  • x1, y1, x2, y2: top-left, bottom-right coordinates (in mm)
  • font: ie "Arial"
  • size: text size in points, ie. 10
  • bold, italic, underline: text style (non-empty to enable)
  • foreground, background: text and fill colors, ie. 0xFFFFFF
  • align: text aling, 'L': left, 'R': right, 'C': center
  • text: default string, can be replaced at runtime
  • priority: Z-Order

Elements can be defined manually (just passing a dict), or they can be read from a CSV sheet (using parse_csv)

Example

from pyfpdf import Template

elements = [
    { 'name': 'company_logo', 'type': 'I', 'x1': 20.0, 'y1': 17.0, 'x2': 78.0, 'y2': 30.0, 'font': None, 'size': 0.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': 'logo', 'priority': 2, },
    { 'name': 'company_name', 'type': 'T', 'x1': 17.0, 'y1': 32.5, 'x2': 115.0, 'y2': 37.5, 'font': 'Arial', 'size': 12.0, 'bold': 1, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, },
    { 'name': 'box', 'type': 'B', 'x1': 15.0, 'y1': 15.0, 'x2': 185.0, 'y2': 260.0, 'font': 'Arial', 'size': 0.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': None, 'priority': 0, },
    { 'name': 'box_x', 'type': 'B', 'x1': 95.0, 'y1': 15.0, 'x2': 105.0, 'y2': 25.0, 'font': 'Arial', 'size': 0.0, 'bold': 1, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': None, 'priority': 2, },
    { 'name': 'line1', 'type': 'L', 'x1': 100.0, 'y1': 25.0, 'x2': 100.0, 'y2': 57.0, 'font': 'Arial', 'size': 0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': None, 'priority': 3, },
    { 'name': 'barcode', 'type': 'BC', 'x1': 20.0, 'y1': 246.5, 'x2': 140.0, 'y2': 254.0, 'font': 'Interleaved 2of5 NT', 'size': 0.75, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '200000000001000159053338016581200810081', 'priority': 3, },
]

f = Template(format="A4",elements=elements,
             title="Sample Invoice")
f.add_page()
f["company_name"] = "Sample Company"
f["company_logo"] = "pyfpdf/tutorial/logo.png"

f.render("./template.pdf")

See template.py or Web2Py for a complete example.

Designer

This library includes a program designer.py to visually modify the designs of a template (i.e., an invoice, report, etc.)

Input files are CSV spreadsheets describing the design (see above). Once opened, the designer displays the template with the elements as how they will be located.

The toolbar has buttons for:

  • Open, save and print (preview) template
  • Add, delete and duplicate
  • Find items by name or by text
  • Find and replace (modify selected elements, mainly move x/y)

Over an element, double left click opens a dialog to edit its text and right click opens a dialog with the properties window. You can select multiple items by holding down shift and left click them. To move the elements you can use the arrow keys or drag the elements.

To run it, just go to the directory and run:

python designer.py

(you need to have wx installed).

As an example, the following screenshot shows the Visual Designer, with the sample invoice.csv file open and 'logo' element selected, editing its properties:

Comment by wasu...@gmail.com, Feb 7, 2012

Where is parse_cvs?


Sign in to add a comment
Powered by Google Project Hosting