POST /v1/generate
POST /v1/generate
Generates a PDF or PNG file from HTML content. This endpoint uses Playwright to render the document with full CSS support.
Request
Section titled “Request”Headers
Section titled “Headers”| Header | Required | Description |
|---|---|---|
Authorization | Yes | Bearer gk_your_api_key |
Content-Type | Yes | application/json |
Accept | No | application/json for metadata, omit for raw file |
{ "html": "<!DOCTYPE html><html>...</html>", "format": "pdf", "options": { "width": 816, "height": 1056, "scale": 1 }}Parameters
Section titled “Parameters”| Parameter | Type | Required | Description |
|---|---|---|---|
html | string | Yes | HTML content to render |
format | string | Yes | Output format: "pdf" or "png" |
options | object | No | Rendering options |
options.width | number | No | Page width in pixels (default: 816 = 8.5in @ 96dpi) |
options.height | number | No | Page height in pixels (default: 1056 = 11in @ 96dpi) |
options.scale | number | No | Scale factor (0.1 to 3, default: 1) |
Response
Section titled “Response”Raw File Response (Default)
Section titled “Raw File Response (Default)”When no Accept: application/json header is provided, the response is the raw file:
Headers:
Content-Type: application/pdfContent-Disposition: attachment; filename="document-1705320000000.pdf"Content-Length: 45678Body: Binary PDF/PNG data
JSON Response
Section titled “JSON Response”When Accept: application/json is provided:
{ "url": "data:application/pdf;base64,JVBERi0xLjQK...", "format": "pdf", "size": 45678, "expiresAt": "2024-01-16T12:00:00.000Z"}| Field | Type | Description |
|---|---|---|
url | string | Base64 data URL of the generated file |
format | string | Output format (pdf or png) |
size | number | File size in bytes |
expiresAt | string | Expiration timestamp (24 hours) |
Error Responses
Section titled “Error Responses”400 Bad Request - Invalid parameters
{ "error": "Validation failed", "code": "VALIDATION_ERROR", "details": [ { "path": ["format"], "message": "Invalid enum value. Expected 'pdf' | 'png'" } ]}429 Too Many Requests - Monthly limit exceeded
{ "error": "Monthly PDF limit exceeded", "code": "MONTHLY_LIMIT_EXCEEDED", "limit": 100, "used": 100, "tier": "free", "upgrade": "https://glyph.you/pricing"}503 Service Unavailable - Playwright not available
{ "error": "PDF generation not available. Playwright is not installed.", "code": "PLAYWRIGHT_NOT_INSTALLED", "details": { "install": "bun add playwright && npx playwright install chromium" }}Code Examples
Section titled “Code Examples”cURL (Download PDF)
Section titled “cURL (Download PDF)”curl -X POST https://api.glyph.you/v1/generate \ -H "Authorization: Bearer gk_your_api_key" \ -H "Content-Type: application/json" \ -d '{ "html": "<!DOCTYPE html><html><body><h1>Hello World</h1></body></html>", "format": "pdf" }' \ --output document.pdfcURL (Get JSON metadata)
Section titled “cURL (Get JSON metadata)”curl -X POST https://api.glyph.you/v1/generate \ -H "Authorization: Bearer gk_your_api_key" \ -H "Content-Type: application/json" \ -H "Accept: application/json" \ -d '{ "html": "<!DOCTYPE html><html><body><h1>Hello World</h1></body></html>", "format": "pdf" }'JavaScript (Browser Download)
Section titled “JavaScript (Browser Download)”const response = await fetch('https://api.glyph.you/v1/generate', { method: 'POST', headers: { 'Authorization': 'Bearer gk_your_api_key', 'Content-Type': 'application/json' }, body: JSON.stringify({ html: document.querySelector('glyph-editor').getHtml(), format: 'pdf' })});
// Create download linkconst blob = await response.blob();const url = URL.createObjectURL(blob);const a = document.createElement('a');a.href = url;a.download = 'quote.pdf';a.click();URL.revokeObjectURL(url);JavaScript (Node.js - Save to File)
Section titled “JavaScript (Node.js - Save to File)”import { writeFileSync } from 'fs';
const response = await fetch('https://api.glyph.you/v1/generate', { method: 'POST', headers: { 'Authorization': 'Bearer gk_your_api_key', 'Content-Type': 'application/json' }, body: JSON.stringify({ html: htmlContent, format: 'pdf' })});
const buffer = Buffer.from(await response.arrayBuffer());writeFileSync('document.pdf', buffer);Python
Section titled “Python”import requests
response = requests.post( 'https://api.glyph.you/v1/generate', headers={ 'Authorization': 'Bearer gk_your_api_key', 'Content-Type': 'application/json' }, json={ 'html': html_content, 'format': 'pdf' })
# Save to filewith open('document.pdf', 'wb') as f: f.write(response.content)Page Sizes
Section titled “Page Sizes”Common page sizes in pixels (at 96 DPI):
| Size | Width | Height |
|---|---|---|
| Letter (8.5” x 11”) | 816 | 1056 |
| A4 (210mm x 297mm) | 794 | 1123 |
| Legal (8.5” x 14”) | 816 | 1344 |
| Tabloid (11” x 17”) | 1056 | 1632 |
Custom Sizes
Section titled “Custom Sizes”{ "html": "...", "format": "pdf", "options": { "width": 794, "height": 1123 }}PNG Generation
Section titled “PNG Generation”For image output:
{ "html": "...", "format": "png", "options": { "scale": 2 }}Best Practices
Section titled “Best Practices”- Use session-based workflow - Generate from a session to ensure you have the latest modifications
- Handle large files - PDFs can be several MB; use streaming for large documents
- Cache when possible - Store generated PDFs to avoid regenerating unchanged documents
- Monitor usage - Check your monthly limit headers to avoid interruptions