Skip to main content

A library to simplify complex typesetting with ReportLab, featuring Japanese Kinsoku rules, automatic font switching, and block alignment.

Project description

ReportLab Typesetting

A Python library to simplify common typesetting tasks with ReportLab.

Installation

pip install reportlab_typesetting

Usage

Here is a simple example of how to use the library:

import logging
import os

from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas

from reportlab_typesetting import (
    BlockAligner,
    CanvasRenderer,
    Font,
    HAlign,
    LayoutEngine,
    VAlign,
)

logging.basicConfig(
    level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
)

OUTPUT_DIR = "output"

if not os.path.exists(OUTPUT_DIR):
    os.makedirs(OUTPUT_DIR)

PDF_PATH = os.path.join(OUTPUT_DIR, "example.pdf")

# 1. ReportLabのCanvasを準備
c = canvas.Canvas(PDF_PATH, pagesize=letter)
page_width, page_height = letter

# 2. フォントを準備(TTFフォントを前提)
font_family = [
    Font(name="avenir-next", path="/path/to/fonts/avenir-next-regular.ttf"),
    Font(name="SourceHanSansJP", path="/path/to/fonts/SourceHanSansJP-Regular.ttf"),
]

# 3. レイアウトエンジンを初期化
engine = LayoutEngine()
engine.add_font_family(font_family)

# 4. レイアウトしたいテキスト
text_content = (
    "これは reportlab_typesetting ライブラリです。"
    "日本語とEnglishが混在した文章の禁則処理や、"
    "自動的なフォント切り替えを提供します。"
    "This is a test of the reportlab_typesetting library. "
)

# 5. テキストをレイアウト
block_width, block_height = 400, 200

x_pos = 50
y_pos = page_height - 50

text_layout = engine.layout(
    text_content,
    width=block_width,
    font_size=16,
    leading_ratio=1.6,
    use_justification=True,
    use_hyphenation=False,
)

# 6. ブロックの中央に配置する例
aligned_layout = (
    BlockAligner(text_layout, width=block_width, height=block_height)
    .alignment(horizontal=HAlign.CENTER, vertical=VAlign.MIDDLE)
    .apply()
)

# 7. 描画
renderer = CanvasRenderer(canvas=c)
renderer.draw_block_guides(x_pos, y_pos, block_width, block_height)
renderer.draw_layout_guides(aligned_layout, x_pos, y_pos)
renderer.render(aligned_layout, x_pos, y_pos)

# 8. 保存
c.save()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

reportlab_typesetting-0.0.3.tar.gz (17.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

reportlab_typesetting-0.0.3-py3-none-any.whl (15.2 kB view details)

Uploaded Python 3

File details

Details for the file reportlab_typesetting-0.0.3.tar.gz.

File metadata

  • Download URL: reportlab_typesetting-0.0.3.tar.gz
  • Upload date:
  • Size: 17.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.4

File hashes

Hashes for reportlab_typesetting-0.0.3.tar.gz
Algorithm Hash digest
SHA256 3158c7187c63e4a13c3307a81998da5a195c1c9c049e937cac1a7511d98e9643
MD5 f3215f13e2ac0c734dd7ee114e3ca15d
BLAKE2b-256 baab87f1493708bde5dc4f9fc0eace15335b63433ef0341c58e52e4b5f1dc1be

See more details on using hashes here.

File details

Details for the file reportlab_typesetting-0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for reportlab_typesetting-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 e6978eba107a16e3a8f3f8bde66c2f3f1742e9973a716cd8bb69a7155f4a5c92
MD5 7288d09f3759ed1d0f0d1349ca72fede
BLAKE2b-256 21fc07dfc0030fc9d9e2a873189efde79085573567cfcb66325f057203707210

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page