요소
텍스트
가장 기본적인 요소입니다. 선택적 스타일링과 함께 텍스트를 추가합니다:
c.Text("Hello, World!")
c.Text("Styled text", template.FontSize(18), template.Bold())
c.Text("Red centered", template.TextColor(pdf.Red), template.AlignCenter())
텍스트 옵션
| 옵션 | 설명 |
|---|---|
FontSize(size) | 폰트 크기 (포인트 단위, 기본값: 12) |
Bold() | 굵게 |
Italic() | 기울임꼴 |
FontFamily(name) | 등록된 폰트 사용 |
TextColor(color) | 전경 색상 |
BgColor(color) | 배경 색상 |
AlignLeft() | 왼쪽 정렬 (기본값) |
AlignCenter() | 가운데 정렬 |
AlignRight() | 오른쪽 정렬 |
Underline() | 밑줄 장식 |
Strikethrough() | 취소선 장식 |
LetterSpacing(pts) | 문자 간 추가 간격 |
TextIndent(value) | 첫 줄 들여쓰기 |
리치 텍스트
RichText()를 사용한 혼합 스타일 인라인 텍스트:
c.RichText(func(rt *template.RichTextBuilder) {
rt.Span("Normal text ")
rt.Span("bold", template.Bold())
rt.Span(" and ")
rt.Span("red", template.TextColor(pdf.Red))
rt.Span(" in one line.")
})
┌─────────────────────────────────────────────┐
│ Normal text bold and red in one line. │
│ ^^^^ ^^^ │
│ bold red color │
└─────────────────────────────────────────────┘
테이블
헤더와 데이터 행이 있는 테이블:
// Basic table
c.Table(
[]string{"Name", "Age", "City"},
[][]string{
{"Alice", "30", "Tokyo"},
{"Bob", "25", "New York"},
{"Charlie", "35", "London"},
},
)
┌──────────┬──────┬──────────┐
│ Name │ Age │ City │ ← 헤더 (굵게)
├──────────┼──────┼──────────┤
│ Alice │ 30 │ Tokyo │
│ Bob │ 25 │ New York│
│ Charlie │ 35 │ London │
└──────────┴──────┴──────────┘
스타일이 적용된 테이블
darkBlue := pdf.RGBHex(0x1A237E)
lightGray := pdf.RGBHex(0xF5F5F5)
c.Table(
[]string{"Product", "Category", "Qty", "Unit Price", "Total"},
[][]string{
{"Laptop Pro 15", "Electronics", "2", "$1,299.00", "$2,598.00"},
{"Wireless Mouse", "Accessories", "10", "$29.99", "$299.90"},
{"USB-C Hub", "Accessories", "5", "$49.99", "$249.95"},
{"Monitor 27\"", "Electronics", "3", "$399.00", "$1,197.00"},
{"Keyboard", "Accessories", "10", "$79.99", "$799.90"},
{"Webcam HD", "Electronics", "4", "$89.99", "$359.96"},
},
template.ColumnWidths(30, 20, 10, 20, 20),
template.TableHeaderStyle(
template.TextColor(pdf.White),
template.BgColor(darkBlue),
),
template.TableStripe(lightGray),
)
┌─────────────┬────────────┬─────┬───────────┬──────────┐
│ Product │ Category │ Qty │Unit Price │ Total │ ← 흰색 글자 + 진한 파란색 배경
├─────────────┼────────────┼─────┼───────────┼──────────┤
│ Laptop Pro │ Electronics│ 2 │ $1,299.00 │$2,598.00 │ ← 흰색 배경
│ Wireless.. │ Accessories│ 10 │ $29.99 │ $299.90 │ ← 회색 스트라이프
│ USB-C Hub │ Accessories│ 5 │ $49.99 │ $249.95 │ ← 흰색 배경
│ Monitor 27"│ Electronics│ 3 │ $399.00 │$1,197.00 │ ← 회색 스트라이프
│ Keyboard │ Accessories│ 10 │ $79.99 │ $799.90 │ ← 흰색 배경
│ Webcam HD │ Electronics│ 4 │ $89.99 │ $359.96 │ ← 회색 스트라이프
└─────────────┴────────────┴─────┴───────────┴──────────┘
테이블 옵션
| 옵션 | 설명 |
|---|---|
ColumnWidths(widths...) | 열 너비 백분율 (합계가 100이어야 함) |
TableHeaderStyle(opts...) | 헤더 행 스타일 |
TableStripe(color) | 교대 행 배경 색상 |
TableCellVAlign(align) | 수직 정렬: VAlignTop, VAlignMiddle, VAlignBottom |
테이블 수직 정렬
c.Table(
[]string{"Align", "Short", "Long Content"},
[][]string{
{"Top", "A", "This is a longer text that wraps to multiple lines"},
{"Middle", "B", "Another longer text for demonstration purposes"},
},
template.TableCellVAlign(document.VAlignMiddle),
)
이미지
JPEG 또는 PNG 이미지를 추가합니다:
imgData, _ := os.ReadFile("photo.jpg")
c.Image(imgData)
c.Image(imgData, template.FitWidth(document.Mm(80)))
c.Image(imgData, template.FitHeight(document.Mm(50)))
이미지 옵션
| 옵션 | 설명 |
|---|---|
FitWidth(value) | 지정된 너비로 축소 |
FitHeight(value) | 지정된 높이로 축소 |
WithFitMode(mode) | 맞춤 모드: FitContain, FitCover, FitStretch, FitOriginal |
WithAlign(align) | 수평 정렬: AlignLeft, AlignCenter, AlignRight |
이미지 맞춤 모드
// Contain: scale to fit, preserve aspect ratio (default)
c.Image(data, template.WithFitMode(document.FitContain))
// Cover: scale to fill, may crop
c.Image(data, template.WithFitMode(document.FitCover))
// Stretch: fill bounds, may distort
c.Image(data, template.WithFitMode(document.FitStretch))
// Original: use original dimensions
c.Image(data, template.WithFitMode(document.FitOriginal))
이미지 정렬
c.Image(data, template.FitWidth(document.Mm(40)), template.WithAlign(document.AlignCenter))
c.Image(data, template.FitWidth(document.Mm(40)), template.WithAlign(document.AlignRight))
목록
비순서 목록
c.List([]string{
"Declarative document definition",
"All element types supported",
"Style options (bold, italic, color, align)",
"Header and footer support",
})
┌─────────────────────────────────────────────┐
│ • Declarative document definition │
│ • All element types supported │
│ • Style options (bold, italic, color, ...) │
│ • Header and footer support │
└─────────────────────────────────────────────┘
순서 목록
c.OrderedList([]string{
"Text with styles",
"Tables with headers",
"Lists (ordered/unordered)",
"Lines and spacers",
"QR codes and barcodes",
})
┌─────────────────────────────────────────────┐
│ 1. Text with styles │
│ 2. Tables with headers │
│ 3. Lists (ordered/unordered) │
│ 4. Lines and spacers │
│ 5. QR codes and barcodes │
└─────────────────────────────────────────────┘
목록 옵션
| 옵션 | 설명 |
|---|---|
ListIndent(value) | 왼쪽 들여쓰기 |
선
선택적 색상 및 두께가 있는 수평선:
// Default line (gray, 1pt)
c.Line()
// Colored line
c.Line(template.LineColor(pdf.Red))
c.Line(template.LineColor(pdf.Blue))
// Custom thickness
c.Line(template.LineThickness(document.Pt(0.5))) // thin
c.Line(template.LineThickness(document.Pt(2))) // medium
c.Line(template.LineThickness(document.Pt(5))) // thick
// Color + thickness
c.Line(
template.LineColor(pdf.RGBHex(0x1A237E)),
template.LineThickness(document.Pt(2)),
)
┌─────────────────────────────────────────────┐
│ Default line: │
│ ─────────────────────────────── (gray 1pt) │
│ Red line: │
│ ─────────────────────────────── (red) │
│ Thin (0.5pt): │
│ ─────────────────────────────── (thin) │
│ Thick (5pt): │
│ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ (thick) │
└─────────────────────────────────────────────┘
선 옵션
| 옵션 | 설명 |
|---|---|
LineColor(color) | 선 색상 |
LineThickness(value) | 선 두께 |
스페이서
요소 간 수직 간격을 추가합니다:
c.Text("Before spacer")
c.Spacer(document.Mm(10)) // 10mm gap
c.Text("After spacer")
c.Spacer(document.Mm(5)) // 5mm
c.Spacer(document.Mm(15)) // 15mm
c.Spacer(document.Mm(30)) // 30mm
┌─────────────────────────────────────────────┐
│ Before spacer │
│ │
│ ↕ 10mm │
│ │
│ After spacer │
└─────────────────────────────────────────────┘
QR 코드
텍스트 또는 URL에서 QR 코드를 생성합니다:
// Basic QR code
c.QRCode("https://gpdf.dev")
// With custom size
c.QRCode("https://gpdf.dev", template.QRSize(document.Mm(30)))
// With error correction level
c.QRCode("HELLO", template.QRSize(document.Mm(25)),
template.QRErrorCorrection(qrcode.LevelH))
// Japanese content
c.QRCode("こんにちは世界", template.QRSize(document.Mm(30)))
다양한 크기의 QR 코드
page.AutoRow(func(r *template.RowBuilder) {
r.Col(4, func(c *template.ColBuilder) {
c.Text("20mm", template.FontSize(9))
c.QRCode("https://gpdf.dev", template.QRSize(document.Mm(20)))
})
r.Col(4, func(c *template.ColBuilder) {
c.Text("30mm", template.FontSize(9))
c.QRCode("https://gpdf.dev", template.QRSize(document.Mm(30)))
})
r.Col(4, func(c *template.ColBuilder) {
c.Text("40mm", template.FontSize(9))
c.QRCode("https://gpdf.dev", template.QRSize(document.Mm(40)))
})
})
┌───────────────┬───────────────┬───────────────┐
│ 20mm │ 30mm │ 40mm │
│ ┌──┐ │ ┌────┐ │ ┌──────┐ │
│ │QR│ │ │ QR │ │ │ QR │ │
│ └──┘ │ └────┘ │ │ │ │
│ │ │ └──────┘ │
└───────────────┴───────────────┴───────────────┘
오류 정정 레벨
| 레벨 | 복구율 | 적합한 용도 |
|---|---|---|
qrcode.LevelL | ~7% | 깨끗한 환경 |
qrcode.LevelM | ~15% | 일반 용도 (기본값) |
qrcode.LevelQ | ~25% | 산업 용도 |
qrcode.LevelH | ~30% | 열악한 환경 |
import "github.com/gpdf-dev/gpdf/qrcode"
c.QRCode("HELLO", template.QRErrorCorrection(qrcode.LevelH))
QR 코드 옵션
| 옵션 | 설명 |
|---|---|
QRSize(value) | 표시 크기 (너비 = 높이) |
QRErrorCorrection(level) | 오류 정정 레벨 |
QRScale(s) | QR 모듈당 픽셀 수 |
바코드
Code 128 바코드를 생성합니다:
// Basic barcode
c.Barcode("INV-2026-0001")
// With custom width
c.Barcode("PRODUCT-A-12345", template.BarcodeWidth(document.Mm(80)))
// With custom height
c.Barcode("SMALL-BAR", template.BarcodeHeight(document.Mm(10)))
// Numeric data (automatically optimized with Code C)
c.Barcode("1234567890")
열 내 바코드
page.AutoRow(func(r *template.RowBuilder) {
r.Col(6, func(c *template.ColBuilder) {
c.Text("Item A", template.FontSize(9))
c.Barcode("ITEM-A-001", template.BarcodeWidth(document.Mm(60)))
})
r.Col(6, func(c *template.ColBuilder) {
c.Text("Item B", template.FontSize(9))
c.Barcode("ITEM-B-002", template.BarcodeWidth(document.Mm(60)))
})
})
┌────────────────────────┬────────────────────────┐
│ Item A │ Item B │
│ ║║│║║│║║║│║║│║║│ │ ║║│║║│║║║│║║│║║│ │
│ ITEM-A-001 │ ITEM-B-002 │
└────────────────────────┴────────────────────────┘
바코드 옵션
| 옵션 | 설명 |
|---|---|
BarcodeWidth(value) | 표시 너비 |
BarcodeHeight(value) | 표시 높이 |
BarcodeFormat(format) | 바코드 형식 (기본값: Code 128) |
절대 위치 지정
일반적인 그리드 흐름 외부에서 페이지의 정확한 XY 좌표에 요소를 배치합니다. 워터마크, 스탬프, 로고, 오버레이에 유용합니다.
page.Absolute(document.Mm(120), document.Mm(20), func(c *template.ColBuilder) {
c.Text("CONFIDENTIAL", template.FontSize(20), template.Bold(), template.TextColor(pdf.Red))
})
// With explicit size
page.Absolute(document.Mm(10), document.Mm(250), func(c *template.ColBuilder) {
c.QRCode("https://gpdf.dev", template.QRSize(document.Mm(20)))
}, gpdf.AbsoluteWidth(document.Mm(25)), gpdf.AbsoluteHeight(document.Mm(25)))
// Use page corner as origin (ignores margins)
page.Absolute(document.Mm(0), document.Mm(0), func(c *template.ColBuilder) {
c.Text("Top-left corner of page")
}, gpdf.AbsoluteOriginPage())
┌─ Page ──────────────────────────────────────────┐
│ ┌─ Content Area ───────────────────────────┐ │
│ │ │ │
│ │ [Normal flow content] │ │
│ │ │ │
│ │ ┌──────────────┐ │ │
│ │ │ CONFIDENTIAL │ ← absolute │ │
│ │ └──────────────┘ (120,20) │ │
│ │ │ │
│ │ ┌──┐ │ │
│ │ │QR│ ← absolute (10,250) │ │
│ │ └──┘ │ │
│ └──────────────────────────────────────────┘ │
└─────────────────────────────────────────────────┘
좌표 원점
| 원점 | 설명 | 기본값 |
|---|---|---|
| 콘텐츠 영역 | 콘텐츠 영역의 좌상단 기준 XY (여백 내부) | 예 |
| 페이지 | 페이지 좌상단 모서리 기준 XY (여백 무시) | 아니오 |
절대 위치 지정 옵션
| 옵션 | 설명 |
|---|---|
AbsoluteWidth(value) | 절대 블록의 명시적 너비 |
AbsoluteHeight(value) | 절대 블록의 명시적 높이 |
AbsoluteOriginPage() | 콘텐츠 영역 대신 페이지 모서리를 원점으로 사용 |
페이지 번호 / 전체 페이지 수
머리글이나 바닥글에서 자동 페이지 번호 매기기:
// Current page number (e.g., "1", "2", "3")
c.PageNumber(template.AlignRight(), template.FontSize(8))
// Total page count (e.g., "4 total pages")
c.TotalPages(template.AlignRight(), template.FontSize(9))
둘 다 스타일링을 위한 표준 텍스트 옵션을 지원합니다.