How to export HTML to PDF from PHP

The Pdfcrowd API is a web service that lets you easily generate PDF from web pages and raw HTML code in your PHP applications. PDFs are generated 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

Newsletter

Status report

Wikipedia page

Code samples

The following 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: max-age=0");
header("Accept-Ranges: none");
header("Content-Disposition: attachment; filename=\"google_com.pdf\"");

// send the generated PDF 
echo $pdf;

Another option is to 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 preserves document fidelity independently of the device, platform, and software
  • PDF encapsulates a complete description of fixed-layout flat document.
  • PDF can embed used fonts and thus allow fonts to travel with the documents.

Other Languages