An HTML-to-PDF conversion service written in Go.
- HTML/CSS parsing with sanitization and validation
- CSS layout engine with box model and text flow calculations
- PDF generation using gofpdf library
- HTTP API server and background worker processes
- In-memory caching with configurable TTL
- HTML sanitization and input validation
- Health checks and structured logging
- Configurable worker pools and rate limiting
print-service/ ├── cmd/ │ ├── server/ # HTTP server entry point │ └── worker/ # Background worker entry point ├── internal/ │ ├── api/ # HTTP API layer │ ├── core/ # Business logic │ │ ├── domain/ # Domain types and errors │ │ ├── engine/ # HTML/CSS/Layout engines │ │ └── services/ # Core services │ ├── infrastructure/ # External dependencies │ └── pkg/ # Shared utilities ├── configs/ # Configuration files └── assets/ # Static assets - Go 1.21 or higher
- Optional: Redis for production caching/queuing
- Clone the repository:
git clone <repository-url> cd print-service-go- Install dependencies:
go mod download- Run the development server:
go run cmd/server/main.go- Run the background worker:
go run cmd/worker/main.gocurl -X POST http://localhost:8080/api/v1/print \ -H "Content-Type: application/json" \ -d '{ "content": "<html><body><h1>Hello World</h1></body></html>", "content_type": "html", "options": { "page": { "size": {"width": 210, "height": 297, "name": "A4"}, "orientation": "portrait", "margins": {"top": 20, "right": 20, "bottom": 20, "left": 20} }, "output": { "format": "pdf" } } }'curl http://localhost:8080/api/v1/print/{job_id}curl http://localhost:8080/api/v1/print/{job_id}/download -o output.pdfThe service uses YAML configuration files:
configs/development.yaml- Development settingsconfigs/production.yaml- Production settings
Key configuration sections:
- Server: HTTP server settings (port, timeouts, TLS)
- Worker: Background worker pool configuration
- Print: Document processing limits and paths
- Cache: Caching strategy and limits
- Logger: Logging configuration
GET /health- Service health checkGET /ready- Service readiness checkGET /metrics- Service metrics
POST /api/v1/print- Submit print jobGET /api/v1/print/{id}- Get job statusDELETE /api/v1/print/{id}- Cancel jobGET /api/v1/print/{id}/download- Download resultGET /api/v1/jobs- List all jobs
- Parser: Converts HTML to DOM tree
- Sanitizer: Removes dangerous content for security
- Validator: Ensures HTML structure integrity
- Parser: Parses CSS rules and selectors
- Selector: Matches CSS rules to DOM elements
- Cascade: Applies CSS cascade and inheritance
- Box Calculator: Implements CSS box model
- Text Engine: Handles text layout and line breaking
- Flow Engine: Manages document flow (block, inline, flex)
- Page Breaker: Calculates optimal page breaks
- Print Service: Orchestrates the print pipeline
- Cache Service: Manages document and result caching
- Queue Service: Handles job queuing and processing
- Storage Service: Manages file storage operations
- HTML sanitization with configurable allowed tags/attributes
- Domain whitelist/blacklist for external resources
- Input validation and size limits
- Rate limiting for API endpoints
- Secure file handling
- Caching with TTL
- Worker pools for concurrent processing
- Resource limits and timeouts
- Memory management and cleanup
- Rendering pipeline optimization
- Structured JSON logging
- Metrics collection
- Health and readiness checks
- Request tracing and correlation IDs
- Performance monitoring
go test ./...# Build server go build -o bin/server cmd/server/main.go # Build worker go build -o bin/worker cmd/worker/main.go# Build image docker build -t print-service . # Run container docker run -p 8080:8080 print-service- Configure production settings in
configs/production.yaml - Set up Redis for caching and queuing
- Configure TLS certificates
- Set up monitoring and alerting
- Deploy with container orchestration (Kubernetes, Docker Swarm)
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
For support and questions, please open an issue in the GitHub repository.