PHP HTML to PDF API
Convert any URL into a pixel-perfect PDF with one HTTP request in PHP — no dompdf, no headless Chrome, no servers to maintain.
Convert a URL to PDF in PHP
No Composer package and no rendering library — the Microlink REST API turns any URL into a hosted PDF with a single HTTP GET. Here it is with file_get_contents and the cURL extension that ship with PHP.
Step 01 · Convert any URL
A one-liner with file_get_contents — no Composer package to add. Point it at a page and read the hosted document URL from the JSON response.
convert.php
<?php
$query = http_build_query([
'url' => 'https://example.com',
'pdf' => 'true',
'meta' => 'false', // skip metadata extraction for a faster response
]);
$res = json_decode(file_get_contents("https://api.microlink.io?$query"), true);
echo $res['data']['pdf']['url'];Step 02 · Use cURL for more control
When you need timeouts and error handling in production, the bundled cURL extension does the same job.
curl.php
<?php
$query = http_build_query([
'url' => 'https://example.com',
'pdf' => 'true',
'meta' => 'false',
]);
$ch = curl_init("https://api.microlink.io?$query");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
try {
$body = curl_exec($ch);
if ($body === false) throw new RuntimeException(curl_error($ch));
$code = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
} finally {
curl_close($ch);
}
$res = json_decode($body, true, 512, JSON_THROW_ON_ERROR);
if ($code !== 200 || !is_array($res) || ($res['status'] ?? null) !== 'success') {
throw new RuntimeException($res['message'] ?? "HTTP $code");
}
$url = $res['data']['pdf']['url'] ?? null;
if (!is_string($url)) throw new RuntimeException('Malformed API response');
echo $url;Step 03 · Customize the document
Paper format, margins, orientation, and print CSS are all query fields. http_build_query handles the encoding, and nested options use dot notation.
options.php
<?php
$query = http_build_query([
'url' => 'https://example.com',
'pdf.format' => 'A4', // A0-A6 | Letter | Legal | Tabloid
'pdf.margin' => '0.35cm', // cm, mm, in or px
'pdf.landscape' => 'false', // portrait (default) | landscape
'mediaType' => 'print', // print CSS stylesheets | screen (default)
'meta' => 'false',
]);
$res = json_decode(file_get_contents("https://api.microlink.io?$query"), true);
echo $res['data']['pdf']['url'];Step 04 · Save it to disk
The response is a hosted PDF URL on a global CDN. Copy it to disk with file_put_contents, or redirect the visitor straight to it.
save.php
<?php
$query = http_build_query([
'url' => 'https://example.com',
'pdf' => 'true',
]);
$res = json_decode(file_get_contents("https://api.microlink.io?$query"), true);
$pdf = $res['data']['pdf']['url'];
file_put_contents('document.pdf', file_get_contents($pdf));Drop it into your framework
A route, a controller, or a shortcode — the same request becomes your own PDF endpoint, perfect for invoice downloads, packing slips, and report links in Laravel, Symfony, or WordPress.
- Laravel
- Symfony
- WordPress
- PHP
<?php
namespace App\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\HttpClient\HttpClientInterface;
class PdfController
{
// GET /pdf?url=https://example.com
#[Route('/pdf')]
public function convert(Request $request, HttpClientInterface $client): RedirectResponse
{
$res = $client->request('GET', 'https://api.microlink.io', [
'query' => [
'url' => $request->query->get('url'),
'pdf' => 'true',
'meta' => 'false',
],
])->toArray();
return new RedirectResponse($res['data']['pdf']['url']);
}
}Skip the dompdf limitations
Generating PDFs in PHP means fighting dompdf and mPDF CSS limits, wrapping an archived wkhtmltopdf binary, or running a headless browser next to PHP-FPM. The API gives you a real browser rendering engine without any of the infrastructure.
Self-hosted PDF tooling
- dompdf & mPDF never execute JavaScript, so dynamic pages render blank
- Limited modern CSS — flexbox and grid layouts break or misalign
- Snappy wraps wkhtmltopdf, which is archived upstream
- Running headless Chrome next to PHP-FPM stalls workers under load
- You build the process pool, queueing, retries and autoscaling
- Fonts, emoji, and page breaks behave differently on every host
Microlink for PHP
- One HTTP request — cURL or file_get_contents, no extension to add
- Runs anywhere: shared hosting, serverless, containers, your laptop
- Autoscaled managed browser fleet with a 99.9% uptime SLA
- Sub-second cached responses from 330+ edge locations
- A0-A6, Letter, Legal & Tabloid — set with http_build_query
- Print stylesheets, custom CSS & DOM interaction on any host
Built for the way you write PHP.
A REST API that feels native in PHP — one HTTP call, JSON back, and at home on any host from shared hosting to a Laravel app. Read the PDF guide to go deeper.
No Libraries to Install
Skip dompdf, mPDF, and wkhtmltopdf entirely. There is no rendering engine to install, patch, or keep in sync across hosts.
cURL or file_get_contents
Use the bundled cURL extension or a one-line file_get_contents — it is a plain HTTP GET, with no Composer package required.
Framework Friendly
Drop it into Laravel, Symfony, or WordPress as a route, controller, or shortcode in a few lines.
Runs on Any Host
Works on shared hosting, serverless, and containers alike — there are no binaries or system libraries to ship.
Real Browser Rendering
Pages render in Headless Chrome, so flexbox, grid, web fonts, and JavaScript all survive — the things dompdf and mPDF silently drop.
Zero Infrastructure
No Chrome process sitting next to PHP-FPM competing for memory. Your host only makes an outbound HTTP request.
Custom Paper & Layout
Every layout option is a query field: pdf.format, pdf.margin, pdf.landscape, pdf.scale, and pdf.pageRanges.
Screen & Print Media
Add mediaType => print to the same query array when you want print stylesheets instead of the on-screen layout.
Generous Free Tier
Start free at 25 requests per day. Production traffic moves to pro.microlink.io with an x-api-key header.
No Libraries to Install
Skip dompdf, mPDF, and wkhtmltopdf entirely. There is no rendering engine to install, patch, or keep in sync across hosts.cURL or file_get_contents
Use the bundled cURL extension or a one-line file_get_contents — it is a plain HTTP GET, with no Composer package required.Framework Friendly
Drop it into Laravel, Symfony, or WordPress as a route, controller, or shortcode in a few lines.
Runs on Any Host
Works on shared hosting, serverless, and containers alike — there are no binaries or system libraries to ship.Real Browser Rendering
Pages render in Headless Chrome, so flexbox, grid, web fonts, and JavaScript all survive — the things dompdf and mPDF silently drop.Zero Infrastructure
No Chrome process sitting next to PHP-FPM competing for memory. Your host only makes an outbound HTTP request.
Custom Paper & Layout
Every layout option is a query field: pdf.format, pdf.margin, pdf.landscape, pdf.scale, and pdf.pageRanges.Screen & Print Media
Add mediaType => print to the same query array when you want print stylesheets instead of the on-screen layout.Generous Free Tier
Start free at 25 requests per day. Production traffic moves to pro.microlink.io with an x-api-key header.
Try it live in the playground
Paste a URL and see the exact API request before you write a line of PHP.
PHP PDF FAQ
What PHP developers ask before integrating. For formats, limits, and SLA, see the PDF API overview.
Do I need dompdf, mPDF, or a headless browser?
No. It is a plain HTTP request to the Microlink API — there is no rendering library to install and no Chromium to download. The Headless Chrome fleet runs on Microlink's side.
That also means JavaScript-heavy pages render correctly — unlike dompdf or mPDF, which never execute scripts.
Does it work on shared hosting?
Yes. There is nothing to compile and no Composer package to add — the cURL extension ships with virtually every PHP install, and
file_get_contents covers the rest wherever allow_url_fopen is enabled.That matters because the usual alternatives do not: wrapping
wkhtmltopdf or running headless Chrome needs shell access and system libraries most shared hosts do not give you.cURL or file_get_contents?
Either works.
file_get_contents is a one-liner when allow_url_fopen is enabled; the cURL extension gives you timeouts and richer error handling for production.Does it work with Laravel, Symfony, and WordPress?
Yes. Because it is just an HTTP call, it drops into a Laravel route, a Symfony controller, or a WordPress shortcode in a few lines — see the tabs above, or the PDF guide.
How do I authenticate from PHP?
Two things change together: send your key as the
x-api-key header, and point the request at pro.microlink.io instead of api.microlink.io. Sending the header to the free endpoint returns an EPRO error.With cURL that is
curl_setopt($ch, CURLOPT_HTTPHEADER, ['x-api-key: KEY']). See the authentication docs and pricing.Will PDF generation block my PHP-FPM workers?
Far less than the alternatives. No Chrome process spawns on your host, so a conversion costs one worker waiting on an outbound request rather than hundreds of megabytes of RAM per document.
The request is still synchronous, so for bulk jobs queue it — Laravel jobs, a Symfony messenger handler, or WP-Cron — and cached documents come back sub-second on repeat conversions.
Start converting in PHP
Get 25 requests/day with zero commitment — no account, no credit card. Drop the snippet into a route and ship a PDF today.
No login needed
25 reqs/day free
No credit card