Open In App

Creating Ebooks with borb in Python

Last Updated : 06 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Creating ebooks can be a rewarding endeavor, whether for sharing knowledge, storytelling, or distributing documentation. The borb library in Python is a powerful tool that enables developers to generate PDFs with rich formatting and various multimedia elements. This article will guide you through the process of creating a well-formatted ebook using the borb library, covering the basics and demonstrating how to incorporate different types of content.

Setting Up the Environment

Before you start, ensure you have Python installed. You can install the borb library using pip:

pip install borb

Step to Creating Ebooks with Borb

Let's create an ebook that includes text, images, barcodes, charts, maps, and lists. We'll also style the content and add custom fonts.

Importing Necessary Modules

Here, we import various classes and functions from the borb library that we'll use to create and manipulate the PDF.

Python
from decimal import Decimal
from borb.pdf import Document, Page, Paragraph, PDF, SingleColumnLayout, HexColor, Alignment
from borb.pdf.canvas.font.simple_font.true_type_font import TrueTypeFont
from borb.pdf import FixedColumnWidthTable, Image
from borb.pdf.canvas.layout.image.barcode import Barcode, BarcodeType
from borb.pdf import Chart
from borb.pdf import MapOfTheWorld
from borb.pdf import InlineFlow, ChunkOfText, Emojis
from borb.pdf import ProgressBar
from borb.pdf import UnorderedList

Creating the Document and Page:

We start by creating a Document object and then a Page object with specific dimensions. The page is added to the document.

Python
pdf = Document()
page = Page(width=Decimal(1400), height=Decimal(800))
pdf.add_page(page)

Setting Up the Layout:

A SingleColumnLayout is created for the page to manage the placement of elements.

Python
layout = SingleColumnLayout(page)

Adding a Title:

We add a centered paragraph as a title to the layout.

Python
layout.add(Paragraph("Table", font_size=20, horizontal_alignment=Alignment.CENTERED))

Defining a Table:

A FixedColumnWidthTable is defined with 3 columns and 3 rows, along with custom border properties.

Python
table = FixedColumnWidthTable(number_of_columns=3, number_of_rows=3, border_top=True,
        border_right=True,
        border_bottom=True,
        border_left=True,
        border_color=HexColor("#00ff00"),
        border_radius_bottom_left=Decimal(0.1),
        border_radius_bottom_right=Decimal(0.1),
        border_width=Decimal(0.1)
        )

Adding an Image:

An image is added to the table using its URL.

Python
image = Image(
            "https://media.geeksforgeeks.org/wp-content/cdn-uploads/20210420155809/gfg-new-logo.png",
            width=Decimal(150),
            height=Decimal(100),
        )

Creating Barcodes:

Two barcodes are created: a QR code and a Code 128 barcode.

Python
qr_code = Barcode(data="https://www.geeksforgeeks.org", type=BarcodeType.QR, width=Decimal(128), height=Decimal(128))
bar = Barcode(data="https://www.geeksforgeeks.org", type=BarcodeType.CODE_128, width=Decimal(128), height=Decimal(128))

Creating a Chart:

A function is defined to create a scatter plot using Matplotlib, which is then added to the PDF as a chart.

Python
def create_plot():
    import matplotlib.pyplot as plt
    import numpy as np

    fig = plt.figure(dpi=800)
    ax = plt.axes()
    x = np.linspace(0, 10, 30)
    y = np.sin(x)
    ax.plot(x, y)

    return plt.gcf()

chart = Chart(create_plot(), width=Decimal(350), height=Decimal(300))

Adding a Map:

A map is added with custom fill and stroke colors.

Python
map = MapOfTheWorld(
            horizontal_alignment=Alignment.CENTERED,
            fill_color=HexColor("#00ffff"),
            stroke_color=HexColor("#ffffff"),
            line_width=Decimal(0.1),
        ).set_fill_color(fill_color=HexColor("#0b5394"), key="India")

Adding Emojis:

Emojis are included in an inline flow alongside text.

Python
flow = InlineFlow()
flow.add(ChunkOfText("Hello", font_size=72))
flow.add(Emojis.EARTH_AMERICAS.value)
flow.add(ChunkOfText("!", font_size=72))

Adding Elements to the Table:

Various elements like the image, barcodes, chart, map, and emojis are added to the table.

Python
table.add(image)
table.add(qr_code)
table.add(chart)
table.add(bar)
table.add(map)
table.add(flow)
table.add(Paragraph("Text-data", padding_left=Decimal(100), 
                    font_size=24 , background_color=HexColor("00FF00")))

Adding ProgressBars and Nested Tables:

A nested table with progress bars is added.

Python
table.add(FixedColumnWidthTable(number_of_columns=2, number_of_rows=3)
               .add(Paragraph("Coders"))
               .add(ProgressBar(percentage=0.45))
               .add(Paragraph("Non-Coders"))
               .add(ProgressBar(percentage=0.55))
               .add(Paragraph("Hehe ^_^"))
               .add(ProgressBar(percentage=1))
               .set_padding_on_all_cells(Decimal(3), Decimal(3), Decimal(3), Decimal(3))
               )

Adding an Unordered List:

An unordered list is created and added to the table.

Python
ul = UnorderedList()
ul.add(Paragraph("Item 1"))
ul.add(Paragraph("Item 2"))
ul.add(Paragraph("Item 3"))
table.add(ul)

Adding the Table to the Layout:

The table is added to the layout.

Python
layout.add(table)

Adding a Styled Paragraph:

A styled paragraph with custom font, size, color, and alignment is added to the layout.

Python
layout.add(Paragraph(
    "This is a custom styled paragraph.",
    font="Helvetica",
    font_size=72,
    font_color=HexColor("ff0000"),
    horizontal_alignment=Alignment.CENTERED,
    vertical_alignment=Alignment.TOP
))

Saving the PDF:

Finally, the document is saved to a file named styled_ebook.pdf.

Python
with open("styled_ebook.pdf", "wb") as pdf_file_handle:
    PDF.dumps(pdf_file_handle, pdf)

Output



Next Article
Article Tags :
Practice Tags :

Similar Reads