/simple-url-to-pdf

nodejs+puppeteer+express / no-nonsense

Primary LanguageJavaScript

PDF Generation API

Deploy

A service that generates PDFs from web pages using Puppeteer.

Endpoints

Generate PDF

Convert a webpage to PDF format.

POST /api/render

{
  "url": "https://example.com",
  "goto": {
    // Optional Puppeteer GoTo options
    // See: https://pptr.dev/api/puppeteer.gotooptions
  },
  "pdf": {
    // Optional PDF generation options
    // See: https://pptr.dev/api/puppeteer.pdfoptions
  }
}

GET /api/render

/api/render?url=https://example.com

Security Rules

URL Restrictions

  • URLs must be valid and properly formatted
  • If ALLOWED_PREFIXES environment variable is set, URLs must start with one of the allowed prefixes
  • If ALLOWED_PREFIXES is empty, all valid URLs are allowed

CORS

  • CORS restrictions are enforced based on the ALLOWED_CORS environment variable
  • If ALLOWED_CORS is empty, all origins are allowed
  • Origins must match exactly with the whitelist entries

Response

Success

  • Content-Type: application/pdf
  • Body: PDF file buffer

Errors

400 Bad Request

{
  "status": "error",
  "message": "Invalid URL"
}

403 Forbidden

{
  "status": "error",
  "message": "This URL is not allowed"
}

Environment Variables

Variable Description Format
ALLOWED_PREFIXES Comma-separated list of allowed URL prefixes prefix1, prefix2, ...
ALLOWED_CORS Comma-separated list of allowed CORS origins origin1, origin2, ...

Examples

Basic PDF Generation

curl -X POST https://pdf-api.mass.build/api/render \
  -H "Content-Type: application/json" \
  -d '{"url": "https://example.com"}'

With Custom PDF Options

curl -X POST https://pdf-api.mass.build/api/render \
  -H "Content-Type: application/json" \
  -d '{
    "url": "https://example.com",
    "pdf": {
      "format": "A4",
      "printBackground": true
    }
  }'

Using GET Request

curl https://pdf-api.mass.build/api/render?url=https://example.com