PHP5 min read · June 2, 2026

Convert HTML to PDF in PHP Without Installing Anything

Generate PDFs from HTML in plain PHP using a REST API. No Composer packages required — just a single HTTP request with curl or file_get_contents.

Most PHP PDF tutorials start with "install this library" — and then you spend an afternoon wrestling with dependencies, system fonts, or a binary that won't work on your shared host. This tutorial skips all of that.

The HTML to PDF API is a REST service: you POST your HTML, you GET a PDF back. No Composer, no extensions, no binaries. Any PHP version that can make an HTTP request can use it.

Generate a PDF with PHP curl

Here's the full working example using curl — the most universally available option in PHP:

generate-pdf.phpphp
<?php

$apiKey = 'your_api_key_here';
$html   = '<h1>Hello World</h1><p>Generated with PHP and HTML to PDF API.</p>';

$ch = curl_init('https://platform.htmltopdfapi.co/api/v1/pdf/generate');
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST           => true,
    CURLOPT_HTTPHEADER     => [
        'Authorization: Bearer ' . $apiKey,
        'Content-Type: application/json',
        'Accept: application/pdf',
    ],
    CURLOPT_POSTFIELDS => json_encode([
        'html'        => $html,
        'paper_size'  => 'a4',
        'orientation' => 'portrait',
    ]),
]);

$pdf        = curl_exec($ch);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if ($statusCode === 200) {
    header('Content-Type: application/pdf');
    header('Content-Disposition: inline; filename="document.pdf"');
    echo $pdf;
} else {
    http_response_code(500);
    echo 'PDF generation failed: ' . $pdf;
}

Convert a URL to PDF

Prefer to render a URL? Swap the html field for url:

url-to-pdf.phpphp
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
    'url'         => 'https://example.com',
    'paper_size'  => 'a4',
    'orientation' => 'portrait',
]));

Save to Disk Instead of Streaming

To save the PDF to a file rather than streaming it:

save-to-disk.phpphp
if ($statusCode === 200) {
    file_put_contents('/tmp/output.pdf', $pdf);
    echo 'Saved to /tmp/output.pdf';
}

Using Guzzle or the PHP SDK

If you're already using Guzzle or are in a Laravel/Symfony project, the official PHP SDK wraps these calls with a fluent interface and handles errors gracefully. See the integration guide for details.

Either way — with or without a library — the API itself is just HTTP. Any PHP environment can use it.