Skip to main content
The Notice PDF Generation Service is a Node.js service that generates PDFs from HTML templates for notices. It runs alongside the Django application.

Features

  • Converts HTML to PDF using Puppeteer
  • Supports custom templates for different notice types
  • Runs as a service alongside the Django application
  • Integrated into Docker for production deployments

Setup

Install Dependencies

cd backend/notice_service
npm install

Environment Configuration

Copy the example environment file:
cp env.example .env

Environment Variables

VariableDefaultDescription
DJANGO_ENVdevelopmentEnvironment name
NOTICE_SERVICE_PORT9229Port for the service
LOG_LEVELinfoLogging level (error, warn, info, debug)
CHROMIUM_PATH/usr/bin/chromiumPath to Chromium executable
PUPPETEER_MAX_CONCURRENCY5Maximum concurrent Puppeteer instances

Running the Service

Production

npm run start

Development (with hot reloading)

npm run dev

API Endpoints

Health Check

GET /health
Returns the health status of the service.

Convert to PDF

POST /convert-to-pdf
Converts HTML content to PDF. Request Body:
{
  "marked_up_context": {
    // Template-specific context data
  },
  "customer": "customer_name",
  "template_type": "notice_detail",
  "event_type": "capital_call"
}
Response:
  • Content-Type: application/pdf
  • Body: PDF file buffer

Templates

Templates are located in src/templates. Each customer can have custom templates.

Directory Structure

src/templates/
├── default/
│   └── notice_detail.tsx
├── customer_a/
│   └── notice_detail.tsx
└── customer_b/
    └── notice_detail.tsx

Adding a New Template

  1. Create a directory under src/templates/{customer_name}
  2. Add your React component for the template
  3. Add any associated CSS files
  4. Update the template registry in src/utils/generateTemplatePdf.ts

Configuration

All configuration is centralized in config.ts. The service exports helper functions:
import { isDev, isDeployed, config } from './config';

// Check environment
if (isDev()) {
  // Development-specific logic
}

if (isDeployed()) {
  // Staging, sandbox, or production logic
}

// Access full config
console.log(config.port);

Docker Integration

The service is integrated into the Django Docker container and runs alongside the main application. It starts automatically when the container starts.

Testing

Generate Sample PDF

npm run generate
This generates a sample PDF in the current directory for verification.

Manual Testing

  1. Start the service: npm run dev
  2. Send a test request:
curl -X POST http://localhost:9229/convert-to-pdf \
  -H "Content-Type: application/json" \
  -d '{"customer": "default", "template_type": "notice_detail", "event_type": "capital_call", "marked_up_context": {}}' \
  --output test.pdf

Troubleshooting

Puppeteer Issues

If Puppeteer fails to launch:
  1. Ensure Chromium is installed
  2. Check CHROMIUM_PATH environment variable
  3. On Linux, install required dependencies:
apt-get install -y chromium-browser

Memory Issues

If running out of memory with many concurrent requests:
  1. Reduce PUPPETEER_MAX_CONCURRENCY
  2. Increase container memory limits