Navigation

  • index
  • modules |
  • next |
  • previous |
  • GenroPy v0.7dev documentation »
  • 8. batch »
  • 3. print »

3.2.2. print layout file¶

Last page update: November 23, 2011

First steps:

  • Introduction
  • File location

Features:

  • Layout page - features:

    • “letterhead” layout print
    • “grid” layout print
  • Layout print regions:

    • page
    • doc
    • grid

File creation:

Creation of the file:

  • import

  • Main class:

    • Main class webpage variables
    • Main class methods
    • Attributes explanation

Examples:

  • “grid” print layout - example
  • “letterhead” layout print - example

Library reference:

  • Library reference

3.2.2.1. Introduction¶

The print layout file allows to specify the layout of a print

  • In the File location section we describe the specific location of the print layout file
  • In the ... TODO Layout page - features, Layout print regions

Once you created the file you have to:

  • import the correct module - import section
  • create the Main class - Main class section

Inside the Main class you may customize your layout through:

  • some variables - Main class webpage variables section
  • some methods - Main class methods section

3.2.2.2. File location¶

The location of the print layout file must follow a standard path followed by a custom path:

projectName/packages/packageName/resources/tables/tableName/customPath

where:

  • projectName is the name of the project

  • packages is the packages folder

  • packageName is the name of the package

  • resources is the resources folder

  • tables is the tables folder

  • tableName is the name of the table related to the print

  • customPath is the path you choose for your print layout file through the “html_res” webpage variable of the Main class of the print settings file (there is any convention about the name you have to use)

    customPath syntax:

    anyFolderYouWant/fileNameOfThePrintSettingsFile
    

    Example:

    if you have a project called base, a package called invoice, a doctor table and in your print settings file html_res = 'html_builder/my_layout', then the path of the print layout file is:

    base/packages/invoice/resources/tables/doctor/html_builder/my_layout
    

    where “html_builder” is a folder, “my_layout” is the file name of the print layout file.

This is a graphical map of the location of the print layout file into a project:

In this image the print layout file is called “custom_file”; html_res = ‘custom_folder/custom_file’
../../_images/print_layout_file14.png

3.2.2.3. Layout page - features¶

There are two different layout for a print:

  1. the “letterhead” layout print - a print of the data of a single record

    Note

    this layout can be useful for example to print the employee cards of a company, the personal card of a doctor, and so on

  2. the “grid” layout print - a print of a set of many data taken from various database tables

    Note

    this layout can be useful for example to print the monthly sales of a company

In the next two sections we describe these two different layout modes

3.2.2.3.1. “letterhead” layout print¶

TODO

For a complete example, check the “letterhead” layout print - example example

3.2.2.3.2. “grid” layout print¶

TODO

For a complete example, check the “grid” print layout - example example

3.2.2.4. Layout print regions¶

TODO

TODO an image with the graphical differences between page - doc - grid

CLIPBOARD:

here you define the main constants:
maintable, some heights page header/footer doc header/footer grid header/footer
for page we mean the sheet itself so logo or page numbers can fill that header
or footer doc header/footer should contains the proper record info
grid are the rows (the invoice rows for example) a selection that is related to
the primary record or entity you need to print
it can have a header (tipically the name of the columns themself)
and a footer that we can use for the totals (you can put them inside the doc footer)
so these are the main areas so grid_col_widths is the standard widths for the main
grid's columns if you put a height to 0 the hook does not being called grid_col_headers
is similar to the columns of a standard table

3.2.2.4.1. page¶

TODO

3.2.2.4.2. doc¶

TODO

3.2.2.4.3. grid¶

TODO

3.2.2.5. Creation of the file¶

3.2.2.5.1. import¶

In order to use the layout functionalities you have to import in your print layout file the TableScriptToHtml class:

from gnr.web.gnrbaseclasses import TableScriptToHtml

Then we have to create the Main class:

3.2.2.5.2. Main class¶

The Main class inherits directly from the TableScriptToHtml class, so write:

class Main(TableScriptToHtml):

In the Main class you have to add some webpage variables and some methods that allow to customize the settings file:

3.2.2.5.3. Main class webpage variables¶

Note

the unit of measurement of all the numerical variables are in millimeters (e.g: print_width = 200 means 200 millimeters)

With the term webpages variables we mean that there are some defined variables belonging to the two parent classes (the BagToHtml class and the TableScriptToHtml class) of the Main class that allow you to customize your print layout.

We list here all these variables with a short description about them, specifying their parent class; if you need a more complete description, click on their name to go on their description section.

List of the webpage variables of the BagToHtml class:

List of the webpage variables of the TableScriptToHtml class:

3.2.2.6. Main class methods¶

In this section we introduce most of the important layout hook methods that allow you to personalize the print. For more information, click on the relative links

They are:

  • mainLayout(): MANDATORY - it gives the page object through which you create the print
  • gridLayout(): if you have a grid then this method is mandatory - it gives the grid object through which you create the grid layout
  • defineStandardStyles(): TODO
  • prepareRow(): TODO
  • onRecordLoaded(): TODO

