There are several ways in fpdf to add text to a PDF document, each of which comes with its own special features and its own set of advantages and disadvantages. You will need to pick the right one for your specific task.
|method||lines||markdown support||HTML support||accepts new current position||details|
||one||no||no||fixed||Inserts a single-line text string with a precise location on the base line of the font.|
||one||yes||no||yes||Inserts a single-line text string within the boundaries of a given box, optionally with background and border.|
||several||yes||no||yes||Inserts a multi-line text string within the boundaries of a given box, optionally with background and border.|
||several||no||no||auto||Inserts a multi-line text string within the boundaries of the page margins, starting at the current x/y location (typically the end of the last inserted text).|
||several||no||yes||auto||From html.py. An extension to
There are a few advanced typesetting features that fpdf doesn't currently support.
- Automatic ligatures - Some writing systems (eg. most hindic scripts such as Devaganari, Hangul) frequently combine a number of written characters into a single glyph. This would require advanced font analysis capabilities, which aren't currently implemented.
- Contextual forms - In some writing systems (eg. Arabic, Hebrew, Mongolian, etc.), characters may take a different shape, depending on whether they appear at the beginning, in the middle, or at the end of a word, or isolated. Fpdf will always use the same standard shape in those cases.
- Vertical writing - Some writing systems are meant to be written vertically. Doing so is not directly supported. In cases where this just means to stack characters on top of each other (eg. Chinese, Japanese, etc.), client software can implement this by placing each character individuall at the correct location. In cases where the characters are connected with each other (eg. Mongolian), this may be more difficult, if possible at all.
For all text insertion methods, the relevant font related properties (eg. font/style and foreground/background color) must be set before invoking them. This includes using:
.set_draw_color()- for cell borders
.set_fill_color()- for the background
Change in current position
.multi_cell() let you specify where the current position (
.y) should go after the call.
This is handled by the parameters
Their values must one of the following enums values or an equivalent string:
Prints a single-line character string. In contrast to the other text methods,
the position is given explicitly, and not taken from
.y. The origin is
on the left of the first character, on the baseline. This method allows placing
a string with typographical precision on the page, but it is usually easier to
Prints a cell (rectangular area) with optional borders, background color and
character string. The upper-left corner of the cell corresponds to the current
position. The text can be aligned or centered. After the call, the current
position moves to the selected
new_y position. It is possible to put a link on the text.
markdown=True, then minimal markdown
styling is enabled, to render parts of the text in bold, italics, and/or
If automatic page breaking is enabled and the cell goes beyond the limit, a page break is performed before outputting.
Allows printing text with line breaks. Those can be automatic (breaking at the
most recent space or soft-hyphen character) as soon as the text reaches the
right border of the cell, or explicit (via the
As many cells as necessary are stacked, one below the other.
Text can be aligned, centered or justified. The cell block can be framed and
the background painted.
new_x="RIGHT", new_y="TOP", maximum height=pdf.font_size can be
useful to build tables with multiline text in cells.
In normal operation, returns a boolean indicating if page break was triggered.
split_only == True, returns
txt split into lines in an array (with any markdown markup removed).
Prints multi-line text between the page margins, starting from the current position. When the right margin is reached, a line break occurs at the most recent space or soft-hyphen character, and text continues from the left margin. A manual break happens any time the \n character is met, Upon method exit, the current position is left near the end of the text, ready for the next call to continue without a gap, potentially with a different font or size set. Returns a boolean indicating if page break was triggered.
The primary purpose of this method is to print continuously wrapping text, where different parts may be rendered in different fonts or font sizes. This contrasts eg. with
.multi_cell(), where a change in font family or size can only become effective on a new line.
This method can be accessed by using the class
HTMLMixin from "html.py". It is very similar to
.write(), but accepts basic HTML formatted text as input. See html.py for more details and the supported HTML tags.
Note that when using data from actual web pages, the result may not look exactly as expected, because
.write_html() prints all whitespace unchanged as it finds them, while webbrowsers rather collapse each run of consequitive whitespace into a single space character.