API reference

Main classes

class pdfino.Template

A template that can be used to generate a PDF file.

property default_font: Font | None

Return the default font.

property stylesheet: Dict[str, ParagraphStyle | ListStyle | LineStyle]

Return the indexed stylesheet.

class pdfino.Document(*, template: Template | None = None)

A PDF document.

template_class

alias of Template

add(reportlab_element: Flowable) None

Add a ReportLab Flowable directly to the document elements.

Parameters:

reportlab_element – The flowable to add.

add_image() None

Add an image to the document elements.

add_list(items: List[str], *, bullet_type: Literal['1', 'a', 'A', 'i', 'I'] | Literal['circle', 'square', 'blackstar', 'sparkle', 'diamond'], style: str, options: ElementOptions | None = None, item_options: ElementOptions | None = None) None

Add a list to the document elements.

Parameters:
  • items – The list of items to add.

  • bullet_type – The type of bullet to use for the list.

  • style – The style to use for the list.

  • options – The options to use for the list.

  • item_options – The options to use for the list items.

add_page_break() None

Add a page break to the document elements.

add_paragraph(text: str, *, style: str, options: ElementOptions | None = None) None

Add a paragraph to the document elements.

Parameters:
  • text – The text to add.

  • style – The style to use for the paragraph.

  • options – The options to use for the paragraph.

add_separator(height: int = 1, *, options: ElementOptions | None = None) None

Add a line separator to the document elements.

color and margin options are used for the line. The line is added as a Drawing element, and the position of the line is determined by the margin options.

Parameters:
  • height – The height of the line.

  • options – The options to use for the line.

add_spacer(height: int = 1) None

Add a spacer to the document elements.

Parameters:

height – The height of the spacer.

add_table() None

Add a table to the document elements.

h1(text: str, *, options: ElementOptions | None = None) None

Add a h1 to the document elements.

Parameters:
  • text – The text to add.

  • options – The options to use for the header.

h2(text: str, *, options: ElementOptions | None = None) None

Add a h2 to the document elements.

Parameters:
  • text – The text to add.

  • options – The options to use for the header.

h3(text: str, *, options: ElementOptions | None = None) None

Add a h3 to the document elements.

Parameters:
  • text – The text to add.

  • options – The options to use for the header.

h4(text: str, *, options: ElementOptions | None = None) None

Add a h4 to the document elements.

Parameters:
  • text – The text to add.

  • options – The options to use for the header.

p(text: str, *, options: ElementOptions | None = None) None

Add a paragraph to the document elements.

Parameters:
  • text – The text to add.

  • options – The options to use for the paragraph.

br() None

Add a line break to the document elements.

hr(*, height: int = 1, options: ElementOptions | None = None) None

Add a horizontal line to the document elements.

Parameters:
  • height – The height of the line.

  • options – The options to use for the line.

ol(items: List[str], *, bullet_type: Literal['1', 'a', 'A', 'i', 'I'] = '1', options: ElementOptions | None = None, item_options: ElementOptions | None = None) None

Add an ordered list to the document elements.

ul(items: List[str], *, bullet_type: Literal['circle', 'square', 'blackstar', 'sparkle', 'diamond'] = 'circle', options: ElementOptions | None = None, item_options: ElementOptions | None = None) None

Add an unordered list to the document elements.

property actual_width: float

Return the actual width of the document.

property bytes: bytes

Return the bytes data of the document.

property stylesheet: Dict[str, ParagraphStyle | ListStyle | LineStyle]

Return the indexed stylesheet.

save_as(file_path: Path | str) None

Save the document to a file.

Parameters:

file_path – The path to the file to save the document to.

Custom types

class pdfino.Font(name: str, normal: Path, bold: Path | None = None, italic: Path | None = None, bold_italic: Path | None = None, default: bool = False)

Bases: NamedTuple

NamedTuple representing a font definition with different variants.

Parameters:
  • name – Name of the font.

  • normal – Path to the normal variant of the font.

  • bold – Path to the bold variant of the font.

  • italic – Path to the italic variant of the font.

  • bold_italic – Path to the bold italic variant of the font.

  • default – Whether this font should be used as the default font for a Template.

class pdfino.Margins(top: int, right: int, bottom: int, left: int)

Bases: NamedTuple

NamedTuple representing margins.

Parameters:
  • top – Top margin.

  • right – Right margin.

  • bottom – Bottom margin.

  • left – Left margin.

class pdfino.Pagesize(width: float, height: float)

NamedTuple representing a page size in points, for a resolution of 72 dpi.

Parameters:
  • width – Width of the page.

  • height – Height of the page.

classmethod from_name(pagesize: str) Pagesize

Alternative constructor to create a Pagesize from a string, e.g. “A4”.

Parameters:

pagesize – ReportLab pagesize.

Returns:

Pagesize named tuple.

Raises:

ValueError – If the pagesize is not a valid constant of reportlab.lib.pagesizes.

class pdfino.Style(name: str, parent: str | None = None, options: ElementOptions | None = None, font_name: str | None = None, font_size: int | float | None = None, line_height: int | float | None = None)

Bases: NamedTuple

NamedTuple representing a style definition.

Parameters:
  • name – Name of the style.

  • parent – Parent style to inherit from (name of another style)

  • options – Options for the style.

  • font_name – Name of the font to use.

  • font_size – Size of the font.

  • line_height – Line height of the style, relative to the font size.

class pdfino.type_definitions.LayoutOptions

Bases: TypedDict

Layout options for a page.

margin_bottom: int
margin_left: int
margin_right: int
margin_top: int
margins: Margins
class pdfino.type_definitions.StyleOptions

Bases: TypedDict

Styling options for an element.

color: str
class pdfino.type_definitions.ElementOptions

Bases: LayoutOptions, StyleOptions

All options for an element.

align: Literal['left', 'right', 'center', 'justify']
color: str
margin_bottom: int
margin_left: int
margin_right: int
margin_top: int
margins: Margins

Integrations

class pdfino.django.PdfResponse(content: bytes, *, filename: str, as_attachment: bool = False, **kwargs)

A Django HttpResponse that returns PDF content.