Code for convert HTML to PDF in PHP
by Dinesh[ Edit ] 2012-10-06 14:24:41
Convert HTML to PDF in PHP
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.hiox.com');
// set HTTP response headers
header("Content-Type: application/pdf");
header("Cache-Control: no-cache");
header("Accept-Ranges: none");
header("Content-Disposition: attachment; filename="hiox_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("My HTML Layout");
The API lets you also convert a local HTML file:
$pdf = $client->convertFile("/path/to/MyLayout.html");