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.2.tar.gz (17.1 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.2-py3-none-any.whl (14.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: reportlab_typesetting-0.0.2.tar.gz
  • Upload date:
  • Size: 17.1 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.2.tar.gz
Algorithm Hash digest
SHA256 93231e1d220aac17b2a12d3c9d856b3b566b337a8088196d3baaa57d68ac0750
MD5 3f43f0427e9dd88ddd6b2da47a9b34d4
BLAKE2b-256 c39084c4808f6a62c54546b5381128c75785419be128c8994929aec6be45b20a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for reportlab_typesetting-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 fce21d27409b24b6bfb25bf464cc0ab2f9801670c4d41a2f60af3d398b49bc13
MD5 f5a13a8cdfea4f69071f4bb4c7223b60
BLAKE2b-256 3fa975e1ae18f3abf42fd027db61aee2c44469ceea5972afd8885f9caa4df18f

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