borb PDF Library
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
- breaking Major architectural changes in borb v3.0.0/v3.0.1, including the removal of the `Decimal` type for numeric operations and the integration of `Annotation` objects into the `LayoutElement` framework. Code written for borb v2.x will likely require significant modification.
- gotcha Starting with v3.0.7, many advanced features (e.g., image processing, advanced text layout, generative AI integrations) are moved to optional 'extras'. A simple `pip install borb` will only install the core library. To access full functionality, you must install with `borb[full]` or specific feature groups like `borb[image,text]`.
- gotcha Versions prior to v3.0.1 had packaging issues due to the inclusion of non-Python files, which could cause problems when attempting to package or deploy `borb` in certain environments.
Install
-
pip install borb -
pip install borb[full] -
pip install borb[image,text,genai]
Imports
- Document
from borb.pdf.document.document import Document
- Page
from borb.pdf.page.page import Page
- Paragraph
from borb.pdf.canvas.layout.text.paragraph import Paragraph
- Image
from borb.pdf.canvas.layout.image.image import Image
- PDF
from borb.pdf.pdf import PDF
- Rectangle
from borb.pdf.canvas.geometry.rectangle import Rectangle
Quickstart
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()