# chromium/pdfium architecture

> Architecture diagram of the GitHub repository chromium/pdfium, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/chromium/pdfium
- Repository: https://github.com/chromium/pdfium

## Overview

##### Overall Architecture and Main Components
- PDF Parsing Subsystem:
- “fpdfapi/parser” components (found under core/fpdfapi/parser) which are responsible for tokenizing and building the internal representation of PDF objects.
- This layer feeds into higher-level components, such as document assembly (core/fpdfdoc) and text extraction (core/fpdftext).

- Document Model and Object Management:
- "fpdfdoc" module that maintains the internal PDF document structure and metadata.
- Aspects such as bookmark trees, annotations, and form objects.

- Rendering Engine:
- Rendering modules under core/fpdfapi/render and the graphics engine in fxge.
- Connection between the device-independent rendering and computation routines (e.g., in fxcrt for low-level utilities and fxge for platform-specific rendering) and the higher-level PDF rendering calls.
- Experimental Skia backend (configured via pdf_use_skia) and integration with font rendering subsystems.

- Font, Image, and Codec Modules:
- Font system (core/fpdfapi/font) as well as fxcrt utilities that manage memory, string handling, and numerical operations that are essential to rendering.

- JavaScript and XFA Support:
- Fxjs and xfa directories as separate modules.
- The fxjs engine interacts with V8 (as indicated by cfx_v8 files) to support embedded JavaScript execution.
- The xfa and fxfa directories (plus supplementary XFA form modules under fpdfxfa and fpdfsdk/formfiller) represent support for Adobe’s XML Forms Architecture, integrating with PDF content if XFA is enabled.

- Public API / SDK Interface:
- “public” and “fpdfsdk” directories that expose embeddable APIs for external developers.
- Components as stable interfaces that sit on top of the core engine.

##### Supporting Infrastructure and Build Tools
- Build System & Configuration:
- A component for GN build configuration and Ninja-based build execution.
- Reference the integration with depot_tools (used to obtain compilers and toolchains) and the role of the provided scripts (such as install-build-deps.sh in Linux).

- Third-Party Libraries:
- Third-party dependencies such as libraries for JPEG, PNG, OpenJPEG, freetype, and others (located in the third_party directory).
- These serve as low-level libraries supporting decoding, font rendering, or image processing operations.

##### Relationships and Data Flow
- Also, show the optional flow for JavaScript and XFA: if enabled, the parsed document gives rise to script data that is processed by fxjs, and if there is form support, the XFA subsystem handles forms and interactive elements.

- Component Dependency Relationships:
- Modules like fxcrt provide utility functions that are used widely across the parser, rendering, and other submodules.
- The rendering engine may depend on both the document layer and the codec libraries for embedded image support.
- The public API/SDK acts as a façade for external calls to the core engine.

##### Architectural Patterns / Design Principles
- Modular and Layered Design:
- Use distinct layers (e.g., parsing, document assembly, rendering, platform abstractions) that communicate via clearly defined interfaces.
- Each module (such as parser, renderer, and form-handling) can be further broken down to show that they follow separation of concerns.

- Platform Abstraction:

- Extensibility:
- That the project is built with extensibility in mind (e.g., support for multiple rendering backends, optional JavaScript engine integration, and XFA support that can be toggled via GN args).

## Diagram (Mermaid)

```mermaid
flowchart TD
    A("PDF File Input"):::input

    subgraph "Parsing Layer"
        P("PDF Parsing Subsystem (fpdfapi/parser)"):::parsing
    end

    subgraph "Document Model"
        D("Document Model & Object Management (fpdfdoc)"):::document
    end

    subgraph "Rendering Engine"
        R("Rendering Modules (core/fpdfapi/render)"):::render
        G("Graphics Engine (fxge)"):::render
    end

    subgraph "Font, Image, & Codec Modules"
        F("Font System (fpdfapi/font)"):::aux
        C("Codec Modules (fxcodec)"):::aux
    end

    subgraph "Optional Features (JS/XFA)"
        J("JavaScript Engine (fxjs)"):::optional
        X("XFA Support (xfa)"):::optional
    end

    subgraph "Public API / SDK Interface"
        U("Public API (public)"):::api
        S("SDK Interface (fpdfsdk)"):::api
    end

    subgraph "Build & Dependencies"
        B("Build System & Config (BUILD.gn)"):::build
        T("Third‑Party Libraries (third_party)"):::build
    end

    %% Data flow
    A --> P
    P --> D
    D --> R
    D --> J
    D --> X
    R --> G
    R -->|"uses"| F
    R -->|"uses"| C
    R --> U
    U --> S

    %% Build and Dependencies wiring (dotted lines)
    B --- P
    B --- D
    B --- R
    T --- F
    T --- C

    %% Class definitions for styling
    classDef input fill:#f9c,stroke:#333,stroke-width:2px;
    classDef parsing fill:#bbf,stroke:#333,stroke-width:2px;
    classDef document fill:#cff,stroke:#333,stroke-width:2px;
    classDef render fill:#fcf,stroke:#333,stroke-width:2px;
    classDef aux fill:#dfd,stroke:#333,stroke-width:2px;
    classDef optional fill:#ffd,stroke:#333,stroke-width:2px;
    classDef api fill:#fdd,stroke:#333,stroke-width:2px;
    classDef build fill:#ddf,stroke:#333,stroke-width:2px;

    %% Click events for mapped components
    click P "https://github.com/chromium/pdfium/tree/main/core/fpdfapi/parser"
    click D "https://github.com/chromium/pdfium/tree/main/core/fpdfdoc"
    click R "https://github.com/chromium/pdfium/tree/main/core/fpdfapi/render"
    click G "https://github.com/chromium/pdfium/tree/main/core/fxge"
    click F "https://github.com/chromium/pdfium/tree/main/core/fpdfapi/font"
    click C "https://github.com/chromium/pdfium/tree/main/core/fxcodec"
    click J "https://github.com/chromium/pdfium/tree/main/fxjs"
    click X "https://github.com/chromium/pdfium/tree/main/xfa"
    click U "https://github.com/chromium/pdfium/tree/main/public"
    click S "https://github.com/chromium/pdfium/tree/main/fpdfsdk"
    click B "https://github.com/chromium/pdfium/blob/main/BUILD.gn"
    click T "https://github.com/chromium/pdfium/tree/main/third_party"
```
