How to convert HTML to PDF in PHP

The Pdfcrowd API is a web service that lets you easily export 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.

Have a look at the following samples created with the API. Click a thumbnail to open the PDF.


JavaScript vector chart

Invoice

Wikipedia page

Status report

Newsletter

     

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('http://www.google.com/');

// set HTTP response headers
header("Content-Type: application/pdf");
header("Cache-Control: no-cache");
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 was developed by Adobe Systems in 1993
  • PDF can embed used fonts and thus allow fonts to travel with the documents.
  • PDF encapsulates a complete description of fixed-layout flat document.

Other Languages