Warning

the following four methods can be used if and only if there is a webpage variable defined with a different value with respect to 0. For those methods we point up the related webpage variable

  • docHeader() define the header of the doc. To use it give a different value to the doc_header_height webpage variable with respect to 0
  • docFooter(): define the footer of the doc. To use it give a different value to the doc_footer_height webpage variable with respect to 0
  • pageHeader(): define the header of the page. To use it give a different value to the page_header_height webpage variable with respect to 0
  • pageFooter(): define the footer of the page. To use it give a different value to the page_header_height webpage variable with respect to 0

Inside the Main class methods, you can create the layout through the following four methods:

  • the layout(): allow to return a layout element
  • the row(): allow to return a row element
  • the cell(): allow to return a cell element
  • the rowCell(): allow to return a rowCell

3.2.2.6.1. prepareRow()¶

prepareRow(self, row)¶

This method allow to define all the rows of the grid

Note

to select data from a database table, you have to use the rowCell(), not the normal cell():

def prepareRow(self, row):
    style_cell = 'text-indent:2mm;border-bottom-style:dotted;'
    self.rowCell('data', style=style_cell)

3.2.2.7. Attributes explanation¶

3.2.2.7.1. lastPage¶

The lastPage attribute belongs to the pageFooter() and the docFooter() methods

  1. usage of lastPage in the docFooter() method:

    In some cases you need that the docFooter is used only in the last page (for example, when you print an extract of the monthly doctor invoices and you want in the last page the total sum of doctor’s operations)

    To use the docFooter() in this way, write at the beginning of the method these two lines:

    if not lastPage:
        return
    

    lastPage is automatically passes as True when the print batch is going to create the last page

  2. usage of lastPage in the pageFooter() method:

    If you need to modify the footer of the page, you can use the pageFooter method. If you need to create a different pageFooter in the last page, you can use the lastPage attribute.

    Just write at the beginning of the method these two lines:

    if not lastPage:
        return
    

    lastPage is automatically passes as True when the print batch is going to create the last page

3.2.2.8. Examples¶

3.2.2.9. “grid” print layout - example¶

Let’s see an example page of a “grid” layout print; read the comments that explain the code

TODO

3.2.2.10. “letterhead” layout print - example¶

Let’s see an example page of a “letterhead” layout print; read the comments that explain the code

TODO translate the code and add comments!

#!/usr/bin/env pythonw
# -*- coding: UTF-8 -*-

from gnr.web.gnrbaseclasses import TableScriptToHtml
from gnr.core.gnrbag import Bag

