Create documents with dynamic height with borb

547 views Asked by At

I think borb is pretty amazing for generating PDFs with Python.

I'd love to use borb to create documents to print on a thermal printer (fixed width, endless height of document). The documents have messages of different length thus the height of the document would need to be adjusted every time (so no blank space gets printed after the message).

Is there a way to determine the size/height of the content and to resize the page accordingly in borb?

1

There are 1 answers

0
Joris Schellekens On

disclaimer: I am the author of borb.

The PDF specification explicitly states a maximum page width/height. So from that point of view, you will not be able to go above those limits.

Now to answer your other question, you can determine the width and height of a piece of content in borb by adding it to a (dummy) Page.

def get_layout_box(p: Paragraph) -> Rectangle:
    pg: Page = Page()
    ZERO: Decimal = Decimal(0)
    W: Decimal = Decimal(1000)   # max width you would allow
    H: Decimal = Decimal(1000)   # max height you would allow
    return p.layout(pg, Rectangle(ZERO, ZERO, W, H))