PDFPipe

Guide

Generate a PDF from HTML in PHP

You have a PHP script, some HTML (an invoice, a receipt, a certificate), and you need to hand the browser a PDF. The usual answers are dompdf, mpdf, or a wkhtmltopdf binary, each with its own CSS surprises and server requirements. This guide shows the lighter path: one cURL call that works on any host PHP runs on, including shared hosting where you cannot install binaries at all.

The minimal script

The PDFPipe endpoint is POST https://api.pdfpipe.xyz/v1/pdf. Send a JSON body with your HTML and options, attach a bearer token, and you get back raw application/pdf bytes you can echo straight to the browser.

php
<?php

$id = $_GET['id'] ?? '1001';

$html = <<<HTML
<html>
  <body style="font-family: sans-serif; padding: 40px">
    <h1>Invoice #{$id}</h1>
    <p>Amount due: $420.00</p>
  </body>
</html>
HTML;

$ch = curl_init('https://api.pdfpipe.xyz/v1/pdf');
curl_setopt_array($ch, [
    CURLOPT_POST           => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_TIMEOUT        => 20,
    CURLOPT_HTTPHEADER     => [
        'Content-Type: application/json',
        'Authorization: Bearer ' . getenv('PDFPIPE_KEY'),
    ],
    CURLOPT_POSTFIELDS => json_encode([
        'html'    => $html,
        'options' => ['format' => 'A4'],
    ]),
]);

$pdf    = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
curl_close($ch);

if ($status !== 200) {
    http_response_code(502);
    exit('PDF render failed: ' . $pdf);
}

header('Content-Type: application/pdf');
header('Content-Disposition: inline; filename="invoice-' . $id . '.pdf"');
echo $pdf;

That is the whole feature, in one file, with zero Composer dependencies. cURL ships with effectively every PHP install. If you prefer Guzzle in a framework codebase, the request is the same three lines of configuration. (Using Laravel? There is a dedicated Laravel guide with the Http facade.)

Why not dompdf or mpdf?

Pure-PHP renderers parse and lay out HTML themselves, which means they support the CSS they got around to implementing. The moment your invoice template uses flexbox, grid, or a web font with fallbacks, the output stops matching what you designed in the browser.

Concerndompdf / mpdf / wkhtmltopdfcURL to PDFPipe
Modern CSSPartial: flexbox and grid break or are ignoredCurrent engine, matches your browser
Memory limitLong documents hit memory_limit wallsRender happens off your server
Shared hostingwkhtmltopdf binary often cannot be installedWorks anywhere cURL works
FontsManual font registration and subsettingWeb fonts load like in a browser

Saving to disk instead of streaming

Generating documents in a queue job or cron? Write the bytes to storage instead of echoing them.

php
if ($status === 200) {
    file_put_contents(__DIR__ . "/storage/invoice-{$id}.pdf", $pdf);
}

Keep the API key in an environment variable, never in the repository. The options object also accepts other page sizes (for example Letter), margins, and landscape orientation, documented in full on the docs page.

Paste your own HTML into the live playground on the home page to see the PDF render instantly, then read the docs for every option.

See pricing →