npm package: @gaumala/http-pdf-printer
Description: HTTP server for generating PDFs from HTML using Puppeteer
Latest version: 0.1.0

# HTTP PDF Printer

A Node.js library that provides an HTTP server for generating PDFs from HTML. It uses Puppeteer to render HTML documents and serves them as PDFs via a REST API.

## Features

- Generate PDFs from HTML via HTTP API
- Configurable HTML generation function
- Static file serving for CSS, images, fonts, etc.
- Scheduled automatic browser restarts (configurable via cron schedule)
- Health check endpoint
- Document-based architecture for efficient PDF generation

## Installation

```bash
npm install @gaumala/http-pdf-printer
```

## Quick Start

### Running the Sample

The project includes a sample application that demonstrates basic usage. First, clone the repository:

```bash
git clone <repository-url>
cd http-pdf-printer
npm install
```

Then run the sample:

```bash
node sample/app.js
```

The sample will start the server and use `sample/static/` as the static directory for CSS and other assets.

This will start the server on `http://localhost:12480` with the doc server on `http://localhost:12481`.

### Generating a PDF with curl

Once the server is running, you can generate a PDF by sending a PUT request to the `/print` endpoint:

```bash
curl -X PUT http://localhost:12480/print \
  -H "Content-Type: application/json" \
  -d '{"name": "John"}' \
  --output output.pdf
```

The server will:
1. Generate HTML from your JSON data using the `generateHTML` function
2. Register the HTML document in the doc server
3. Use Puppeteer to render the HTML and generate a PDF
4. Return the PDF as the response
5. Clean up the HTML document

## API Endpoints

### `PUT /print`
Generates a PDF from JSON data.

**Request:**
- Method: `PUT`
- Content-Type: `application/json`
- Body: JSON object (structure depends on your `generateHTML` function)

**Response:**
- Content-Type: `application/pdf`
- Body: PDF file

### `PUT /restart`
Manually restarts the Puppeteer browser instance.

**Request:**
- Method: `PUT`

**Response:**
- Status: `200 OK`

### `GET /health`
Health check endpoint.

**Request:**
- Method: `GET`

**Response:**
- Status: `200 OK` (healthy) or `503 Service Unavailable` (unhealthy)
- Content-Type: `application/json`
- Body: `{"status": "healthy", "timestamp": "..."}` or `{"status": "unhealthy", "error": "...", "timestamp": "..."}`

## Usage as a Library

```javascript
import { runServer } from "@gaumala/http-pdf-printer";

const generateHTML = async (data) => {
  // Your HTML generation logic here
  return `<html>...</html>`;
};

const stop = await runServer({
  generateHTML,
  staticDir: "/path/to/static/files",
  port: 12480, // optional, defaults to 12480
  host: "localhost", // optional, defaults to "localhost"
  restartSchedule: "0 0 * * *", // optional, defaults to daily at midnight
});

// Later, to stop the server:
stop();
```

### Options

- `generateHTML` (required): Async function that takes JSON data and returns HTML string
- `staticDir` (required): Absolute path to directory containing static files (CSS, images, fonts, etc.)
- `port` (optional): Port number for the main server (default: `12480`)
- `host` (optional): Hostname for the server (default: `"localhost"`)
- `restartSchedule` (optional): Cron schedule for automatic browser restarts (default: `"0 0 * * *"` - daily at midnight)

## Project Structure

```
http-pdf-printer/
├── src/
│   ├── index.js          # Main library entry point
│   ├── doc-server.js     # Document server for HTML storage
│   ├── http.js           # HTTP utilities
│   └── pdf-printer.js    # Puppeteer PDF generation
├── sample/
│   ├── app.js            # Sample application
│   └── static/
│       └── style.css     # Sample CSS file
└── package.json
```

## License

MIT