Code to convert HTML to PDF PHP

The Pdfcrowd API is a web service that lets you easily print web pages and raw HTML code to PDF in your PHP applications. PDFs are created in the cloud, no 3rd party libraries are needed. All you need is a tiny PHP API client library.

Check out the following samples created with the API. Click a thumbnail to open the PDF.

Wikipedia page



Status report

JavaScript vector chart


Code samples

This code converts a web page and sends the generated PDF as an HTTP response:

require 'pdfcrowd.php';

// create an API client instance
$client = new Pdfcrowd("username", "apikey");

// convert a web page and store the generated PDF into a variable
$pdf = $client->convertURI('');

// set HTTP response headers
header("Content-Type: application/pdf");
header("Cache-Control: max-age=0");
header("Accept-Ranges: none");
header("Content-Disposition: attachment; filename=\"google_com.pdf\"");

// send the generated PDF 
echo $pdf;

You can also convert raw HTML code, just use the convertHtml() method instead of convertURI():

$pdf = $client->convertHtml("<body>My HTML Layout</body>");

The API lets you also convert a local HTML file:

$pdf = $client->convertFile("/path/to/MyLayout.html");

Did you know ...

  • PDF encapsulates a complete description of fixed-layout flat document.
  • PDF can embed used fonts and thus allow fonts to travel with the documents.
  • PDF was introduced by Adobe Systems in 1993

Other Languages