PHP code to convert HTML to PDF

The Pdfcrowd API is an online tool that lets you easily convert web pages and raw HTML code to PDF 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.


Status report

JavaScript vector chart

Newsletter

Invoice

Wikipedia page

Code samples

The following code converts a web page and sends the generated PDF to the browser:

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;

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 preserves document fidelity independently of the device, platform, and software
  • PDF supports security and permissions to allow the author to retain control of the document and associated rights.
  • PDF can embed used fonts and thus allow fonts to travel with the documents.

Other Languages