@gaumala/http-pdf-printer
v0.1.0
Published
HTTP server for generating PDFs from HTML using Puppeteer
Downloads
3
Readme
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
npm install @gaumala/http-pdf-printerQuick Start
Running the Sample
The project includes a sample application that demonstrates basic usage. First, clone the repository:
git clone <repository-url>
cd http-pdf-printer
npm installThen run the sample:
node sample/app.jsThe 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:
curl -X PUT http://localhost:12480/print \
-H "Content-Type: application/json" \
-d '{"name": "John"}' \
--output output.pdfThe server will:
- Generate HTML from your JSON data using the
generateHTMLfunction - Register the HTML document in the doc server
- Use Puppeteer to render the HTML and generate a PDF
- Return the PDF as the response
- 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
generateHTMLfunction)
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) or503 Service Unavailable(unhealthy) - Content-Type:
application/json - Body:
{"status": "healthy", "timestamp": "..."}or{"status": "unhealthy", "error": "...", "timestamp": "..."}
Usage as a Library
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 stringstaticDir(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.jsonLicense
MIT
