Inkspan › Guides › How to generate a PDF from a template in Laravel
Call the Inkspan /v1/generate/template endpoint from Laravel to generate a PDF from a template — no headless browser,
font stack, or rendering infrastructure in your deploy. Keep your API key in the environment and POST.
Laravel's HTTP client returns the raw body via ->body(). Store it with the Storage facade or return it as a download response.
// app/Http/Controllers/PdfController.php
use Illuminate\Support\Facades\Http;
$res = Http::withHeaders(['X-API-Key' => config('services.inkspan.key')])
->post('https://api.inkspan.dev/v1/generate/template', {"template": "<h1>{{ name }}</h1>", "data": {"name": "Ada"}});
$res->throw();
return response($res->body(), 200)
->header('Content-Type', 'application/pdf');
| Name | Type | Required | Description |
|---|---|---|---|
template | string | yes | A Jinja2 template. Supports {{ variables }}, {% for %} loops, {% if %} conditionals, and filters. |
data | object | yes | JSON object whose top-level keys become template variables. |
filename | string | no | Filename in the response's Content-Disposition. |
Returns a PDF as a binary stream with Content-Type: application/pdf and Content-Disposition: attachment; filename=document.pdf. Write the response body straight to disk — do not parse it as JSON.
Put INKSPAN_KEY in .env and read it with env() inside a config file so it survives config:cache.
{% for i in items %}…{% endfor %}, not {{#each}}.data become variables. Passing {"data": {"invoice": {...}}} means you reference {{ invoice.number }}, not {{ number }}.| Status | Meaning | Fix |
|---|---|---|
401 | Invalid or missing API key | Send your key in the X-API-Key header. |
402 | Monthly quota exceeded | The response reports used/quota. Upgrade at inkspan.dev/#pricing. |
422 | Missing or invalid parameter | Check required fields in the table above — the body names the offending field. |
Failed requests do not count against your quota; only successful ones are metered.
Get a free API key →Same operation in cURL · Python · Node.js · Other frameworks: Next.js · Django · Ruby on Rails · PHP · All guides