gipdf

package module
v1.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 12, 2025 License: MIT Imports: 3 Imported by: 0

README

gipdf

A flexible PDF generation library for Go, built on top of github.com/phpdave11/gofpdf. This library provides a component-based API for creating PDF documents with a fluid builder pattern.

Installation

go get github.com/GymITPro/gipdf

Features

  • Component-based document building
  • Headers and footers
  • Text fields with custom formatting
  • Data fields with labels
  • Image embedding
  • Signature fields
  • Custom fonts
  • Automatic page breaks
  • Layout management with rows and columns

Basic Usage

package main

import (
	"io/ioutil"
	"log"
	"os"

	"github.com/GymITPro/gipdf"
)

func main() {
	// Create a new document with configuration
	doc := gipdf.NewDocument(gipdf.Config{
		Padding: gipdf.PaddingLTRB(10, 10, 10, 10),
		Fonts: []*gipdf.ConfigFont{
			{
				Name:  "Helvetica",
				Style: "",
				Data:  nil, // Built-in font
			},
		},
	})

	// Add a header
	doc.AddHeader(gipdf.PaddingAll(5), 2, func(r *gipdf.Row) {
		r.TextField("Document Header", 1, 10, gipdf.AlignmentCenter, 
			gipdf.UseFontSize(14))
	})

	// Add content
	doc.Row(gipdf.PaddingAll(5), 2, func(r *gipdf.Row) {
		r.TextField("Hello World", 1, 10, gipdf.AlignmentLeft)
	})

	// Generate the PDF
	pdfBytes, err := doc.Render()
	if err != nil {
		log.Fatal(err)
	}

	// Save to file
	err = ioutil.WriteFile("output.pdf", pdfBytes, 0644)
	if err != nil {
		log.Fatal(err)
	}
}

Components

Document

The main container for your PDF content.

doc := gipdf.NewDocument(gipdf.Config{
    Padding: gipdf.PaddingAll(10),
})
Rows and Columns

Create layouts using rows (horizontal) and columns (vertical).

doc.Row(gipdf.PaddingAll(5), 2, func(r *gipdf.Row) {
    r.Column(gipdf.PaddingAll(2), 1, 0, 1, func(c *gipdf.Column) {
        // Column content
    })
})
Widgets

Add content to your PDF with various widgets:

TextField
row.TextField("This is text", 1, 10, gipdf.AlignmentLeft, 
    gipdf.UseFontSize(12),
    gipdf.UseColor(gipdf.ColorRGB(0, 0, 0)))
DataField
row.DataField("Label", "Value", 1, 8, 10)
Image
imageData, _ := ioutil.ReadFile("logo.png")
row.Image(imageData, "png", 1)
SignatureField
signatureData, _ := ioutil.ReadFile("signature.png")
row.SignatureField(signatureData, "png", "2022-01-01", "John Doe", 1, 10)
EmptyField
row.EmptyField(1)
Headers and Footers
// Add a standard header
doc.AddHeader(gipdf.PaddingAll(5), 2, func(r *gipdf.Row) {
    r.TextField("Document Header", 1, 10, gipdf.AlignmentCenter)
})

// Add a first page header (different from other pages)
doc.AddFirstPageHeader(gipdf.PaddingAll(5), 2, func(r *gipdf.Row) {
    r.TextField("First Page Header", 1, 10, gipdf.AlignmentCenter)
})

// Add a footer
doc.AddFooter(gipdf.PaddingAll(5), 2, func(r *gipdf.Row) {
    r.TextField("Page " + gipdf.CurrentPage() + " of " + gipdf.TotalPages(), 
        1, 10, gipdf.AlignmentCenter)
})
Styling

Apply styling to text using configuration functions:

row.TextField("Styled Text", 1, 10, gipdf.AlignmentLeft,
    gipdf.UseFont(gipdf.Font{Name: "Helvetica", Style: "B", Size: 14}),
    gipdf.UseColor(gipdf.ColorRGB(255, 0, 0)),
    gipdf.UseFillColor(gipdf.ColorRGB(240, 240, 240)))

Page Breaks

Add manual page breaks:

doc.PageBreak()

License

MIT

Documentation

Index

Constants

View Source
const AlignmentCenter = "C"
View Source
const AlignmentLeft = "L"
View Source
const AlignmentRight = "R"

Variables

This section is empty.

Functions

func GetHeight

func GetHeight(row *Row) float64

func UseColor

func UseColor(color Color) configFunc

func UseFillColor

func UseFillColor(color Color) configFunc

func UseFont

func UseFont(font Font) configFunc

func UseFontSize

func UseFontSize(size float64) configFunc

Types

type Color

type Color struct {
	Red   int `json:"red"`
	Green int `json:"green"`
	Blue  int `json:"blue"`
}

func ColorRGB

func ColorRGB(r, g, b int) Color

type Column

type Column struct {
	Rows        []Widget `json:"columns"`
	Padding     Padding  `json:"padding"`
	Height      float64  `json:"height"`
	Spacing     float64  `json:"spacing"`
	AspectRatio float64  `json:"aspect_ratio"`
	// contains filtered or unexported fields
}

