빠른 시작

설치

go get github.com/gpdf-dev/gpdf

gpdf는 외부 의존성이 전혀 없습니다 — Go 표준 라이브러리만 사용합니다.

Hello World

package main

import (
    "os"
    "github.com/gpdf-dev/gpdf/document"
    "github.com/gpdf-dev/gpdf/template"
)

func main() {
    doc := template.New(
        template.WithPageSize(document.A4),
        template.WithMargins(document.UniformEdges(document.Mm(20))),
    )

    page := doc.AddPage()
    page.AutoRow(func(r *template.RowBuilder) {
        r.Col(12, func(c *template.ColBuilder) {
            c.Text("Hello, World!", template.FontSize(24), template.Bold())
        })
    })

    data, _ := doc.Generate()
    os.WriteFile("hello.pdf", data, 0644)
}
┌─ A4 ──────────────────────────────────┐
│                                       │
│   Hello, World!    ← 24pt, Bold       │
│                                       │
│                                       │
└───────────────────────────────────────┘

PDF를 생성하는 세 가지 방법

gpdf는 PDF 생성을 위한 세 가지 접근 방식을 제공합니다.

1. Builder API (권장)

가장 일반적인 접근 방식 — 빌더 패턴을 활용한 프로그래밍 방식의 제어:

doc := template.New(template.WithPageSize(document.A4))
page := doc.AddPage()
page.AutoRow(func(r *template.RowBuilder) {
    r.Col(6, func(c *template.ColBuilder) {
        c.Text("Left column")
    })
    r.Col(6, func(c *template.ColBuilder) {
        c.Text("Right column")
    })
})
data, err := doc.Generate()

2. JSON 스키마

JSON으로 문서를 선언적으로 정의합니다 — 동적 콘텐츠 및 API 기반 생성에 적합합니다:

schema := []byte(`{
    "page": {"size": "A4", "margins": "20mm"},
    "body": [
        {"row": {"cols": [
            {"span": 12, "text": "Hello from JSON!", "style": {"size": 24, "bold": true}}
        ]}}
    ]
}`)

doc, err := template.FromJSON(schema, nil)
data, err := doc.Generate()

3. 재사용 가능한 컴포넌트

일반적인 비즈니스 문서를 위한 사전 구축된 컴포넌트:

doc := template.Invoice(template.InvoiceData{
    Number:  "#INV-2026-001",
    Date:    "March 1, 2026",
    DueDate: "March 31, 2026",
    From:    template.InvoiceParty{Name: "ACME Corp", Address: []string{"123 Main St"}},
    To:      template.InvoiceParty{Name: "Client Inc", Address: []string{"456 Oak Ave"}},
    Items: []template.InvoiceItem{
        {Description: "Web Development", Quantity: "40 hrs", UnitPrice: 150.00, Amount: 6000.00},
    },
    TaxRate:  10,
    Currency: "$",
})
data, err := doc.Generate()

출력 방법

// Generate는 PDF를 []byte로 반환합니다
data, err := doc.Generate()

// Render는 PDF를 임의의 io.Writer에 씁니다
err := doc.Render(w)

다음 단계