How to Have Your AI Agent Generate PDFs Automatically
AI agents are increasingly responsible for generating structured documents — invoices, reports, summaries — and delivering them to end users. The problem: most LLM outputs are plain text or Markdown, not production-ready PDFs.
In this guide we'll wire up an AI agent (using any orchestration framework) to call the AgentGen API and produce a pixel-perfect PDF with a single HTTP request.
Why HTML → PDF?
HTML is the perfect intermediate format. Your agent can generate HTML using a template, inject dynamic data (client name, totals, line items), and hand it off to a headless-browser renderer. The output is identical to what a designer would produce in Word or InDesign — without any proprietary tooling.
Step 1: Get an API key
Sign up at agent-gen.com, buy a token pack (PDF generation costs 2 tokens per page), and copy your API key from the dashboard.
Step 2: Build your HTML template
const invoiceHtml = (data) => `
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: 'Helvetica Neue', sans-serif; padding: 40px; color: #111; }
.header { display: flex; justify-content: space-between; margin-bottom: 40px; }
.total { font-size: 1.5rem; font-weight: bold; color: #4f46e5; }
</style>
</head>
<body>
<div class="header">
<h1>Invoice #${data.number}</h1>
<p>${data.date}</p>
</div>
<p>Bill to: <strong>${data.client}</strong></p>
<!-- line items table -->
<p class="total">Total: ${data.total}</p>
</body>
</html>
Step 3: Call the API from your agent tool
async function generateInvoicePdf(data) {
const res = await fetch('https://www.agent-gen.com/api/v1/generate/pdf', {
method: 'POST',
headers: {
'X-API-Key': process.env.AGENTGEN_API_KEY,
'Content-Type': 'application/json',
},
body: JSON.stringify({
html: invoiceHtml(data),
format: 'A4',
margin: { top: '20mm', bottom: '20mm', left: '15mm', right: '15mm' },
}),
})
const { url } = await res.json()
return url // ready-to-download PDF URL
}
Step 4: Register as an agent tool
Register generateInvoicePdf as a tool in your agent framework. When the LLM decides to create an invoice, it calls the tool with structured arguments and gets back a URL to return to the user.
Token cost
A one-page invoice costs 2 tokens. At the Growth tier ($39 for 2,500 tokens) that's roughly 1,250 invoices for $39 — far cheaper than any SaaS invoice generator.
Ready to start? Create a free account and generate your first PDF in minutes.
Ready to start generating?
Create a free account and generate your first PDF or image in minutes.
Get started free