Examples¶
Each example is a standalone Go module in the examples/ directory with its own dependencies and local templates.
Running Examples¶
- Start zipreport-server (required for PDF generation):
- Navigate to an example directory and run:
Available Examples¶
simple¶
Basic PDF generation from a template.
Demonstrates:
- Loading a template from directory
- Passing data to templates
- Rendering to PDF
newsletter¶
MIME email generation with embedded images.
Demonstrates:
- MIMEReport for email generation
- Embedded images and styling
- No server required
filter_example¶
Dynamic image generation using filters.
Demonstrates:
- Using
pngfilter for dynamic charts - Chart generation with gonum/plot
- Passing data to image generators
print_css_chrome¶
CSS Paged Media for print layouts.
Demonstrates:
- CSS
@pagerules - Page breaks and headers/footers
- Print-specific styling
pagedjs¶
PagedJS for advanced print layouts with dynamic charts.
Demonstrates:
- PagedJS library integration
- JavaScript event waiting
- Complex page layouts
analytics_dashboard¶
Dashboard with multiple charts and data visualization.
Demonstrates:
- Multiple chart types
- Grid layouts
- Data-driven visualizations
executive_report¶
Professional business report with sections.
Demonstrates:
- Multi-section reports
- Tables and data presentation
- Professional styling
product_brochure¶
Marketing brochure with product showcase.
Demonstrates:
- Product layouts
- Image handling
- Marketing-style design
Example Structure¶
Each example follows this structure:
example-name/
├── go.mod # Module definition with replace directive
├── go.sum # Dependencies
├── main.go # Example code
├── README.md # Usage instructions
└── template/ # Local template files
├── manifest.json
├── index.html
└── ...
go.mod with Local Development¶
module github.com/zipreport/zipreport-go/examples/simple
go 1.24
require github.com/zipreport/zipreport-go v1.0.0
replace github.com/zipreport/zipreport-go => ../..
Creating Your Own Example¶
- Create a new directory in
examples/:
- Initialize the module:
- Add replace directive to
go.mod:
- Create template directory:
- Add template files:
# manifest.json
echo '{
"title": "My Example",
"author": "Me",
"version": "1.0.0",
"description": "My example report",
"params": ["name"]
}' > template/manifest.json
# index.html
echo '<h1>Hello {{ name }}</h1>' > template/index.html
- Create main.go:
package main
import (
"fmt"
"os"
"github.com/zipreport/zipreport-go/pkg/api"
"github.com/zipreport/zipreport-go/pkg/report"
)
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: go run main.go <output.pdf>")
os.Exit(1)
}
zpt, err := report.LoadDir("template")
if err != nil {
fmt.Printf("Failed to load template: %v\n", err)
os.Exit(1)
}
data := map[string]interface{}{
"name": "World",
}
serverURL := os.Getenv("ZIPREPORT_URL")
if serverURL == "" {
serverURL = "http://localhost:6543"
}
client := api.NewZipReport(serverURL, "")
result := client.RenderDefaults(zpt, data, nil)
if !result.Success {
fmt.Printf("Render failed: %s\n", result.Error)
os.Exit(1)
}
if err := os.WriteFile(os.Args[1], result.Report, 0644); err != nil {
fmt.Printf("Failed to save PDF: %v\n", err)
os.Exit(1)
}
fmt.Printf("PDF saved to %s\n", os.Args[1])
}
- Run:
Output Directory¶
Example outputs are saved to the output/ directory in the project root: