fpdf2 - Basics (original) (raw)

New in 2.7.6

Text Flow Regions

Text regions are a hierarchy of classes that enable to flow text within a given outline. In the simplest case, it is just the running text column of a page. But it can also be a sequence of outlines, such as several parallel columns or the cells of a table. Other outlines may be combined by addition or subtraction to create more complex shapes.

There are two general categories of regions. One defines boundaries for running text that will just continue in the same manner one the next page. Those include columns and tables. The second category are distinct shapes. Examples would be a circle, a rectangle, a polygon of individual shape or even an image. They may be used individually, in combination, or to modify the outline of a multipage column. Shape regions will typically not cause a page break when they are full. In the future, a possibility to chain them may be implemented, so that a new shape will continue with the text that didn't fit into the previous one.

The currently implemented text regions are:

Other types like Table cells, shaped regions and combinations are still in the design phase, see Quo vadis, .write()?.

General Operation

Using the different region types and combination always follows the same pattern. The main difference to the normal FPDF.write() method is that all added text will first be buffered, and only gets rendered on the page when the context of the region is closed. This is necessary so that text can be aligned within the given boundaries even if its font, style, or size are arbitrarily varied along the way.

The graphic shows the relationship of page, text areas and paragraphs (with varying alignment) for the example of a two-column layout.

Text Start Position

When rendering, the vertical start position of the text will be at the lowest one out of:

The horizontal start position will be either at the current x position, if that lies within the boundaries of the region/column, or at the left edge of the region. In both horizontal and vertical positioning, regions with multiple columns may follow additional rules and restrictions.

Interaction Between Regions

Several region instances can exist at the same time. But only one of them can act as context manager at any given time. It is not currently possible to activate them recursively. But it is possible to use them intermittingly. This will probably most often make sense between a columnar region and a table or a graphic. You may have some running text ending at a given height, then insert a table/graphic, and finally continue the running text at the new height below the table within the existing column(s).

Common Parameters

All types of text regions have the following constructor parameters in common:

All of those values can be overridden for each individual paragraph.

Common Methods

Paragraphs

The primary purpose of paragraphs is to enable variations in horizontal text alignment, while the horizontal extents of the text are managed by the text region. To set the alignment, you can use the align argument when creating the paragraph. Valid values are defined in the Align enum.

For more typographical control, you can use the following arguments. Most of those override the settings of the current region when set, and default to the value set there.

Other than text regions, paragraphs should always be used as context managers and never be reused. Violating those rules may result in the entered text turning up on the page out of sequence.

Possible Future Extensions

Those features are currently not supported, but Pull Requests are welcome to implement them:

Images

New in 2.7.7

Most arguments for inserting images into text regions are the same as for the FPDF.image() method, and have the same or equivalent meaning.

Since the image will be placed automatically, the x and y parameters are not available. The positioning can be controlled with align, where the default is LEFT, with the alternatives RIGHT and CENTER.

If neither width nor height are specified, the image will be inserted with the size resulting from the PDF default resolution of 72 dpi. If the fill_width parameter is set to True, it increases the size to fill the full column width if necessary. If the image is wider than the column width, it will always be reduced in size proportionally.

The top_margin and bottom_margin parameters have the same effect as with text paragraphs.