$ show prawnpdf/prawn
Prawn is a pure Ruby library for generating PDF documents, designed for server-side PDF creation. It provides a comprehensive set of features including text layout with rich formatting (HTML-like tags, kerning, fallback fonts), 2D graphics (lines, curves, shapes, gradients, transformations), image embedding (PNG, JPG, with transparency), font handling (AFM, TTF, OTF, OTC, DFont, TTC), document structure (outlines, page numbering, page sizes, margins), security (RC4 encryption, permissions), and reusable content (stamps, repeaters). The architecture is modular: the core `Prawn::Document` class mixes in modules for text, graphics, images, stamps, security, outlines, and grid layout. Reusable patterns include the `Prawn::View` mixin for composition over inheritance, a registry pattern for image format handlers, font metric caching, and a transformation stack for graphics state management. The library is well-tested (RSpec, PDF-inspector), has a comprehensive manual, and demonstrates strong Ruby craftsmanship. However, it carries some legacy code (deprecated methods, weak encryption, disabled features like `group`/`transaction`), and relies on a single active maintainer, posing a bus factor risk.
The LLM identified 5 files worth highlighting for reuse:
Provides a `Prawn::View` mixin that allows any Ruby class to gain the full Prawn document DSL without inheriting from `Prawn::Document`. It defines a `document` method and delegates all missing methods to that document, enabling clean separation of document generation logic from business objects. This is a textbook example of composition over inheritance, and can be extracted for any library that needs to offer a DSL mixin.
Implements a per-document cache for font string width calculations using a `SynchronizedCache` (thread-safe hash). It caches computed widths by font identifier and string, avoiding redundant metric lookups. The pattern is generic and can be reused for any expensive computation that benefits from per-instance caching with thread safety.
Converts HTML-like markup strings (e.g., `<b>bold</b>`, `<color rgb='...'>`) into an array of formatted text hashes with properties like `:text`, `:styles`, `:color`, `:link`, `:anchor`. Handles nested tags, escaping, and options. This parser is self-contained and could be extracted for any project that needs to parse simple inline markup into structured formatting data.
Monkey-patches `Numeric` with methods like `#mm`, `#cm`, `#dm`, `#m`, `#in`, `#ft`, `#yd`, `#pt` to convert to PDF points. This is a convenient DSL for specifying dimensions in human-readable units. While monkey-patching is controversial, the pattern is clean, well-documented, and easily reusable for any project that needs unit conversion in a numeric context.
Manages a stack of coordinate transformation matrices (CTMs) for PDF graphics state. Provides `save_graphics_state` and `restore_graphics_state` methods that push/pop CTM entries. This is a classic stack-based state management pattern, used by many graphics libraries, and is implemented in a clean, self-contained module that could be reused for any 2D rendering system.