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.4.tar.gz (18.5 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.4-py3-none-any.whl (16.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: reportlab_typesetting-0.0.4.tar.gz
  • Upload date:
  • Size: 18.5 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.4.tar.gz
Algorithm Hash digest
SHA256 c1e914aaaea935d53137966278039ddca41c5ac86548db4a3b267b1df8364fee
MD5 bccc4a04b9d497ccac564750c8bf336b
BLAKE2b-256 3062b235334ce2b730b8f1005120f67df67897563b5a6ce9c43133b6bdedfc87

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for reportlab_typesetting-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 ed0e5ce58656d469c662914e419ccb47ab74f3d34c77a5b4f23b714ad36f3eb3
MD5 0b28954b8ac483a498bfee786caedad3
BLAKE2b-256 b0f9db923749244e0decaacb0136ffe6bbdfdd349c2ec59c5b34486ce9f1cc8e

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