How to print HTML to PDF PHP

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


Newsletter

Invoice

Status report

Wikipedia page

JavaScript vector chart

     

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: no-cache");
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>");

Or use convertFile() to convert a local HTML file:

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

Did you know ...

  • PDF supports security. The creator of a PDF file can set various security options. It is possible to lock a PDF so it can only be viewed with a password. It is also possible to forbid changing the content of a PDF or disable the option to print a PDF file.
  • PDF was created by Adobe Systems in 1993
  • PDF can embed used fonts and thus allow fonts to travel with the documents.

Other Languages