Add text and image to MultiColumnLayout in borb library

89 Views Asked by At

I am new to this library. I want to create "newspaper lookalike" page with this library.

I want to add headline, lead, text and image to the pdf file. Text is very long so i want to use MultiColumnLayout. I also want to set up the margins and spacing between the columns. Are there any explanations, code snippets for this? When every I copy/paste the code that I find on the internet, I get some kind of error, like code is updated.

I have this code, but I do not have a clue how to add text, margins, column spacing or even image:

from borb.pdf import Document
from borb.pdf import Page
from borb.pdf import PageLayout
from borb.pdf import MultiColumnLayout

# create an empty PDF
Document = Document()

# create an empty Page
Page = Page()
doc.add_page(page)

# set a PageLayout
PageLayout = MultiColumnLayout(page, 
                               column_widths=[w - Decimal(100)], 
                               margin_top=Decimal(50), 
                               margin_right=Decimal(50),
                               margin_bottom=Decimal(50),
                               margin_left=Decimal(50),
                                )
1

There are 1 best solutions below

0
Joris Schellekens On

disclaimer: I am the author of borb

Let's go over your code

The imports are fine for now. You will need more imports if you want to add content, but we'll get to that.

from borb.pdf import Document
from borb.pdf import Page
from borb.pdf import PageLayout
from borb.pdf import MultiColumnLayout

Next we create an empty Document. The Document class represents a PDF. It would be better if you did not name your variables after classes.

# create an empty PDF
doc = Document()

One thing I would also do is to add type hints. But that's completely optional.

Next we add an empty Page to the Document we made. Again, don't name your variables after classes.

# create an empty Page
page = Page()
doc.add_page(page)

Next we use a PageLayout. MultiColumnLayout is an instance of PageLayout that will split a Page into several columns, and flow content from one column to the next when needed.

Again, do not name variables after existing classes.

# determine the width of a single column
column_width = (Decimal(595) - Decimal(100)) // Decimal(2)

# determine the margin between columns
inter_column_margin: Decimal = Decimal(5)
column_width -= inter_column_margin

# set a PageLayout
layout = MultiColumnLayout(page,
                           column_widths=[column_width, column_width],
                           inter_column_margins=[inter_column_margin],
                           margin_top=Decimal(50),
                           margin_right=Decimal(50),
                           margin_bottom=Decimal(50),
                           margin_left=Decimal(50),
                          )

Now we can add some content!

from borb.pdf import Paragraph
from borb.pdf import Lipsum

for _ in range(0, 20):
    layout.add(Paragraph(Lipsum.generate_lipsum_text(5)))

And finally we can store the Document

with open("output.pdf", "wb") as in_file_handle:
    PDF.dumps(in_file_handle, doc)

Or, the complete example in 1 snippet:

from borb.pdf import Document
from borb.pdf import Page
from borb.pdf import PageLayout
from borb.pdf import MultiColumnLayout
from borb.pdf import Paragraph

# create an empty PDF
doc = Document()

# create an empty Page
page = Page()
doc.add_page(page)

# determine the width of a single column
column_width = (Decimal(595) - Decimal(100)) // Decimal(2)

# determine the margin between columns
inter_column_margin: Decimal = Decimal(5)
column_width -= inter_column_margin

# set a PageLayout
layout = MultiColumnLayout(
    page,
    column_widths=[column_width, column_width],
    inter_column_margins=[inter_column_margin],
    margin_top=Decimal(50),
    margin_right=Decimal(50),
    margin_bottom=Decimal(50),
    margin_left=Decimal(50),
)

# add content
for _ in range(0, 20):
    layout.add(Paragraph(Lipsum.generate_lipsum_text(5)))


# store
with open("output.pdf", "wb") as in_file_handle:
    PDF.dumps(in_file_handle, doc)

which yields the following PDF:

enter image description here

If you need more examples, check out the examples repository.