func (*Column) DataField

func (r *Column) DataField(title, content string, aspectRatio, titleHeight, fieldHeight float64, configs ...configFunc) *Column

func (*Column) Done

func (r *Column) Done() *Document

func (*Column) EmptyField

func (r *Column) EmptyField(aspectRatio float64) *Column

func (*Column) GetAspectRatio

func (r *Column) GetAspectRatio() float64

func (*Column) Image

func (r *Column) Image(image []byte, imageType string, aspectRatio float64) *Column

func (*Column) Parent

func (r *Column) Parent() *Row

func (*Column) Render

func (r *Column) Render(pdf *Document, x, y, width, height float64)

func (*Column) Row

func (r *Column) Row(padding Padding, spacing float64, aspectRatio float64, builder func(*Row)) *Column

func (*Column) SignatureField

func (r *Column) SignatureField(image []byte, imageType, date, subtitle string, aspectRatio, textHeight float64, configs ...configFunc) *Column

func (*Column) TextField

func (r *Column) TextField(content string, aspectRatio, textHeight float64, alignment string, configs ...configFunc) *Column

type Config

type Config struct {
	Padding Padding       `json:"padding"`
	Fonts   []*ConfigFont `json:"fonts"`
}

type ConfigFont

type ConfigFont struct {
	Name    string   `json:"name"`
	Style   string   `json:"style"`
	Data    []byte   `json:"data"`
	Default *float64 `json:"default"`
}

type Document

type Document struct {
	Padding         Padding `json:"padding"`
	Rows            []*Row  `json:"rows"`
	FirstPageHeader []*Row  `json:"first_page_header"`
	Header          []*Row  `json:"header"`
	Footer          []*Row  `json:"footer"`

	*gofpdf.Fpdf
	// contains filtered or unexported fields
}

func NewDocument

func NewDocument(config Config) *Document

func (*Document) AddFirstPageHeader

func (d *Document) AddFirstPageHeader(padding Padding, spacing float64, builder func(*Row)) *Document

func (*Document) AddFooter

func (d *Document) AddFooter(padding Padding, spacing float64, builder func(*Row)) *Document

func (*Document) AddHeader

func (d *Document) AddHeader(padding Padding, spacing float64, builder func(*Row)) *Document

func (*Document) AddPage

func (d *Document) AddPage()

func (*Document) PageBreak

func (d *Document) PageBreak() *Document

func (*Document) Render

func (d *Document) Render() ([]byte, error)

func (*Document) Row

func (d *Document) Row(padding Padding, spacing float64, builder func(*Row)) *Document

type Font

type Font struct {
	Name  string  `json:"name"`
	Style string  `json:"style"`
	Size  float64 `json:"size"`
}
type Header struct {
	Title    string `json:"title"`
	SubTitle string `json:"sub_title"`
	Image    []byte `json:"image"`
}

type Padding

type Padding struct {
	Left   float64 `json:"left"`
	Top    float64 `json:"top"`
	Right  float64 `json:"right"`
	Bottom float64 `json:"bottom"`
}

func PaddingAll

func PaddingAll(i float64) Padding

func PaddingB

func PaddingB(i float64) Padding

func PaddingL

func PaddingL(i float64) Padding

func PaddingLTRB

func PaddingLTRB(l, t, r, b float64) Padding

func PaddingR

func PaddingR(i float64) Padding

func PaddingT

func PaddingT(i float64) Padding

type Row

type Row struct {
	Columns     []Widget `json:"columns"`
	Padding     Padding  `json:"padding"`
	Spacing     float64  `json:"spacing"`
	IsPageBreak bool     `json:"is_page_break"`
	AspectRatio float64  `json:"aspect_ratio"`
	// contains filtered or unexported fields
}

func (*Row) Column

func (r *Row) Column(padding Padding, spacing, height float64, aspectRatio float64, builder func(*Column)) *Row

func (*Row) DataField

func (r *Row) DataField(title, content string, aspectRatio, titleHeight, fieldHeight float64, configs ...configFunc) *Row

func (*Row) EmptyField

func (r *Row) EmptyField(aspectRatio float64) *Row

func (*Row) GetAspectRatio

func (r *Row) GetAspectRatio() float64

func (*Row) Image

func (r *Row) Image(image []byte, imageType string, aspectRatio float64) *Row

func (*Row) Parent

func (r *Row) Parent() *Column

func (*Row) Render

func (r *Row) Render(pdf *Document, x, y, width, height float64)

func (*Row) SignatureField

func (r *Row) SignatureField(image []byte, imageType, date, subtitle string, aspectRatio, textHeight float64, configs ...configFunc) *Row

func (*Row) TextField

func (r *Row) TextField(content string, aspectRatio, textHeight float64, alignment string, configs ...configFunc) *Row

type Widget

type Widget interface {
	Render(pdf *Document, x, y, width, height float64)
	GetAspectRatio() float64
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL