CoolFace
Apppublic

fmard/pdf-api

sourceHugging Faceupdated 4mo agoView on Hugging Face
0likes
App README

PDF Generator API

Convert HTML to PDF using headless Chromium. Supports raw HTML, Jinja2 templates, and custom page options.

API Endpoints

POST /api/generate — Generate PDF from HTML

Request body (JSON):

json
{
  "html": "<html><body><h1>Hello World</h1></body></html>",
  "options": {
    "format": "A4",
    "landscape": false,
    "margin": { "top": "20mm", "right": "20mm", "bottom": "20mm", "left": "20mm" },
    "scale": 1.0,
    "printBackground": true
  }
}

Using a built-in template:

json
{
  "template": "invoice",
  "data": {
    "company_name": "Acme Inc.",
    "client_name": "John Doe",
    "invoice_number": "INV-001",
    "date": "2024-01-15",
    "due_date": "2024-02-15",
    "items": [
      { "description": "Web Development", "quantity": 1, "price": 5000 }
    ],
    "tax_rate": 10
  }
}

Response: PDF binary file (Content-Type: application/pdf)

GET /api/health — Health check

Returns: {"status": "healthy", "service": "pdf-generator-api"}

GET /api/templates — List built-in templates

Returns: {"templates": ["invoice", "receipt", "report", "resume"]}

Built-in Templates

TemplateDescription
invoiceProfessional invoice with line items, tax, and totals
receiptSimple point-of-sale receipt
reportDocument report with sections
resumeClean resume/CV layout

PDF Options

OptionTypeDefaultDescription
formatstring"A4"Paper size: A3, A4, A5, Letter, Legal, Tabloid
landscapeboolfalsePage orientation
marginobject20mm allMargins (top, right, bottom, left)
scalefloat1.0Scale factor (0.1 - 2.0)
printBackgroundbooltrueInclude CSS background colors/images
headerTemplatestring""Custom header HTML
footerTemplatestring""Custom footer HTML

Rate Limits

  • —100 requests per hour per IP
  • —Maximum HTML size: 5MB
  • —Generation timeout: 30 seconds

Deployment

This project is configured for deployment on HuggingFace Spaces using Docker. Simply push to your HF Space repository and it will build and deploy automatically.

Local Development

bash
pip install -r requirements.txt
playwright install chromium
uvicorn main:app --reload --port 7860