class Main(TableScriptToHtml):
    maintable = 'polimed.medico'              #
    rows_table = 'polimed.prestazione'
    doc_header_height = 10                    # docHeader is the callback
    doc_footer_height = 10                    # docFooter is the callback
    grid_header_height = 6.2                  # bodyHeaderHeight
    grid_col_widths = [17,12,0,0,20,15,15,20] # rowColWidth
    grid_col_headers = 'Date,Hour,Patient,Performance,Convention,Amount,Cost,Invoice'
    grid_row_height = 5.3
    row_mode = 'attribute'
    rows_path = 'rows'

    def mainLayout(self, page):
        style = """font-family:"Lucida Grande", Lucida, Verdana, sans-serif;
                   text-align:left;
                   line-height:5mm;
                   font-size:9pt;"""
        return page.layout(width=190,
                           height=self.page_height,
                           um='mm',top=0,
                           left=5,border_width=0,
                           lbl_height=4,lbl_class='caption',
                           style=style)

    def docHeader(self, header):
        layout = header.layout(um='mm',
                               lbl_class='smallCaption',
                               top=1,bottom=1,left=1,right=1,
                               lbl_height=3,
                               border_width=.3,
                               border_color='gray',
                               style='line-height:6mm;text-align:left;text-indent:2mm;')
        row=layout.row(height=10)
        row.cell("%s %s" % (self.field('@anagrafica_id.cognome'), self.field('@anagrafica_id.nome')),
                 lbl='Performances of')
        row.cell(self.toText(self.getData('period.from')), lbl='From',width=30,content_class='aligned_right')
        row.cell(self.toText(self.getData('period.to')), lbl='To', width=30,content_class='aligned_right')
        row.cell(self.pageCounter(mask='%s of %s'), lbl='Page', width=12, content_class='aligned_right')

    def docFooter(self, footer, lastPage=None):
        if not lastPage:
            return
        layout = footer.layout(name='footerL',um='mm',border_color='gray',
                               lbl_class='smallCaption',
                               top=1,bottom=1,left=80,right=1,
                               lbl_height=3,border_width=0.3,
                               content_class='aligned_right')
        row=layout.row(height=0)
        totals_dict = {}
        totals_dict['importo'],totals_dict['costo'] = self.getData('rows').sum('#a.importo,#a.costo')
        row.cell(self.toText(totals_dict['importo'],format=self.currencyFormat), lbl='Total amount')
        row.cell(self.toText(totals_dict['costo'],format=self.currencyFormat), lbl='Total cost')

    def gridLayout(self, grid):
        return grid.layout(name='rowsL',um='mm',border_color='gray',
                           top=1,bottom=1,left=1,right=1,
                           border_width=0.3,lbl_class='caption',
                           style='line-height:5mm;text-align:left;font-size:7.5pt')

    def prepareRow(self, row):
        style_cell = 'text-indent:2mm;border-bottom-style:dotted;'
        self.rowCell('data', style=style_cell)
        self.rowCell('ora', format='HH:mm', style=style_cell)
        self.rowCell('paziente', style=style_cell)
        self.rowCell('prestazione', style=style_cell)
        self.rowCell('convenzione_codice', style=style_cell)
        self.rowCell('importo', format=self.currencyFormat, style=style_cell,content_class='aligned_right')
        self.rowCell('costo', format=self.currencyFormat, style=style_cell,content_class='aligned_right')
        self.rowCell('fattura', style=style_cell, content_class='aligned_right')

    def onRecordLoaded(self):
        where = '$date >= :begin_date AND $date <= :end_date AND doctor_id=:d_id'
        columns ='''$doctor,$date,$hour,$patient,$performance,
                    @convention_id.code AS convention_code,
                    $amount,$cost,@invoice_id.number AS invoice'''
        query = self.db.table(self.rows_table).query(columns=columns, where=where,
                                                     begin_data = self.getData('period.from'),
                                                     end_data = self.getData('period.to'),
                                                     d_id=self.record['id'])
        selection = query.selection()
        if not selection:
            return False
        self.setData('rows',selection.output('grid'))"""

3.2.2.11. clipboard¶

Note

my clipboard...

--Layout, righe e celle--

Per posizionare le cose, abbiamo a disposizione tre oggetti:

    * **layout**. Possono contenere soltanto righe.
    * **row**. Possono contenere soltanto celle. Le righe hanno l'altezza, se non viene
    specificata (o se è zero) la riga è elastica.
    * **celle**. Possono contenere UN SOLO layout. Le celle hanno la larghezza.
    Due celle attaccate autocollassano i bordi (rimane un bordo solo).

--Attributi e callbacks--

Il foglio è diviso in varie parti che hanno corrispondenti callbacks:

(attributo, callback)
attributo page_header, callback pageHeader -- header della pagina (es. per carta intestata)
page_footer, callback pageFooter -- footer della pagina (es. per carta intestata)
callback docHeader -- intestazione del documento
callback docFooter -- footer del documento
callback prepareRow -- chiamato per ogni riga del corpo

Il ``pageHeader``/``pageFooter`` è solitamente riservato alla carta intestata o al template,
``docHeader``/``docFooter`` viene usato per la testata/footer. Ad esempio, in una stampa fattura,
l'intestazione va nel ``docHeader`` mentre le righe nel corpo.

``prepareRow`` viene chiamata in automatico per ogni riga. Ha una sintassi tipo field.

Il componente prende i dati da una tabella, ma se invece si vogliono passare dati con
un altro sistema si può ridefinire il metodo ``loadRecord``

3.2.2.12. Library reference¶

For the complete library reference, check:

  • the TableScriptToHtml class
  • the BagToHtml class

Logo

Table Of Contents

  • 3.2.2. print layout file
    • 3.2.2.1. Introduction
    • 3.2.2.2. File location
    • 3.2.2.3. Layout page - features
      • 3.2.2.3.1. “letterhead” layout print
      • 3.2.2.3.2. “grid” layout print
    • 3.2.2.4. Layout print regions
      • 3.2.2.4.1. page
      • 3.2.2.4.2. doc
      • 3.2.2.4.3. grid
    • 3.2.2.5. Creation of the file
      • 3.2.2.5.1. import
      • 3.2.2.5.2. Main class
      • 3.2.2.5.3. Main class webpage variables
    • 3.2.2.6. Main class methods
      • 3.2.2.6.1. prepareRow()
    • 3.2.2.7. Attributes explanation
      • 3.2.2.7.1. lastPage
    • 3.2.2.8. Examples
    • 3.2.2.9. “grid” print layout - example
    • 3.2.2.10. “letterhead” layout print - example
    • 3.2.2.11. clipboard
    • 3.2.2.12. Library reference

Previous topic

3.2.1. print settings file

Next topic

3.2.3. html template

This Page

  • Show Source

Quick search

Enter search terms or a module, class or function name.

Navigation

  • index
  • modules |
  • next |
  • previous |
  • GenroPy v0.7dev documentation »
  • 8. batch »
  • 3. print »
© Copyright 2011, The GenroPy Team. Last updated on Nov 23, 2011. Created using Sphinx 1.0.7.