RenderPDFs/Tutorials/PHP
📘 Quickstart · 5 minutes

Generate PDFs in PHP

Generate PDFs from HTML, Markdown, URLs or templates in PHP — using cURL or Guzzle and the RenderPDFs REST API.

1. Install

RenderPDFs uses a plain REST API — no SDK required. For PHP, install the dependency below and grab your API key from renderpdfs.com/signup (free, 100 PDFs/month, no credit card).

Install (Guzzle recommended)bash
composer require guzzlehttp/guzzle

2. 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.

HTML → PDF (Guzzle)php
<?php
use GuzzleHttp\Client;

$client = new Client();

$response = $client->post('https://api.renderpdfs.com/v1/generate', [
    'headers' => [
        'X-API-Key'    => getenv('RENDERPDFS_KEY'),
        'Content-Type' => 'application/json',
    ],
    'json' => [
        'html' => '<h1>Hello PDF</h1><p>Generated from PHP</p>',
    ],
]);

file_put_contents('hello.pdf', (string) $response->getBody());

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.

URL → PDF (plain cURL)php
<?php
$ch = curl_init('https://api.renderpdfs.com/v1/generate');
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST           => true,
    CURLOPT_HTTPHEADER     => [
        'X-API-Key: ' . getenv('RENDERPDFS_KEY'),
        'Content-Type: application/json',
    ],
    CURLOPT_POSTFIELDS => json_encode([
        'url'     => 'https://example.com',
        'options' => ['format' => 'A4', 'printBackground' => true],
    ]),
]);

$pdf = curl_exec($ch);
curl_close($ch);

file_put_contents('page.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.

Built-in invoice templatephp
$response = $client->post('https://api.renderpdfs.com/v1/generate', [
    'headers' => ['X-API-Key' => getenv('RENDERPDFS_KEY')],
    'json' => [
        'template' => 'invoice',
        'data' => [
            'company'        => 'Acme Inc.',
            'invoice_number' => 'INV-001',
            'items' => [
                ['description' => 'Consulting', 'quantity' => 10, 'unit_price' => 120],
            ],
        ],
    ],
]);

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.

Custom paper size & marginsphp
$response = $client->post('https://api.renderpdfs.com/v1/generate', [
    'headers' => ['X-API-Key' => getenv('RENDERPDFS_KEY')],
    'json' => [
        'html'    => '<h1>Report</h1>',
        'options' => [
            'format'           => 'Letter',
            'landscape'        => false,
            'margin'           => ['top' => '20mm', 'bottom' => '20mm', 'left' => '15mm', 'right' => '15mm'],
            'printBackground'  => true,
        ],
    ],
]);

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.

Store the PDF and get a URLphp
$response = $client->post('https://api.renderpdfs.com/v1/generate', [
    'headers' => ['X-API-Key' => getenv('RENDERPDFS_KEY')],
    'json'    => ['html' => '<h1>Shared report</h1>', 'store' => true],
]);

$result = json_decode((string) $response->getBody(), true);
echo "Download at {$result['url']} (expires in {$result['expires_in']}s)";

7. Notes & gotchas

Authentication

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.

Response format

By default the endpoint streams back the raw PDF binary (Content-Type: application/pdf). Set `store: true` in the body and the response becomes { url, expires_in } — useful for emailing links or attaching to webhooks.

Handling errors

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.