Skip to content

PHP convert HTML to PDF

PHP Convert HTML to PDF With a Chromium Rendering API

SitePDF is the way to convert HTML to PDF from PHP with one HTTP call and real Chromium output, so there is no wkhtmltopdf binary and no headless browser on your server. It suits PHP teams whose invoices, statements and reports outgrew Dompdf or mPDF.

  • PDF and archive in one call
  • Plans from $49 a month
  • Your HTML stays yours

Short answer

To convert HTML to PDF in PHP without running a renderer yourself, post the HTML to a rendering API and store the PDF it returns. SitePDF uses managed Chromium, so flexbox, grid, web fonts and print CSS print the way they look in the browser. Starter API is $49 a month for 10,000 renders ($288 billed yearly), Growth is $99 a month for 50,000 and Scale is $299 a month for 250,000.

Written and fact checked by the SitePDF team.

The common ways to convert HTML to PDF in PHP

These are the routes PHP teams pick most often. The right one depends on how complex the HTML is and who looks after the server.

Route How it renders CSS you can rely on What you run Where it hurts
Dompdf Its own PHP layout engine CSS 2.1 and part of CSS 3, no flexbox or grid A Composer package Modern layouts need a table based rewrite
mPDF Its own PHP layout engine Wide HTML and CSS subset, no flexbox or grid A Composer package Memory use on long documents, a second print template
TCPDF Writes PDF commands directly A small HTML subset A Composer package Templates become code, not HTML
wkhtmltopdf with a wrapper An old WebKit engine No modern CSS, limited JavaScript A system binary and fonts The project is archived, so nobody fixes new issues
Browsershot Real Chrome through Puppeteer What Chrome supports Node, Puppeteer and Chromium on the server You operate a browser, its memory and its updates
SitePDF API Managed Chromium What Chrome supports, with print CSS Nothing, one HTTP call Document content leaves your servers, and you pay per render

Plans and prices are from our own pricing page. Library notes are from the projects: Dompdf, wkhtmltopdf status and Browsershot.

When a PHP library is enough and when it is not

A plain letter, a receipt or a one page certificate converts well with Dompdf or mPDF, and there is no reason to pay for an API. The trouble starts when the same HTML must look like your web app, with a flexbox header, a grid of line items, a web font and a chart.

Then the library renders something close but not equal, and a developer keeps a second print template that drifts from the first. If that is your situation, the pages on a Dompdf alternative and an mPDF alternative show the differences in detail.

A worked example with numbers

These are planning assumptions, not measurements. A small SaaS such as Testimonialmanager, which lets customers collect and show quotes, wants an exportable PDF report of those quotes for each client, about 6,000 PDFs a month. The report uses a grid of quote cards and a web font.

Running Browsershot yourself, assume 16 hours of setup at $75 an hour, a $20 a month server, and 3 hours a month of upkeep for browser updates, fonts and memory crashes. Year one is $1,200 plus $240 plus $2,700, or $4,140. Starter API at $288 billed yearly covers 6,000 renders a month with room to spare, so year one is $288. Your hourly rate and upkeep time will differ, so replace those figures with yours.

How the call works from PHP

Render your Blade, Twig or plain PHP template to a string as you do today, then send it. The response holds the PDF address, which you download or hand to the user. Pass archive to keep a dated copy, format for page size, and header_html and footer_html for page numbers on multi page documents.

Laravel teams can read the Laravel version of this page and the general PDF generator API page for the full list of options.

What does not carry over

The API renders HTML to a PDF and nothing else, so it does not merge, split or fill form fields in existing PDFs, and it does not sign documents. Pages behind a login need cookies or headers passed on the call. Your document content is sent to an outside service, which some regulated teams cannot do, and a PHP library keeps it inside your own network.

Who it is for and who it is not for

It is for PHP teams with a developer who can make an HTTP call, whose invoices, statements or reports are already HTML and need to match the screen, from a few thousand to a few hundred thousand documents a month. Product teams that bill on a monthly cycle can see the sizing on HTML to PDF API for SaaS.

It is not for one off letters, fixed layout forms, teams that must keep all content in house, or very high volumes where a tuned cluster of your own would cost less.

Convert an HTML string to a PDF from plain PHP and keep a dated copy.
<?php
$html = file_get_contents(__DIR__ . '/statement.html');

$ch = curl_init('https://api.sitepdf.com/v1/render');
curl_setopt_array($ch, [
    CURLOPT_POST => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => [
        'Authorization: Bearer ' . getenv('SITEPDF_KEY'),
        'Content-Type: application/json',
    ],
    CURLOPT_POSTFIELDS => json_encode([
        'html' => $html,
        'format' => 'A4',
        'archive' => true,
    ]),
]);

$result = json_decode(curl_exec($ch), true);
$pdfUrl = $result['pdf_url'];

This is the documented API call shape. Full request and response walkthrough.

Questions about this job

How do I convert HTML to PDF in PHP?
Render your template to an HTML string, then either pass it to a library such as Dompdf or mPDF, or post it to a rendering API such as SitePDF and download the PDF it returns. The API route prints with real Chromium, so modern CSS works.
What is the best PHP library to convert HTML to PDF?
For simple documents Dompdf and mPDF are the usual choices and are free. For layouts that use flexbox, grid or web fonts, a Chromium based route such as Browsershot or a hosted API prints closer to the browser.
Can PHP convert HTML to PDF without wkhtmltopdf?
Yes. Dompdf, mPDF and TCPDF need no binary, and a hosted API needs only an HTTP call. wkhtmltopdf is archived and its engine lacks modern CSS, so most new projects avoid it.
How much does an HTML to PDF API cost for PHP?
SitePDF Starter API is $49 a month for 10,000 renders, or $288 billed yearly. Growth is $99 a month for 50,000 renders and Scale is $299 a month for 250,000. Count one render per document.
Is there a SitePDF PHP package?
No. The API is plain HTTP with a bearer key, so curl, Guzzle or the Laravel HTTP client all work, and the example above shows the full call in a few lines.

Index

More PDF and archiving tools

Get started

Render and archive with one API call

A pixel perfect PDF and a dated copy of the page from the same request. Plans from $49 a month, half price billed yearly.

HTML to PDF, in your browser

Buy the plan