kordoc

repository·main·Indexed 23 days ago

https://github.com/chrisryugj/kordoc

A document processing engine for parsing, comparing, and generating Korean government formats (HWP3-5, HWPX, HWPML, PDF, XLS(X), DOCX) to Markdown. Version 4.4.0 features a CLI and MCP server for AI agent integration, offering lossless roundtrip editing, form-filling, document diffing, and a layout-preserving SVG renderer. It includes built-in OCR for scanned documents, RAG-ready structural chunking, and specialized presets for generating official Korean administrative reports.

Tokens
35.5K
Snippets
56
Records
196
Agent score
81%

What's inside kordoc

  1. Overview of kordoc capabilities

    main

    kordoc is a document processing engine designed for official government-style documents. Key capabilities include:

    • Document to Markdown Conversion: Converts HWP3, HWP (5.x), HWPX, HWPML, PDF, XLS, XLSX, DOCX, and images (PNG/JPG/WebP via OCR) into Markdown for LLM consumption.
    • Complex Table Reconstruction: Accurately restores complex or borderless tables (including comparison tables in legal PDFs) into Markdown tables.
    • Document Comparison: Automatically generates comparison tables (신구대조표) between two documents (e.g., HWP vs HWPX).
    • Markdown to HWPX: Converts AI-generated Markdown back into official HWPX report formats.
    • Government Standard Document Generation (v4.0+): Includes engines for 16 types of standard government forms and 60+ draft templates. Supports official numbering (e.g., 1. 가. 1) 가)...), layout rules, and kordoc lint for compliance.
    • Lossless Roundtrip (v3.0+): Using patchHwpx or patchHwp, you can edit Markdown and apply changes back to the original file without losing any original formatting (including adding/deleting rows in tables).
    • Layout Rendering (v3.10+): Renders original layouts as SVG using HWPX layout cache or a pure TypeScript reflow engine.
    • Chart Generation (v3.16+): Converts Markdown ```chart fences into native HWP/OOXML charts (20+ types).
    • Automatic Sealing (v3.16+): Automatically places seal/signature PNGs near anchor text like (인) or 서명 또는 인 using kordoc seal.
    • Form Auto-filling: Automatically fills templates (applications, reports) while preserving 100% of original formatting.
    • AI Agent Integration (MCP): Direct integration with Claude Desktop, Cursor, and Codex.
  2. Core Capabilities of kordoc

    main

    kordoc is designed to automate the lifecycle of Korean official documents. Key capabilities include:

    • Document to Markdown Conversion: Converts HWP3, HWP (5.x), HWPX, HWPML, PDF, XLS, XLSX, DOCX, and images (PNG/JPG/WebP) to Markdown. Includes automatic OCR for images.
    • Table Reconstruction: Structurally restores complex or borderless tables (from PDF or HWP) into accurate Markdown tables.
    • Document Comparison (Diff): Compares two documents, including cross-format comparison (e.g., HWP vs HWPX).
    • Markdown to HWPX Generation: Converts AI-generated Markdown back into official HWPX report formats.
    • Government-Standard Generation: Includes presets for Gaejosik reports, statutory drafts, and press releases, following official Korean administrative styles (8-level numbering, specific headers/footers).
    • Lossless Roundtripping: Using patchHwpx or patchHwp, you can swap text in a document while preserving 100% of the original formatting.
    • Layout-Preserving Rendering: Reproduces original layouts as SVG or via a pure-TS reflow engine.
    • Chart Generation: Converts Markdown chart fences into native Hancom charts.
    • Stamp/Signature Placement: Uses kordoc seal to place stamp PNGs at anchor phrases like (인) without shifting layout.
    • Form Auto-fill: Automatically fills blank fields in official templates while preserving original formatting.
    • PII Redaction: Detects and masks sensitive information (Resident Registration Numbers, phone numbers, etc.) in a format-preserving way using kordoc redact.
  3. Understand word breaking behavior (Latin vs Non-Latin)

    main

    The engine handles word breaking (line wrapping) differently for English (Latin) and Korean (Non-Latin) characters to match Hancom (HWP) behavior. Note that the property names for Non-Latin characters are counter-intuitive:

    PropertyLatin (English)Non-Latin (Korean)
    breakLatinWordKEEP_WORD (keeps words)KEEP_WORD (breaks characters)
    breakNonLatinWordBREAK_WORD (breaks words)BREAK_WORD (keeps words)

    Recommended Production Setting: To match standard official documents, use:

    • breakNonLatinWord="BREAK_WORD" (Keeps Korean words/eojeol intact)
    • breakLatinWord="KEEP_WORD" (Keeps English words intact)

    Note: Only single tokens that are longer than the line width (like long URLs) will be forcibly broken.

  4. Automate form filling with kordoc

    main

    kordoc supports automatic form filling for official document templates. It can recognize and process several patterns:

    • Label-Value cells: Identifying pairs in tables.
    • Checkboxes: Converting to .
    • Parentheses: Filling empty spaces like 일반( )통 $\rightarrow$ 일반(3)통.
    • Annotations: Converting patterns like (한자:) to (한자:金).

    Additionally, AI agents can interact with these forms using the fill_form MCP tool.

  5. Structure a Korean Official Document (Draft/Enforcement)

    main

    A standard official document (기안문/시행문) is divided into three main sections: Header (두문), Body (본문), and Closing (결문).

    1. Header (두문)

    • Administrative Agency Name: Centered at the very top. If multiple agencies are involved, include the superior agency name.
    • Recipient (수신): Format as Recipient Name (Position/Department). For internal approvals, use 내부결재. If there are multiple recipients, use 수신자 참조 and list them in the Closing section.
    • Via (경유): Only used if the document is being routed through an intermediary agency.

    2. Body (본문)

    • Title (제목): Located below the Header. Format: Title [Space] Content.
    • Content (내용): Follows the hierarchical bullet system. It is common practice to start with 1. 관련: (Reference Document Number + Title). Use the 'Time-Place-Target-Content-Method' (일·장·대·내·방) sequence for details.
    • Attachments (붙임): Listed after the body content.

    3. Closing (결문)

    • Sender Name (발신 명의): Centered below the body. Usually the head of the agency. The official seal (관인) should be stamped so that the last character of the name is in the center of the seal.
    • Approval Line (결재/협조): Uses a 2-line structure:
      • Line 1: 결재 (Position) Drafter Signature (Position) Reviewer Signature (Position) Approver Signature
      • Line 2: 협조 (Position) Collaborator Signature (Collaborators must be on a separate line).
    • Signifiers: Use for Proposer (발의자) and for Reporter (보고자) before their position/rank.
    • Execution/Receipt (시행/접수): Format as 시행 [Agency Name]-[Serial No.](Date).
    • Agency Info: A 2-column layout at the bottom containing Address, Website, Phone, Fax, Email, and Disclosure Status (Public/Partial/Private).
  6. Use built-in government standard templates

    main
    kordoc includes built-in HWPX templates based on Korean government standards (e.g., gian for general official documents and gian-simple for internal reports). You can use these via the CLI or API to create high-quality official documents without providing a template file.
  7. Parse BIFF Records from a Stream

    main

    Every BIFF record consists of a 4-byte header followed by variable-length data. The header contains an opcode (16-bit) and a length (16-bit). If the data length exceeds 8224 bytes, the record is split into multiple segments using CONTINUE (0x003C) records.

    Record Header Layout:

    • opcode: 16-bit Little-Endian (LE) record type.
    • length: 16-bit LE data length.
    • data: The actual payload of length bytes.

    Implementation Logic: Iterate through the stream by reading the opcode and length, then advancing the offset by 4 + length until the end of the stream is reached.

    while offset < stream.length:
      opcode = readU16LE(offset)
      length = readU16LE(offset + 2)
      data = stream[offset+4 : offset+4+length]
      yield { opcode, length, data }
      offset += 4 + length
  8. Use Format Profiles to replicate document styles

    main

    Format Profiles allow you to extract and apply specific document styles (borders, shading, column widths, and cell fonts) without sharing the original document content.

    1. Extract a profile: Use hwpxToProfile(hwpx) to get a JSON representation of the reference style.
    2. Apply a profile: Use markdownToHwpx(md, { profile }) to apply that style to a new document.

    The schema for these profiles is defined in docs/format-profile-spec.md.

  9. Interpret PDF text quality signals

    main

    When parsing PDFs with parsePdf, kordoc provides quality signals to help determine if OCR (Optical Character Recognition) is required. kordoc does not perform OCR itself but provides the metadata needed to route documents to an OCR pipeline.

    Key Quality Fields:

    • pageQuality: A per-page quality signal.
    • qualitySummary: A summary of the document's text quality.
    • needsOcr: Boolean indicating if the text layer is likely broken or unreadable.
    • ocrReason: A string explaining why OCR is recommended (e.g., broken ToUnicode/CMap mappings or control characters).
  10. Rules for Tables in Official Documents

    main

    While specific laws do not dictate table formatting, administrative and academic conventions (KAMJE) are widely used.

    Ending a Table

    • If the table ends at the last cell: Leave two spaces (one character width) from the left boundary and write 끝.
    • If the table ends mid-way (empty cells remain): Do not write 끝.. Instead, write 이하 빈칸 on the next line/cell.

    Cell Alignment (Administrative Default)

    • Text (문자): Left-aligned
    • Numbers (숫자): Right-aligned (align by decimal point if applicable)
    • Headers (제목행): Centered

    Table Styling

    • Units: Place (단위: 천원) at the top right of the table or in the column header.
    • Empty/N/A Cells: Use - or 해당없음.
    • Font: Typically slightly smaller than body text (13-14pt) using sans-serif fonts like 한컴돋움.
    • Line Spacing: Default is 123% in HWP (Hangul).
  11. Parse various document formats with kordoc

    main

    kordoc provides parsers for a wide range of document formats, converting them into structured Markdown or IR (Intermediate Representation) blocks:

    • HWP/HWPX: Supports HWP 3.0 (legacy binary), HWP 5.x (binary), HWPML 2.x (XML), and HWPX (modern XML). Includes support for DRM-protected (read-only) files via COM API fallback on Windows.
    • PDF: Supports standard text extraction, 2-column layout detection (for papers/reports), and table detection (both line-based and cluster-based for borderless tables).
    • XLSX: Excel spreadsheet parsing with support for shared strings, merged cells, and multiple sheets.
    • DOCX: Microsoft Word parsing with support for styles, lists, footnotes, hyperlinks, and images.
    • OCR: For scanned or image-based PDFs, you can enable OCR using the ocr: true option (supports PP-OCRv5 Korean) or use external providers like Tesseract or Claude Vision.
  12. Use Format Profiles to reproduce document styles

    main

    Format profiles allow you to reproduce a table's borders, shading, measured column widths, and cell fonts without needing the original source document. This is useful for sharing an organization's formatting (e.g., a specific government report style) without leaking sensitive content.

    To use this feature:

    1. Extract a style-only JSON profile from a reference HWPX file using hwpxToProfile(hwpx).
    2. Apply that profile to a new document using markdownToHwpx(md, { profile }).

    The schema for these profiles is defined in docs/format-profile-spec.md.