Generate PDFs in Laravel
Generate PDFs from HTML, Markdown, URLs, or built-in templates in Laravel — using the built-in Http facade and the RenderPDFs REST API.
1. Install
RenderPDFs uses a plain REST API — no SDK required. For Laravel, install the dependency below and grab your API key from renderpdfs.com/signup (free, 100 PDFs/month, no credit card).
# Laravel ships with Http (Guzzle under the hood) — no install needed
# Just add your key to .env:
echo 'RENDERPDFS_KEY=rpdf_your_key' >> .env2. Convert HTML to PDF
The simplest case: send an HTML string, get back a PDF binary. Anything Chromium renders works — Flexbox, Grid, web fonts, SVG, JavaScript.
use Illuminate\Support\Facades\Http;
$response = Http::withHeaders([
'X-API-Key' => config('services.renderpdfs.key'),
])
->post('https://api.renderpdfs.com/v1/generate', [
'html' => '<h1>Invoice #' . $invoice->id . '</h1>',
]);
return response($response->body(), 200, [
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'attachment; filename="invoice.pdf"',
]);3. Convert a URL to PDF
Pass url instead of html and RenderPDFs fetches the page, waits for JS, and snapshots it. Useful for archiving dashboards, public pages, or invoices served from your app.
$html = view('invoices.show', ['invoice' => $invoice])->render();
$pdf = Http::withHeaders(['X-API-Key' => config('services.renderpdfs.key')])
->post('https://api.renderpdfs.com/v1/generate', [
'html' => $html,
'options' => ['format' => 'A4', 'printBackground' => true],
])
->body();
Storage::disk('s3')->put("invoices/{$invoice->id}.pdf", $pdf);4. Use a built-in template
Skip the design work. RenderPDFs ships six battle-tested templates — invoice, receipt, report, contract, certificate, offer_letter. Send JSON, get a styled PDF.
$pdf = Http::withHeaders(['X-API-Key' => config('services.renderpdfs.key')])
->post('https://api.renderpdfs.com/v1/generate', [
'template' => 'invoice',
'data' => [
'company' => $user->company,
'invoice_number' => $invoice->number,
'items' => $invoice->items->toArray(),
],
])
->body();5. Custom paper, margins, headers
Control page format, orientation, margins, and running headers/footers via the options object. All standard Chromium PDF settings are supported.
// In your Mailable's build() method:
public function build()
{
$pdf = Http::withHeaders(['X-API-Key' => config('services.renderpdfs.key')])
->post('https://api.renderpdfs.com/v1/generate', [
'template' => 'invoice',
'data' => $this->invoice->toArray(),
])
->body();
return $this->subject('Your invoice')
->view('emails.invoice')
->attachData($pdf, "invoice-{$this->invoice->number}.pdf", [
'mime' => 'application/pdf',
]);
}6. Store the PDF and share a link
For emailable links or webhook payloads, set store: true in the body. The response becomes { url, expires_in } — the PDF is hosted on our CDN for 24 hours by default.
// app/Jobs/GenerateInvoicePdf.php
public function handle(): void
{
$result = Http::withHeaders(['X-API-Key' => config('services.renderpdfs.key')])
->post('https://api.renderpdfs.com/v1/generate', [
'template' => 'invoice',
'data' => $this->invoice->toArray(),
'store' => true, // returns { url, expires_in }
])
->json();
$this->invoice->update(['pdf_url' => $result['url']]);
}7. Notes & gotchas
Every request needs an X-API-Key header. Grab a free key at renderpdfs.com/signup — 100 PDFs/month, no credit card. Treat the key like a password: keep it server-side, never expose it in browser code.
Add a `services.renderpdfs.key` entry in `config/services.php` mapped to `env('RENDERPDFS_KEY')`, then reference it as `config('services.renderpdfs.key')`. Keeps env reads cached and out of your runtime code.
Non-2xx responses return JSON: { error: string }. The most common cases are 401 (bad API key), 402 (over plan quota), and 422 (invalid HTML or URL). Always parse the error body before retrying.
Other languages
Generate your first PDF in 60 seconds
100 free PDFs per month. No credit card. HTML, Markdown, URLs, templates, MCP — all included.