Skip to content

A high-performance, containerized PDF generation service built with Ruby and Puppeteer. This microservice provides a simple HTTP API for converting HTML to PDF with extensive customization options.

License

Notifications You must be signed in to change notification settings

rebelcolony/PupPDF

Repository files navigation

PDF Generator Service

A high-performance, containerized PDF generation service built with Ruby and Puppeteer. This microservice provides a simple HTTP API for converting HTML to PDF with extensive customization options.

Features

  • πŸš€ Fast PDF generation using Puppeteer and Grover
  • πŸ“¦ Containerized with Docker for easy deployment
  • πŸ”§ Highly configurable page settings and margins
  • πŸ›‘οΈ Token-based authentication
  • πŸ“Š Production-ready with health checks
  • 🎯 Kamal deployment configuration included

Architecture

This service uses:

  • Sinatra - Lightweight web framework
  • Grover - Ruby wrapper around Puppeteer for PDF generation
  • Puppeteer - Headless Chrome for rendering
  • Puma - High-performance web server
  • Docker - Containerization
  • Kamal - Zero-downtime deployment

Prerequisites

For local development:

  • Ruby v3.3.5 (exact version needed)
  • Node.js (for Puppeteer)
  • Redis (optional, for caching)

macOS Setup

# Install Ruby version manager
brew install rbenv ruby-build

# Initialize rbenv
rbenv init

# Install Ruby 3.3.5
rbenv install 3.3.5
rbenv local 3.3.5

# Install dependencies
gem install bundler
bundle install
npm install

# Additional tools
brew install poppler
brew install redis
brew services start redis

# For ARM processors (M1/M2 Macs)
softwareupdate --install-rosetta

Installation

  1. Clone the repository:
git clone https://github.com/rebelcolony/PupPDF.git
cd PupPDF
  1. Create environment file:
cp .env.example .env
# Edit .env with your configuration
  1. Install dependencies:
bundle install
npm install

Usage

Running Locally

# Start the server
ruby app.rb

# Or use the Procfile
heroku local

The service will be available at http://localhost:4567

API Documentation

Generate PDF

POST /generate

Headers:

  • X-Authentication-Token: Your secret token
  • Content-Type: application/x-www-form-urlencoded

Parameters:

  • html (required): HTML content to convert
  • options (optional): JSON string with PDF options

Example:

curl -X POST http://localhost:4567/generate \
  -H "X-Authentication-Token: your-secret-token" \
  -d "html=<h1>Hello World</h1>" \
  -d 'options={"format":"A4","margin":{"top":"20px"}}' \
  --output document.pdf

Available Options:

{
  "format": "A4|LETTER|LEGAL",
  "margin": {
    "top": "20px",
    "bottom": "20px",
    "left": "20px",
    "right": "20px"
  },
  "displayHeaderFooter": true,
  "headerTemplate": "<div>Header</div>",
  "footerTemplate": "<div>Footer</div>",
  "printBackground": true,
  "landscape": false,
  "scale": 1.0,
  "pageRanges": "1-5",
  "preferCSSPageSize": true
}

Health Check

GET /healthz

Returns 200 OK if service is healthy.

Docker

Build and Run

# Build image
docker build -t pdf-generator .

# Run container
docker run -p 4567:4567 -e SECRET_TOKEN=your-token pdf-generator

Docker Compose

version: '3.8'
services:
  pdf-generator:
    build: .
    ports:
      - "4567:4567"
    environment:
      - SECRET_TOKEN=your-secret-token

Deployment

This project includes Kamal configuration for easy deployment:

# Setup Kamal
kamal setup

# Deploy
kamal deploy

Configuration

Environment Variables

Variable Description Required
SECRET_TOKEN Authentication token for API access Yes
PORT Port to run the service on (default: 4567) No
RACK_ENV Environment (production/development) No

Grover Configuration

Default PDF settings can be modified in config/grover.rb.

Contributing

We welcome contributions! Please see our Contributing Guidelines for details.

Testing

# Run tests
bundle exec rspec

# Run with coverage
COVERAGE=true bundle exec rspec

Performance

The service can handle approximately:

  • 50-100 concurrent PDF generations (depending on complexity)
  • Average response time: 1-3 seconds for standard documents
  • Memory usage: ~200MB per worker

Security

  • Token-based authentication
  • No data persistence - all processing is stateless
  • Runs Puppeteer with --no-sandbox flag in container (secure in Docker context)
  • Input sanitization for HTML content

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

  • Grover for the excellent Puppeteer wrapper
  • Puppeteer for headless Chrome automation
  • Sinatra for the lightweight web framework

Support

For issues and feature requests, please use the GitHub issue tracker.

About

A high-performance, containerized PDF generation service built with Ruby and Puppeteer. This microservice provides a simple HTTP API for converting HTML to PDF with extensive customization options.

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published