pdfme.utils

pdfme.utils.subs(string, *args, **kwargs)

Function to take string, format it using args and kwargs and encode it into bytes.

Parameters:

string (str) – string to be transformed.

Returns:

the resulting bytes.

Return type:

bytes

pdfme.utils.process_style(style, pdf=None)

Function to use a named style from the PDF instance passed, if style is a string or style itself if this is a dict.

Parameters:
  • style (str, dict) – a style name (str) or a style dict.

  • pdf (PDF, optional) – the PDF to extract the named style from.

Returns:

a style dict.

Return type:

dict

pdfme.utils.get_page_size(size)

Function to get tuple with the width and height of a page, from the value in size.

If size is a str, it should be the name of a page size: a5, a4, a3, b5, b4, jis-b5, jis-b4, letter, legal and ledger.

If size is a int, the page will be a square of size (int, int).

If size is a list or tuple, it will be converted to a tuple.

Parameters:

size (int, float, str, iterable) – the page size.

Returns:

tuple with the page width and height.

Return type:

tuple

pdfme.utils.parse_margin(margin)

Function to transform margin into a dict containing keys top, left, bottom and right with the margins.

If margin is a dict, it is returned as it is.

If margin is a string, it will be splitted using commas or spaces, and each substring will be converted into a number, and after this, the list obtained will have the same treatment of an iterable.

If margin is an iterable of 1 element, its value will be the margin for the four sides. If it has 2 elements, the first one will be the top and bottom margin, and the second one will be the left and right margin. If it has 3 elements, these will be the top, right and bottom margins, and the left margin will be the second number (the same as right). If it has 4 elements, they will be the top, right, bottom and left margins respectively.

Parameters:

margin (str, int, float, tuple, list, dict) – the margin element.

Returns:

dict containing keys top, left, bottom and right with the margins.

Return type:

dict

pdfme.utils.parse_style_str(style_str, fonts)

Function to parse a style string into a style dict.

It parses a string with a semi-colon separeted list of the style attributes you want to apply (for a list of the attributes you can use in this string see pdfme.text.PDFText). For the ones that are of type bool, you just have to include the name and it will mean they are True, and for the rest you need to include the name, a colon, and the value of the attribute. In case the value is a color, it can be any of the possible string inputs to function pdfme.color.parse_color(). Empty values mean None, and "1" == True and "0" == False for bool attributes.

This is an example of a valid style string:

".b;s:10;c:1;u:0;bg:"
Parameters:
  • style_str (str) – The string representing the text style.

  • fonts (PDFFonts) – If a font family is included, this is needed to check if it is among the fonts already added to the PDFFonts instance passed.

Raises:

ValueError – If the string format is not valid.

Returns:

A style dict like the one described in pdfme.text.PDFText.

Return type:

dict

pdfme.utils.create_graphics(graphics)

Function to transform a list of graphics dicts (with lines and fill rectangles) into a PDF stream, ready to be added to a PDF page stream.

Parameters:

graphics (list) – list of graphics dicts.

Returns:

a PDF stream containing the passed graphics.

Return type:

str

pdfme.utils.to_roman(n)

Function to transform n integer into a string with its corresponding Roman representation.

Parameters:

n (int) – the number to be transformed.

Returns:

the Roman representation of the integer passed.

Return type:

str

pdfme.utils.get_paragraph_stream(x, y, text_stream, graphics_stream)

Function to create a paragraph stream, in position x and y, using stream information in text_stream and graphics_stream.

Parameters:
  • x (int, float) – the x coordinate of the paragraph.

  • y (int, float) – the y coordinate of the paragraph.

  • text_stream (str) – the text stream of the paragraph.

  • graphics_stream (str) – the graphics stream of the paragraph.

Returns:

the whole stream of the paragraph.

Return type:

str

pdfme.utils.copy(obj)

Function to copy objects like the ones used in this project: dicts, lists, PDFText, PDFTable, PDFContent, etc.

Parameters:

obj (Any) – the object to be copied.

Returns:

the copy of the object passed as argument.

Return type:

Any

class pdfme.utils.MuiltiRange

Bases: object

add(*range_args)
pdfme.utils.parse_range_string(range_str)

Function to convert a string of comma-separated integers and integer ranges into a set of all the integers included in those.

Parameters:

range_str (str) – comma-separated list of integers and integer ranges.

Returns:

a set of integers.

Return type:

MuiltiRange

pdfme.utils.as_hex(text)

Takes a text and returns it in UTF-16 Big Endian representation.

Parameters:

text (str) – Input text.

Returns:

The transformed text.

Return type:

str

pdfme.utils.escape_string(text)

Takes in a text and returns all special characters escaped.

Parameters:

text (str) – Input text.

Returns:

Escaped text.

Return type:

str