borb PDF Library

3.0.7 · active · verified Mon Apr 13

borb is a comprehensive Python library designed for reading, creating, and manipulating PDF files. It offers a wide range of functionalities from simple text and image handling to advanced layout management. The library is actively maintained with frequent patch releases, typically on a monthly or bi-monthly cadence, adding new features and improving robustness.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a simple PDF document containing 'Hello World!' text using borb. It initializes a Document, adds a Page, places a Paragraph using a bounding box, and saves the document to a file.

from borb.pdf.document.document import Document
from borb.pdf.page.page import Page
from borb.pdf.canvas.layout.text.paragraph import Paragraph
from borb.pdf.pdf import PDF
from borb.pdf.canvas.geometry.rectangle import Rectangle
import os

def create_hello_world_pdf():
    # Create a new Document
    pdf = Document()
    # Add a Page
    page = Page()
    pdf.add_page(page)

    # Add a Paragraph with 'Hello World!'
    page.add_layout_element(
        Paragraph(
            "Hello World! This is a borb generated PDF.",
            bounding_box=Rectangle(50, 700, 500, 50)
        )
    )

    # Define output path
    output_path = os.environ.get('BORB_OUTPUT_PATH', 'hello_borb.pdf')

    # Store the PDF to a file
    with open(output_path, "wb") as pdf_file_handle:
        PDF.add_document_to_file(pdf, pdf_file_handle)
    print(f"PDF saved to {output_path}")

if __name__ == "__main__":
    create_hello_world_pdf()

view raw JSON →