pdfme.fonts

pdfme.fonts.get_tounicode_cmap(cmap_mappings)

Method returns cmap for Unicode mappings.

class pdfme.fonts.PDFFont(ref)

Bases: ABC

Abstract class that represents a PDF font.

Parameters:

ref (str) – the name of the font, included in every paragraph and page that uses this font.

registered_fonts = {}
loaded_fonts = []
property ref

Property that returns the name (ref) of this font.

Returns:

the name of this font

Return type:

str

static register(font_family, mode, path)

Method for registering user defined custom fonts. The registered font would be loaded when required in the document.

Parameters:
  • font_family – The font family name.

  • mode – The font mode. One of n, b, i, bi.

  • path – Font file path.

abstract property base_font

Abstract property that should return this font’s base font name.

Returns:

the base font name

Return type:

str

abstractmethod get_char_width(char)

Abstract method that should return the width of char character in this font.

Parameters:

char (str) – the character.

Returns:

the character’s width.

Return type:

float

abstractmethod get_text_width(text)

Abstract method that should return the width of the text string in this font.

Parameters:

text (str) – the sentence to measure.

Returns:

the sentence’s width.

Return type:

float

abstractmethod add_font(base)

Abstract method that should add this font to the PDFBase instance, passed as argument.

Parameters:

base (PDFBase) – the base instance to add this font.

class pdfme.fonts.PDFStandardFont(ref, base_font, widths)

Bases: PDFFont

This class represents a standard PDF font.

Parameters:
  • ref (str) – the name of this font.

  • base_font (str) – the base font name of this font.

  • widths (dict) – the widths of each character in this font.

property base_font

See pdfme.fonts.PDFFont.base_font()

get_char_width(char)

See pdfme.fonts.PDFFont.get_char_width()

get_text_width(text)

See pdfme.fonts.PDFFont.get_text_width()

add_font(base)

See pdfme.fonts.PDFFont.add_font()

class pdfme.fonts.PDFTrueTypeFont(ref, filename=None)

Bases: PDFFont

This class represents a TrueType PDF font.

Parameters:
  • ref (str) – the name of this font.

  • base_font (str) – the base font name of this font.

  • widths (dict) – the widths of each character in this font.

property base_font

See pdfme.fonts.PDFFont.base_font()

get_char_width(char)

See pdfme.fonts.PDFFont.get_char_width()

get_text_width(text)

See pdfme.fonts.PDFFont.get_text_width()

load_font(filename)

Method to extract information needed by the PDF document about this font, from font file in filename path.

Parameters:

filename (str) – font file path.

Raises:

ImportError – if fonttools library is not installed.

add_font(base)

See pdfme.fonts.PDFFont.add_font()

encode_text(text)
compute_char_widths()
class pdfme.fonts.PDFFonts

Bases: object

Class that represents the set of all the fonts added to a PDF document.

get_font(font_family, mode)

Method to get a font from its font_family and mode.

Parameters:
  • font_family (str) – the name of the font family

  • mode (str) – the mode of the font you want to get. n, b, i or bi.

Returns:

an object that represents a PDF font.

Return type:

PDFFont

load_font(path, font_family, mode='n')

Method to add a TrueType font to this instance.

Parameters:
  • path (str) – the location of the font file.

  • font_family (str) – the name of the font family

  • mode (str, optional) – the mode of the font you want to get. n, b, i or bi.