# appvantageasia/html2pdf-service architecture

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

- Interactive diagram: https://gitdiagram.com/appvantageasia/html2pdf-service
- Repository: https://github.com/appvantageasia/html2pdf-service

## Overview

##### Project Type and Purpose
- This is a single-purpose microservice: a Restful API that converts HTML into PDF (html2pdf-service).
- It uses Node.js + TypeScript (src/*.ts), Puppeteer under the hood (browser.ts), Dockerization (docker/*), and automated CI/CD.

##### File Structure
- Top-level “src” directory:
- index.ts: server bootstrap
- app.ts: Express app and route definitions
- browser.ts: Puppeteer launcher (Chrome/Chromium)
- render.ts: PDF rendering logic
- “docker/chrome” & “docker/chromium” folders: custom headless browser images
- CI/CD config: .circleci/config.yml and .github/workflows/docker-publish.yml
- “docs/”: static API docs published via GitHub Pages
- k6.js: performance/load-test script

##### README
- Confirms REST API, PDF-from-HTML conversion.
- Links to full documentation: shows endpoints, example requests/responses.
- Mentions containerized deployment (“docker-publish.yml”).

a. Client
- Any HTTP client (curl, Postman, frontend app, other microservice)
b. API Gateway / Node.js Server
- Express-based HTTP server (src/app.ts, src/index.ts)
c. PDF Generation Module
- Puppeteer wrapper (src/browser.ts)
- Rendering logic (src/render.ts)
d. Headless Browser Container
- Custom Chrome/Chromium Docker images (docker/chrome, docker/chromium)
e. CI/CD Pipeline
- CircleCI and GitHub Actions build → test → publish Docker image
f. Documentation Site
- Static API docs hosted on GitHub Pages (docs/)

1. Client sends HTTP POST (HTML payload) →
2. API Server receives request →
3. API calls PDF Generation Module →
4. Module spawns Puppeteer in headless container →
5. Puppeteer navigates to HTML → generates PDF stream →
6. Module returns PDF to API →
7. API streams PDF back to Client
8. CI/CD pipeline monitors repo → builds Docker image → pushes to registry
9. Documentation site is updated on GitHub Pages commits

##### Architectural Patterns & Design Principles
- Single-Responsibility Service: one API, one job (HTML → PDF).
- Modular Code: separation of HTTP handling (app.ts), business logic (render.ts), browser orchestration (browser.ts).
- Containerization: isolates dependencies (Chrome/Chromium) from host environment.
- CI/CD automation: ensures images are tested & published on push/tag.

##### Technologies & Frameworks to Annotate
- Node.js + TypeScript
- Express.js (HTTP routing)
- Puppeteer (headless browser)
- Docker (service & browser images)
- CircleCI / GitHub Actions (CI/CD)
- Yarn 3 (package management)
- k6 (load testing)

##### Essential Elements
- Client → API Server (Express)
- API Server → PDF Module (render.ts)
- PDF Module → Headless Browser (Puppeteer in Docker)
- CI/CD Toolchain → Docker Registry → Deployment
- Static Documentation Site

## Diagram (Mermaid)

```mermaid
flowchart TD
    Client["Client"]:::external
    K6["Load Testing (k6)"]:::test
    subgraph "Node.js Service"
        API["API Server (Express)"]:::service
        App["app.ts"]:::module
        Index["index.ts"]:::module
        BrowserMod["browser.ts"]:::module
        Render["render.ts"]:::module
    end
    subgraph "Browser Container"
        ChromeImage["Chrome Docker Image"]:::container
        ChromiumImage["Chromium Docker Image"]:::container
    end
    subgraph "CI/CD Pipeline"
        CircleCI["CircleCI"]:::ci
        GHActions["GitHub Actions"]:::ci
        DockerRegistry["Docker Registry"]:::container
    end
    Docs["Documentation Site (GitHub Pages)"]:::doc

    Client -->|"POST /api/v1/render"| API
    K6 -->|"load test"| API
    API -->|"initializes| App
    API -->|"initializes| Index
    API -->|"calls Render Module"| Render
    Render -->|"spawns Puppeteer"| BrowserMod
    BrowserMod -->|"runs in container"| ChromeImage
    BrowserMod -->|"runs in container"| ChromiumImage
    ChromeImage -->|"PDF stream"| Render
    ChromiumImage -->|"PDF stream"| Render
    Render -->|"streams PDF"| API
    API -->|"returns PDF"| Client

    CircleCI -->|"build & test"| DockerRegistry
    GHActions -->|"build & publish"| DockerRegistry
    DockerRegistry -->|"deploys container"| BrowserMod

    GHActions -->|"on push updates"| Docs

    classDef external fill:#d0ebff,stroke:#1c7ed6,color:#1c7ed6;
    classDef service fill:#e7f5ff,stroke:#0b7285,color:#0b7285;
    classDef module fill:#e3fafc,stroke:#15aabf,color:#15aabf;
    classDef container fill:#fff3bf,stroke:#f08c00,color:#f08c00;
    classDef ci fill:#e7e5f2,stroke:#5f3dc4,color:#5f3dc4;
    classDef doc fill:#f8f0fc,stroke:#be4bdb,color:#be4bdb;
    classDef test fill:#e6fcf5,stroke:#2f9e44,color:#2f9e44;

    click App "https://github.com/appvantageasia/html2pdf-service/blob/master/src/app.ts"
    click Index "https://github.com/appvantageasia/html2pdf-service/blob/master/src/index.ts"
    click BrowserMod "https://github.com/appvantageasia/html2pdf-service/blob/master/src/browser.ts"
    click Render "https://github.com/appvantageasia/html2pdf-service/blob/master/src/render.ts"
    click ChromeImage "https://github.com/appvantageasia/html2pdf-service/tree/master/docker/chrome/Dockerfile"
    click ChromiumImage "https://github.com/appvantageasia/html2pdf-service/tree/master/docker/chromium/Dockerfile"
    click CircleCI "https://github.com/appvantageasia/html2pdf-service/blob/master/.circleci/config.yml"
    click GHActions "https://github.com/appvantageasia/html2pdf-service/blob/master/.github/workflows/docker-publish.yml"
    click Docs "https://github.com/appvantageasia/html2pdf-service/tree/master/docs/"
    click K6 "https://github.com/appvantageasia/html2pdf-service/blob/master/k6.js"
```
